|
1 | 1 | package kotlinx.serialization.json
|
2 | 2 |
|
3 |
| -import kotlinx.serialization.Serializable |
| 3 | +import kotlinx.serialization.* |
| 4 | +import kotlinx.serialization.descriptors.* |
| 5 | +import kotlinx.serialization.encoding.* |
4 | 6 | import kotlin.test.*
|
5 | 7 |
|
6 | 8 | class JsonElementDecodingTest : JsonTestBase() {
|
@@ -51,4 +53,58 @@ class JsonElementDecodingTest : JsonTestBase() {
|
51 | 53 | json = json.replace("%", "0")
|
52 | 54 | Json.parseToJsonElement(json)
|
53 | 55 | }
|
| 56 | + |
| 57 | + private open class NullAsElementSerializer<T : Any>(private val serializer: KSerializer<T>, val nullElement: T) : KSerializer<T?> { |
| 58 | + final override val descriptor: SerialDescriptor = serializer.descriptor.nullable |
| 59 | + |
| 60 | + final override fun serialize(encoder: Encoder, value: T?) { |
| 61 | + serializer.serialize(encoder, value ?: nullElement) |
| 62 | + } |
| 63 | + |
| 64 | + final override fun deserialize(decoder: Decoder): T = serializer.deserialize(decoder) |
| 65 | + } |
| 66 | + |
| 67 | + private object NullAsJsonNullJsonElementSerializer : NullAsElementSerializer<JsonElement>(JsonElement.serializer(), JsonNull) |
| 68 | + private object NullAsJsonNullJsonPrimitiveSerializer : NullAsElementSerializer<JsonPrimitive>(JsonPrimitive.serializer(), JsonNull) |
| 69 | + private object NullAsJsonNullJsonNullSerializer : NullAsElementSerializer<JsonNull>(JsonNull.serializer(), JsonNull) |
| 70 | + private val noExplicitNullsOrDefaultsJson = Json { |
| 71 | + explicitNulls = false |
| 72 | + encodeDefaults = false |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testNullableJsonElementDecoding() { |
| 77 | + @Serializable |
| 78 | + data class Wrapper( |
| 79 | + @Serializable(NullAsJsonNullJsonElementSerializer::class) |
| 80 | + val value: JsonElement? = null, |
| 81 | + ) |
| 82 | + |
| 83 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = JsonNull), """{"value":null}""", noExplicitNullsOrDefaultsJson) |
| 84 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = null), """{}""", noExplicitNullsOrDefaultsJson) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun testNullableJsonPrimitiveDecoding() { |
| 89 | + @Serializable |
| 90 | + data class Wrapper( |
| 91 | + @Serializable(NullAsJsonNullJsonPrimitiveSerializer::class) |
| 92 | + val value: JsonPrimitive? = null, |
| 93 | + ) |
| 94 | + |
| 95 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = JsonNull), """{"value":null}""", noExplicitNullsOrDefaultsJson) |
| 96 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = null), """{}""", noExplicitNullsOrDefaultsJson) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun testNullableJsonNullDecoding() { |
| 101 | + @Serializable |
| 102 | + data class Wrapper( |
| 103 | + @Serializable(NullAsJsonNullJsonNullSerializer::class) |
| 104 | + val value: JsonNull? = null, |
| 105 | + ) |
| 106 | + |
| 107 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = JsonNull), """{"value":null}""", noExplicitNullsOrDefaultsJson) |
| 108 | + assertJsonFormAndRestored(Wrapper.serializer(), Wrapper(value = null), """{}""", noExplicitNullsOrDefaultsJson) |
| 109 | + } |
54 | 110 | }
|
0 commit comments