-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce @JsonIgnoreUnknownKeys annotation (#2874)
for more fine-grained control over JsonBuilder.ignoreUnknownKeys setting. Fixes #1420 Also, improve error message and path handling when an 'Unknown key' exception is thrown. Fixes #2869 Fixes #2637
- Loading branch information
1 parent
6684f67
commit aee6336
Showing
44 changed files
with
626 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonIgnoreKeysTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.json | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.test.checkSerializationException | ||
import kotlin.test.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class JsonIgnoreKeysTest : JsonTestBase() { | ||
val ignoresKeys = Json(default) { ignoreUnknownKeys = true } | ||
|
||
@Serializable | ||
class Outer(val a: Int, val inner: Inner) | ||
|
||
@Serializable | ||
@JsonIgnoreUnknownKeys | ||
class Inner(val x: String) | ||
|
||
@Test | ||
fun testIgnoresKeyWhenGlobalSettingNotSet() = parametrizedTest { mode -> | ||
val jsonString = """{"a":1,"inner":{"x":"value","unknownKey":"unknownValue"}}""" | ||
val result = default.decodeFromString<Outer>(jsonString, mode) | ||
assertEquals(1, result.a) | ||
assertEquals("value", result.inner.x) | ||
} | ||
|
||
@Test | ||
fun testThrowsWithoutAnnotationWhenGlobalSettingNotSet() = parametrizedTest { mode -> | ||
val jsonString = """{"a":1,"inner":{"x":"value","unknownKey":"unknownValue"}, "b":2}""" | ||
checkSerializationException({ | ||
default.decodeFromString<Outer>(jsonString, mode) | ||
}) { msg -> | ||
assertContains( | ||
msg, | ||
if (mode == JsonTestingMode.TREE) "Encountered an unknown key 'b' at element: \$\n" | ||
else "Encountered an unknown key 'b' at offset 59 at path: \$\n" | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testIgnoresBothKeysWithGlobalSetting() = parametrizedTest { mode -> | ||
val jsonString = """{"a":1,"inner":{"x":"value","unknownKey":"unknownValue"}, "b":2}""" | ||
val result = ignoresKeys.decodeFromString<Outer>(jsonString, mode) | ||
assertEquals(1, result.a) | ||
assertEquals("value", result.inner.x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.