-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test of support for nullable types by not null serializers (#1269)
Tests for #1265
- Loading branch information
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
formats/json/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.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,57 @@ | ||
@file:UseSerializers(GenericSerializersOnFileTest.MySerializer::class) | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class GenericSerializersOnFileTest { | ||
data class GenericClass<T>(val t: T) | ||
|
||
@Serializable | ||
data class Holder(val notnull: GenericClass<String>, val nullable: GenericClass<String>?) | ||
|
||
class MySerializer<E>(val tSer: KSerializer<E>) : KSerializer<GenericClass<E>> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("my int descriptor", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: GenericClass<E>) { | ||
encoder.encodeString(value.t as String) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): GenericClass<E> { | ||
return GenericClass(decoder.decodeString() as E) | ||
} | ||
} | ||
|
||
@Test | ||
fun testSerialize() { | ||
assertEquals( | ||
"""{"notnull":"Not Null","nullable":null}""", | ||
Json.encodeToString(Holder(notnull = GenericClass("Not Null"), nullable = null)) | ||
) | ||
assertEquals( | ||
"""{"notnull":"Not Null","nullable":"Nullable"}""", | ||
Json.encodeToString(Holder(notnull = GenericClass("Not Null"), nullable = GenericClass("Nullable"))) | ||
) | ||
} | ||
|
||
@Test | ||
fun testDeserialize() { | ||
assertEquals( | ||
Holder(notnull = GenericClass("Not Null"), nullable = null), | ||
Json.decodeFromString("""{"notnull":"Not Null","nullable":null}""") | ||
) | ||
assertEquals( | ||
Holder(notnull = GenericClass("Not Null"), nullable = GenericClass("Nullable")), | ||
Json.decodeFromString("""{"notnull":"Not Null","nullable":"Nullable"}""") | ||
) | ||
} | ||
|
||
|
||
} |
109 changes: 109 additions & 0 deletions
109
...ts/json/commonTest/src/kotlinx/serialization/NotNullSerializersCompatibilityOnFileTest.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,109 @@ | ||
/* | ||
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
@file:UseSerializers(NotNullSerializersCompatibilityOnFileTest.NonNullableIntSerializer::class) | ||
@file:UseContextualSerialization(NotNullSerializersCompatibilityOnFileTest.FileContextualType::class) | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
import kotlin.test.* | ||
|
||
class NotNullSerializersCompatibilityOnFileTest { | ||
data class FileContextualType(val text: String) | ||
|
||
object FileContextualSerializer : KSerializer<FileContextualType> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("FileContextualSerializer", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: FileContextualType) { | ||
return encoder.encodeString(value.text) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): FileContextualType { | ||
return FileContextualType(decoder.decodeString()) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class FileContextualHolder(val nullable: FileContextualType?, val nonNullable: FileContextualType) | ||
|
||
|
||
data class ContextualType(val text: String) | ||
|
||
object ContextualSerializer : KSerializer<ContextualType> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("FileContextualSerializer", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: ContextualType) { | ||
return encoder.encodeString(value.text) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): ContextualType { | ||
return ContextualType(decoder.decodeString()) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class ContextualHolder(@Contextual val nullable: ContextualType?, @Contextual val nonNullable: ContextualType) | ||
|
||
|
||
@Serializable | ||
data class Holder(val nullable: Int?, val nonNullable: Int) | ||
|
||
@Serializer(forClass = Int::class) | ||
object NonNullableIntSerializer : KSerializer<Int> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("NotNullIntSerializer", PrimitiveKind.INT) | ||
|
||
override fun serialize(encoder: Encoder, value: Int) { | ||
return encoder.encodeInt(value + 2) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Int { | ||
return (decoder.decodeInt() - 2) | ||
} | ||
} | ||
|
||
@Test | ||
fun testFileLevel() { | ||
assertEquals("""{"nullable":null,"nonNullable":52}""", Json.encodeToString(Holder(nullable = null, nonNullable = 50))) | ||
assertEquals("""{"nullable":2,"nonNullable":2}""", Json.encodeToString(Holder(nullable = 0, nonNullable = 0))) | ||
assertEquals("""{"nullable":12,"nonNullable":52}""", Json.encodeToString(Holder(nullable = 10, nonNullable = 50))) | ||
|
||
assertEquals(Holder(nullable = 0, nonNullable = 50), Json.decodeFromString("""{"nullable":2,"nonNullable":52}""")) | ||
assertEquals(Holder(nullable = null, nonNullable = 50), Json.decodeFromString("""{"nullable":null,"nonNullable":52}""")) | ||
assertEquals(Holder(nullable = 10, nonNullable = 50), Json.decodeFromString("""{"nullable":12,"nonNullable":52}""")) | ||
} | ||
|
||
@Test | ||
fun testFileContextual() { | ||
val module = SerializersModule { | ||
contextual(FileContextualSerializer) | ||
} | ||
|
||
val json = Json { serializersModule = module } | ||
|
||
assertEquals("""{"nullable":null,"nonNullable":"foo"}""", json.encodeToString(FileContextualHolder(null, FileContextualType("foo")))) | ||
assertEquals("""{"nullable":"foo","nonNullable":"bar"}""", json.encodeToString(FileContextualHolder(FileContextualType("foo"), FileContextualType("bar")))) | ||
|
||
assertEquals(FileContextualHolder(null, FileContextualType("foo")), json.decodeFromString("""{"nullable":null,"nonNullable":"foo"}""")) | ||
assertEquals(FileContextualHolder(FileContextualType("foo"), FileContextualType("bar")), json.decodeFromString("""{"nullable":"foo","nonNullable":"bar"}""")) | ||
} | ||
|
||
@Test | ||
fun testContextual() { | ||
val module = SerializersModule { | ||
contextual(ContextualSerializer) | ||
} | ||
|
||
val json = Json { serializersModule = module } | ||
|
||
assertEquals("""{"nullable":null,"nonNullable":"foo"}""", json.encodeToString(ContextualHolder(null, ContextualType("foo")))) | ||
assertEquals("""{"nullable":"foo","nonNullable":"bar"}""", json.encodeToString(ContextualHolder(ContextualType("foo"), ContextualType("bar")))) | ||
|
||
assertEquals(ContextualHolder(null, ContextualType("foo")), json.decodeFromString("""{"nullable":null,"nonNullable":"foo"}""")) | ||
assertEquals(ContextualHolder(ContextualType("foo"), ContextualType("bar")), json.decodeFromString("""{"nullable":"foo","nonNullable":"bar"}""")) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
formats/json/commonTest/src/kotlinx/serialization/test/ContextualTest.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,47 @@ | ||
@file:UseContextualSerialization(ContextualTest.Cont::class) | ||
|
||
package kotlinx.serialization.test | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.UseContextualSerialization | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
|
||
class ContextualTest { | ||
data class Cont(val i: Int) | ||
|
||
@Serializable | ||
data class DateHolder(val cont: Cont?) | ||
|
||
object DateSerializer: KSerializer<Cont> { | ||
override fun deserialize(decoder: Decoder): Cont { | ||
return Cont(decoder.decodeInt()) | ||
} | ||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ContSerializer", PrimitiveKind.INT) | ||
|
||
override fun serialize(encoder: Encoder, value: Cont) { | ||
encoder.encodeInt(value.i) | ||
} | ||
|
||
} | ||
|
||
val module = SerializersModule { | ||
contextual(DateSerializer) | ||
} | ||
|
||
@kotlin.test.Test | ||
fun test() { | ||
val json = Json { serializersModule = module } | ||
|
||
println(json.encodeToString(DateHolder(Cont(42)))) | ||
} | ||
} |