summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt62
1 files changed, 62 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt
new file mode 100644
index 00000000..c3003ca9
--- /dev/null
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/GenericSerializersOnFileTest.kt
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+@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)
+ }
+
+ @Suppress("UNCHECKED_CAST")
+ 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"}""")
+ )
+ }
+
+
+}