summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/features/sealed/SealedInterfacesJsonSerializationTest.kt
blob: a2e6bb67a6513be85315eeab0bd81b0d62fbb5a8 (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
/*
 * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.features.sealed

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.test.*
import kotlin.test.*

class SealedInterfacesJsonSerializationTest : JsonTestBase() {
    @Serializable
    sealed interface I

    @Serializable
    sealed class Response: I {
        @Serializable
        @SerialName("ResponseInt")
        data class ResponseInt(val i: Int): Response()

        @Serializable
        @SerialName("ResponseString")
        data class ResponseString(val s: String): Response()
    }

    @Serializable
    @SerialName("NoResponse")
    object NoResponse: I

    @Test
    fun testSealedInterfaceJson() {
        val messages = listOf(Response.ResponseInt(10), NoResponse, Response.ResponseString("foo"))
        assertJsonFormAndRestored(
            serializer(),
            messages,
            """[{"type":"ResponseInt","i":10},{"type":"NoResponse"},{"type":"ResponseString","s":"foo"}]"""
        )
    }
}