diff --git a/docs/basic-serialization.md b/docs/basic-serialization.md index 14a3e82de..7425be7a6 100644 --- a/docs/basic-serialization.md +++ b/docs/basic-serialization.md @@ -297,7 +297,7 @@ fun main() { It produces the exception: ```text -Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses04.Project', but it was missing +Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses04.Project', but it was missing at path: $ ``` @@ -383,7 +383,7 @@ fun main() { We get the following exception. ```text -Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses07.Project', but it was missing +Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses07.Project', but it was missing at path: $ ``` @@ -411,7 +411,7 @@ Attempts to explicitly specify its value in the serial format, even if the speci value is equal to the default one, produces the following exception. ```text -Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 42: Encountered an unknown key 'language'. +Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 42: Encountered an unknown key 'language' at path: $.name Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys. ``` @@ -493,7 +493,7 @@ Even though the `language` property has a default value, it is still an error to the `null` value to it. ```text -Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 52: Expected string literal but 'null' literal was found. +Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 52: Expected string literal but 'null' literal was found at path: $.language Use 'coerceInputValues = true' in 'Json {}` builder to coerce nulls to default values. ``` diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt index 2b8dce915..6c1372950 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonPath.kt @@ -90,7 +90,6 @@ internal class JsonPath { @OptIn(ExperimentalSerializationApi::class) fun getPath(): String { - if (currentDepth == -1) return "" return buildString { append("$") repeat(currentDepth + 1) { diff --git a/formats/json/commonTest/src/kotlinx/serialization/JsonPathTest.kt b/formats/json/commonTest/src/kotlinx/serialization/JsonPathTest.kt index 9fcf16648..228e0a736 100644 --- a/formats/json/commonTest/src/kotlinx/serialization/JsonPathTest.kt +++ b/formats/json/commonTest/src/kotlinx/serialization/JsonPathTest.kt @@ -32,6 +32,13 @@ class JsonPathTest : JsonTestBase() { expectPath("$.i.d[1]") { Json.decodeFromString("""{"a":42, "i":{"d":{1:{}}""") } } + @Test + fun testUnknownKeyIsProperlyReported() { + expectPath("$.i") { Json.decodeFromString("""{"a":42, "i":{"foo":42}""") } + expectPath("$") { Json.decodeFromString("""{"x":{}, "a": 42}""") } +// Json.decodeFromString("""{"a":42, "x":{}}""") + } + @Test fun testMalformedRootObject() { expectPath("$") { Json.decodeFromString("""{{""") } @@ -58,6 +65,7 @@ class JsonPathTest : JsonTestBase() { expectPath("$.i.d\n") { Json.decodeFromString("""{"a":42, "i":{ "d": {"foo": {}}""") } expectPath("$.i.d\n") { Json.decodeFromString("""{"a":42, "i":{ "d": {42: {"s":"s"}, 42.0:{}}""") } expectPath("$\n") { Json.decodeFromString>("""{"foo":"bar"}""") } + expectPath("$\n") { Json.decodeFromString>("""{42:"bar", "foo":"bar"}""") } } @Test diff --git a/guide/test/BasicSerializationTest.kt b/guide/test/BasicSerializationTest.kt index 88c01857c..0024159db 100644 --- a/guide/test/BasicSerializationTest.kt +++ b/guide/test/BasicSerializationTest.kt @@ -51,7 +51,7 @@ class BasicSerializationTest { @Test fun testExampleClasses04() { captureOutput("ExampleClasses04") { example.exampleClasses04.main() }.verifyOutputLinesStart( - "Exception in thread \"main\" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses04.Project', but it was missing" + "Exception in thread \"main\" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses04.Project', but it was missing at path: $" ) } @@ -72,14 +72,14 @@ class BasicSerializationTest { @Test fun testExampleClasses07() { captureOutput("ExampleClasses07") { example.exampleClasses07.main() }.verifyOutputLinesStart( - "Exception in thread \"main\" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses07.Project', but it was missing" + "Exception in thread \"main\" kotlinx.serialization.MissingFieldException: Field 'language' is required for type with serial name 'example.exampleClasses07.Project', but it was missing at path: $" ) } @Test fun testExampleClasses08() { captureOutput("ExampleClasses08") { example.exampleClasses08.main() }.verifyOutputLinesStart( - "Exception in thread \"main\" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 42: Encountered an unknown key 'language'.", + "Exception in thread \"main\" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 42: Encountered an unknown key 'language' at path: $.name", "Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys." ) } @@ -101,7 +101,7 @@ class BasicSerializationTest { @Test fun testExampleClasses11() { captureOutput("ExampleClasses11") { example.exampleClasses11.main() }.verifyOutputLinesStart( - "Exception in thread \"main\" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 52: Expected string literal but 'null' literal was found.", + "Exception in thread \"main\" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 52: Expected string literal but 'null' literal was found at path: $.language", "Use 'coerceInputValues = true' in 'Json {}` builder to coerce nulls to default values." ) }