summaryrefslogtreecommitdiff
path: root/runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt')
-rw-r--r--runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt44
1 files changed, 44 insertions, 0 deletions
diff --git a/runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt b/runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt
new file mode 100644
index 00000000..5e25f546
--- /dev/null
+++ b/runtime/commonTest/src/kotlinx/serialization/json/polymorphic/JsonListPolymorphismTest.kt
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+package kotlinx.serialization.json.polymorphic
+
+import kotlinx.serialization.*
+import kotlinx.serialization.json.*
+import kotlin.test.*
+
+class JsonListPolymorphismTest : JsonTestBase() {
+
+ @Serializable
+ internal data class ListWrapper(val list: List<@Polymorphic InnerBase>)
+
+ @Test
+ fun testPolymorphicValues() = parametrizedTest(
+ ListWrapper.serializer(),
+ ListWrapper(listOf(InnerImpl(1), InnerImpl2(2))),
+ "{list:[" +
+ "{type:kotlinx.serialization.json.polymorphic.InnerImpl,field:1,str:default,nullable:null}," +
+ "{type:kotlinx.serialization.json.polymorphic.InnerImpl2,field:2}]}",
+ polymorphicJson)
+
+ @Serializable
+ internal data class ListNullableWrapper(val list: List<@Polymorphic InnerBase?>)
+
+ @Test
+ fun testPolymorphicNullableValues() = parametrizedTest(
+ ListNullableWrapper.serializer(),
+ ListNullableWrapper(listOf(InnerImpl(1), null)),
+ "{list:[" +
+ "{type:kotlinx.serialization.json.polymorphic.InnerImpl,field:1,str:default,nullable:null}," +
+ "null]}",
+ polymorphicJson)
+
+ @Test
+ fun testPolymorphicNullableValuesWithNonNullSerializerFails() =
+ parametrizedTest { useStreaming ->
+ val wrapper = ListNullableWrapper(listOf(InnerImpl(1), null))
+ val serialized = polymorphicJson.stringify(ListNullableWrapper.serializer(), wrapper, useStreaming)
+ assertFails { polymorphicJson.parse(ListWrapper.serializer(), serialized, useStreaming) }
+ }
+}