summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt63
1 files changed, 63 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt
new file mode 100644
index 00000000..e2e10e24
--- /dev/null
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonPropertyPolymorphicTest.kt
@@ -0,0 +1,63 @@
+/*
+ * 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.PolymorphicSerializer
+import kotlinx.serialization.json.JsonTestBase
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+class JsonPropertyPolymorphicTest : JsonTestBase() {
+
+ @Test
+ fun testPolymorphicProperties() = assertJsonFormAndRestored(
+ InnerBox.serializer(),
+ InnerBox(InnerImpl(42, "foo")),
+ """{"base":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl",""" +
+ """"field":42,"str":"foo","nullable":null}}""",
+ polymorphicRelaxedJson)
+
+ @Test
+ fun testFlatPolymorphic() = parametrizedTest { jsonTestingMode ->
+ val base: InnerBase = InnerImpl(42, "foo")
+ val string = polymorphicRelaxedJson.encodeToString(PolymorphicSerializer(InnerBase::class), base, jsonTestingMode)
+ assertEquals("""{"type":"kotlinx.serialization.json.polymorphic.InnerImpl",""" +
+ """"field":42,"str":"foo","nullable":null}""", string)
+ assertEquals(base, polymorphicRelaxedJson.decodeFromString(PolymorphicSerializer(InnerBase::class), string, jsonTestingMode))
+ }
+
+ @Test
+ fun testNestedPolymorphicProperties() = assertJsonFormAndRestored(
+ OuterBox.serializer(),
+ OuterBox(OuterImpl(InnerImpl(42), InnerImpl2(42)), InnerImpl2(239)),
+ """{"outerBase":{""" +
+ """"type":"kotlinx.serialization.json.polymorphic.OuterImpl",""" +
+ """"base":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl","field":42,"str":"default","nullable":null},""" +
+ """"base2":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl2","field":42}},""" +
+ """"innerBase":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl2","field":239}}""",
+ polymorphicRelaxedJson)
+
+ @Test
+ fun testPolymorphicNullableProperties() = assertJsonFormAndRestored(
+ InnerNullableBox.serializer(),
+ InnerNullableBox(InnerImpl(42, "foo")),
+ """{"base":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl",""" +
+ """"field":42,"str":"foo","nullable":null}}""",
+ polymorphicRelaxedJson)
+
+ @Test
+ fun testPolymorphicNullablePropertiesWithNull() =
+ assertJsonFormAndRestored(InnerNullableBox.serializer(), InnerNullableBox(null), """{"base":null}""", polymorphicJson)
+
+ @Test
+ fun testNestedPolymorphicNullableProperties() = assertJsonFormAndRestored(
+ OuterNullableBox.serializer(),
+ OuterNullableBox(OuterNullableImpl(InnerImpl(42), null), InnerImpl2(239)),
+ """{"outerBase":{""" +
+ """"type":"kotlinx.serialization.json.polymorphic.OuterNullableImpl",""" +
+ """"base":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl","field":42,"str":"default","nullable":null},"base2":null},""" +
+ """"innerBase":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl2","field":239}}""",
+ polymorphicRelaxedJson)
+}