All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.json.polymorphic.JsonMapPolymorphismTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * Copyright 2017-2020 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 kotlinx.serialization.modules.*
import kotlin.test.*

class JsonMapPolymorphismTest : JsonTestBase() {

    @Serializable
    internal data class MapWrapper(val map: Map)

    @Test
    fun testPolymorphicValues() = assertJsonFormAndRestored(
        MapWrapper.serializer(),
        MapWrapper(mapOf("k1" to InnerImpl(1), "k2" to InnerImpl2(2))),
        """{"map":{"k1":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl","field":1,"str":"default","nullable":null},"k2":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl2","field":2}}}""".trimMargin(),
        polymorphicJson
    )

    @Serializable
    internal data class MapNullableWrapper(val map: Map)

    @Serializable
    internal data class MapKeys(val map: Map<@Polymorphic InnerBase, String>)

    @Test
    fun testPolymorphicNullableValues() = assertJsonFormAndRestored(
        MapNullableWrapper.serializer(),
        MapNullableWrapper(mapOf("k1" to InnerImpl(1), "k2" to null)),
        """{"map":{"k1":{"type":"kotlinx.serialization.json.polymorphic.InnerImpl","field":1,"str":"default","nullable":null},"k2":null}}""",
        polymorphicJson
    )

    @Test
    fun testPolymorphicKeys() {
        val json = Json {
            allowStructuredMapKeys = true
            serializersModule = polymorphicTestModule
            encodeDefaults = true
        }
        assertJsonFormAndRestored(
            MapKeys.serializer(),
            MapKeys(mapOf(InnerImpl(1) to "k2", InnerImpl2(2) to "k2")),
            """{"map":[{"type":"kotlinx.serialization.json.polymorphic.InnerImpl","field":1,"str":"default","nullable":null},"k2",{"type":"kotlinx.serialization.json.polymorphic.InnerImpl2","field":2},"k2"]}""",
            json
        )
    }

    @Test
    fun testPolymorphicKeysInArray() {
        val json = Json {
            allowStructuredMapKeys = true
            useArrayPolymorphism = true
            serializersModule = polymorphicTestModule
            encodeDefaults = true
        }
        assertJsonFormAndRestored(
            MapKeys.serializer(),
            MapKeys(mapOf(InnerImpl(1) to "k2", InnerImpl2(2) to "k2")),
            """{"map":[["kotlinx.serialization.json.polymorphic.InnerImpl",{"field":1,"str":"default","nullable":null}],"k2",["kotlinx.serialization.json.polymorphic.InnerImpl2",{"field":2}],"k2"]}""",
            json
        )
    }

    @Serializable
    abstract class Base

    @Serializable
    data class Derived(val myMap: Map) : Base()

    @Test
    fun testIssue480() {
        val json = Json {
            allowStructuredMapKeys = true
            serializersModule = SerializersModule {
                polymorphic(Base::class) {
                    subclass(Derived.serializer())
                }
            }
        }

        assertJsonFormAndRestored(
            Base.serializer(),
            Derived(mapOf(StringData("hi") to "hello")),
            """{"type":"kotlinx.serialization.json.polymorphic.JsonMapPolymorphismTest.Derived","myMap":[{"data":"hi"},"hello"]}""",
            json
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy