diff options
author | Leonid Startsev <sandwwraith@users.noreply.github.com> | 2023-09-14 19:53:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-14 19:53:40 +0200 |
commit | 7d287c84e83e6af4c5c0ecb27cd573db9d0cffd9 (patch) | |
tree | 85a1425752c4ee6d8d3f5cd3a34a9525ce4d5d5c /formats/json-tests/commonTest/src/kotlinx/serialization | |
parent | 01fcfeefca0ccfa836dcddac8d6fc7a9a409e2cf (diff) | |
download | kotlinx.serialization-7d287c84e83e6af4c5c0ecb27cd573db9d0cffd9.tar.gz |
Support decoding maps with boolean keys (#2440)
We ignore quoted/unquoted state when decoding maps with number keys, so it is logical to do the same for boolean maps.
Fixes #2438
Diffstat (limited to 'formats/json-tests/commonTest/src/kotlinx/serialization')
-rw-r--r-- | formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt | 37 | ||||
-rw-r--r-- | formats/json-tests/commonTest/src/kotlinx/serialization/json/LenientTest.kt | 2 |
2 files changed, 36 insertions, 3 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt index 61f31de9..560e51fe 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt @@ -5,6 +5,7 @@ package kotlinx.serialization.json import kotlinx.serialization.* +import kotlinx.serialization.builtins.* import kotlinx.serialization.descriptors.PrimitiveKind import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor @@ -42,6 +43,9 @@ class JsonMapKeysTest : JsonTestBase() { private data class WithMap(val map: Map<Long, Long>) @Serializable + private data class WithBooleanMap(val map: Map<Boolean, Boolean>) + + @Serializable private data class WithValueKeyMap(val map: Map<PrimitiveCarrier, Long>) @Serializable @@ -60,13 +64,42 @@ class JsonMapKeysTest : JsonTestBase() { private data class WithContextualKey(val map: Map<@Contextual ContextualValue, Long>) @Test - fun testMapKeysShouldBeStrings() = parametrizedTest(default) { + fun testMapKeysSupportNumbers() = parametrizedTest { assertStringFormAndRestored( """{"map":{"10":10,"20":20}}""", WithMap(mapOf(10L to 10L, 20L to 20L)), WithMap.serializer(), - this + default + ) + } + + @Test + fun testMapKeysSupportBooleans() = parametrizedTest { + assertStringFormAndRestored( + """{"map":{"true":false,"false":true}}""", + WithBooleanMap(mapOf(true to false, false to true)), + WithBooleanMap.serializer(), + default + ) + } + + // As a result of quoting ignorance when parsing primitives, it is possible to parse unquoted maps if Kotlin keys are non-string primitives. + // This is not spec-compliant, but I do not see any problems with it. + @Test + fun testMapDeserializesUnquotedKeys() = parametrizedTest { + assertEquals(WithMap(mapOf(10L to 10L, 20L to 20L)), default.decodeFromString("""{"map":{10:10,20:20}}""")) + assertEquals( + WithBooleanMap(mapOf(true to false, false to true)), + default.decodeFromString("""{"map":{true:false,false:true}}""") ) + assertFailsWithSerial("JsonDecodingException") { + default.decodeFromString( + MapSerializer( + String.serializer(), + Boolean.serializer() + ),"""{"map":{true:false,false:true}}""" + ) + } } @Test diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/LenientTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/LenientTest.kt index 41d3c2c2..d24c7d7c 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/LenientTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/LenientTest.kt @@ -37,7 +37,7 @@ class LenientTest : JsonTestBase() { @Test fun testQuotedBoolean() = parametrizedTest { val json = """{"i":1, "l":2, "b":"true", "s":"string"}""" - assertFailsWithSerial("JsonDecodingException") { default.decodeFromString(Holder.serializer(), json, it) } + assertEquals(value, default.decodeFromString(Holder.serializer(), json, it)) assertEquals(value, lenient.decodeFromString(Holder.serializer(), json, it)) } |