diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonTreeReader.kt b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonTreeReader.kt index b6d56b044..7c01daa8f 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonTreeReader.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonTreeReader.kt @@ -1,10 +1,10 @@ /* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.serialization.json.internal -import kotlinx.serialization.* +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.* @OptIn(ExperimentalSerializationApi::class) @@ -34,8 +34,10 @@ internal class JsonTreeReader( result[key] = element // Verify the next token lastToken = lexer.consumeNextToken() - if (lastToken != TC_COMMA && lastToken != TC_END_OBJ) { - lexer.fail("Expected end of the object or comma") + when (lastToken) { + TC_COMMA -> Unit // no-op, can continue with `canConsumeValue` that verifies the token after comma + TC_END_OBJ -> break // `canConsumeValue` can return incorrect result, since it checks token _after_ TC_END_OBJ + else -> lexer.fail("Expected end of the object or comma") } } // Check for the correct ending diff --git a/formats/json/jvmTest/src/kotlinx/serialization/features/JsonStreamFlowTest.kt b/formats/json/jvmTest/src/kotlinx/serialization/features/JsonStreamFlowTest.kt index 455a00381..3de5a615f 100644 --- a/formats/json/jvmTest/src/kotlinx/serialization/features/JsonStreamFlowTest.kt +++ b/formats/json/jvmTest/src/kotlinx/serialization/features/JsonStreamFlowTest.kt @@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.features.sealed.SealedChild +import kotlinx.serialization.features.sealed.SealedParent import kotlinx.serialization.json.* import kotlinx.serialization.json.internal.JsonDecodingException import kotlinx.serialization.test.assertFailsWithMessage @@ -108,6 +110,32 @@ class JsonStreamFlowTest { assertEquals(inputList, json.decodeToSequence(ins, StringData.serializer()).toList()) } + @Test + fun testJsonElement() { + val list = listOf( + buildJsonObject { put("foo", "bar") }, + buildJsonObject { put("foo", "baz") }, + JsonPrimitive(10), + JsonPrimitive("abacaba"), + buildJsonObject { put("foo", "qux") } + ) + val inputWs = """${list[0]} ${list[1]} ${list[2]} ${list[3]} ${list[4]}""" + val decodedWs = json.decodeToSequence(inputWs.asInputStream()).toList() + assertEquals(list, decodedWs, "Failed whitespace-separated test") + val inputArray = """[${list[0]}, ${list[1]},${list[2]} , ${list[3]} ,${list[4]}]""" + val decodedArrayWrap = json.decodeToSequence(inputArray.asInputStream()).toList() + assertEquals(list, decodedArrayWrap, "Failed array-wrapped test") + } + + + @Test + fun testSealedClasses() { + val input = """{"type":"first child","i":1,"j":10} {"type":"first child","i":1,"j":11}""" + val iter = json.iterateOverStream(input.asInputStream(), SealedParent.serializer()) + iter.assertNext(SealedChild(10)) + iter.assertNext(SealedChild(11)) + } + @Test fun testMalformedArray() { val input1 = """[1, 2, 3"""