summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/json/polymorphic/JsonTreeDecoderPolymorphicTest.kt
blob: 9d8e861df5da3b5e9c38d600fa35b94c684fb960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * Copyright 2017-2023 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.Serializable
import kotlinx.serialization.json.*
import kotlin.test.*

class JsonTreeDecoderPolymorphicTest : JsonTestBase() {

    @Serializable
    sealed class Sealed

    @Serializable
    data class ClassContainingItself(
        val a: String,
        val b: String,
        val c: ClassContainingItself? = null,
        val d: String?
    ) : Sealed()

    val inner = ClassContainingItself(
        "InnerA",
        "InnerB",
        null,
        "InnerC"
    )
    val outer = ClassContainingItself(
        "OuterA",
        "OuterB",
        inner,
        "OuterC"
    )

    @Test
    fun testDecodingWhenClassContainsItself() = parametrizedTest { jsonTestingMode ->
        val encoded = default.encodeToString(outer as Sealed, jsonTestingMode)
        val decoded: Sealed = Json.decodeFromString(encoded, jsonTestingMode)
        assertEquals(outer, decoded)
    }
}