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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("EqualsOrHashCode")
package kotlinx.serialization.features
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.test.*
import kotlin.test.*
@Serializable
abstract class AbstractSerializable {
public abstract val rootState: String // no backing field
val publicState: String = "A"
}
@Serializable
open class SerializableBase: AbstractSerializable() {
private val privateState: String = "B" // still should be serialized
@Transient
private val privateTransientState = "C" // not serialized: explicitly transient
val notAState: String // not serialized: no backing field
get() = "D"
override val rootState: String
get() = "E" // still not serializable
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is SerializableBase) return false
if (privateState != other.privateState) return false
if (privateTransientState != other.privateTransientState) return false
return true
}
}
@Serializable
class Derived(val derivedState: Int): SerializableBase() {
override val rootState: String = "foo" // serializable!
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Derived) return false
if (!super.equals(other)) return false
if (derivedState != other.derivedState) return false
if (rootState != other.rootState) return false
return true
}
}
@Serializable
open class Base1(open var state1: String) {
override fun toString(): String {
return "Base1(state1='$state1')"
}
}
@Serializable
class Derived2(@SerialName("state2") override var state1: String): Base1(state1) {
override fun toString(): String {
return "Derived2(state1='$state1')"
}
}
class InheritanceTest {
private val json = Json { encodeDefaults = true }
@Test
fun canBeSerializedAsDerived() {
val derived = Derived(42)
val msg = json.encodeToString(Derived.serializer(), derived)
assertEquals("""{"publicState":"A","privateState":"B","derivedState":42,"rootState":"foo"}""", msg)
val d2 = json.decodeFromString(Derived.serializer(), msg)
assertEquals(derived, d2)
}
@Test
fun canBeSerializedAsParent() {
val derived = Derived(42)
val msg = json.encodeToString(SerializableBase.serializer(), derived)
assertEquals("""{"publicState":"A","privateState":"B"}""", msg)
val d2 = json.decodeFromString(SerializableBase.serializer(), msg)
assertEquals(SerializableBase(), d2)
// no derivedState
assertFailsWithMissingField { json.decodeFromString(Derived.serializer(), msg) }
}
@Test
fun testWithOpenProperty() {
val d = Derived2("foo")
val msgFull = json.encodeToString(Derived2.serializer(), d)
assertEquals("""{"state1":"foo","state2":"foo"}""", msgFull)
assertEquals("""{"state1":"foo"}""", json.encodeToString(Base1.serializer(), d))
val restored = json.decodeFromString(Derived2.serializer(), msgFull)
val restored2 = json.decodeFromString(Derived2.serializer(), """{"state1":"bar","state2":"foo"}""") // state1 is ignored anyway
assertEquals("""Derived2(state1='foo')""", restored.toString())
assertEquals("""Derived2(state1='foo')""", restored2.toString())
}
}
|