Skip to content

Commit 40eb277

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev
2 parents 9157bf8 + cfce5d8 commit 40eb277

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
66
[![TeamCity build](https://img.shields.io/teamcity/http/teamcity.jetbrains.com/s/KotlinTools_KotlinxSerialization_Ko.svg)](https://teamcity.jetbrains.com/viewType.html?buildTypeId=KotlinTools_KotlinxSerialization_Ko&guest=1)
77
[![Kotlin](https://img.shields.io/badge/kotlin-1.8.10-blue.svg?logo=kotlin)](http://kotlinlang.org)
8-
[![Maven Central](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-serialization-core/1.5.0)](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-serialization-core/1.5.0/pom)
8+
[![Maven Central](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-serialization-core/1.5.0)](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-core/1.5.0)
99
[![KDoc link](https://img.shields.io/badge/API_reference-KDoc-blue)](https://kotlinlang.org/api/kotlinx.serialization/)
1010
[![Slack channel](https://img.shields.io/badge/chat-slack-blue.svg?logo=slack)](https://kotlinlang.slack.com/messages/serialization/)
1111

docs/serializers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ fun main() {
814814
### Specifying serializer globally using typealias
815815

816816
kotlinx.serialization tends to be the always-explicit framework when it comes to serialization strategies: normally,
817-
they should be explicitly mentioned in `@Serialiazble` annotation. Therefore, we do not provide any kind of global serializer
817+
they should be explicitly mentioned in `@Serializable` annotation. Therefore, we do not provide any kind of global serializer
818818
configuration (except for [context serializer](#contextual-serialization) mentioned later).
819819

820820
However, in projects with a large number of files and classes, it may be too cumbersome to specify `@file:UseSerializers`

formats/properties/commonMain/src/kotlinx/serialization/properties/Properties.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public sealed class Properties(
5858
if (serializer is AbstractPolymorphicSerializer<*>) {
5959
val casted = serializer as AbstractPolymorphicSerializer<Any>
6060
val actualSerializer = casted.findPolymorphicSerializer(this, value as Any)
61+
encodeTaggedString(nested("type"), actualSerializer.descriptor.serialName)
6162

6263
return actualSerializer.serialize(this, value)
6364
}
@@ -102,9 +103,8 @@ public sealed class Properties(
102103
}
103104

104105
final override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
105-
val type = map["type"]?.toString()
106-
107106
if (deserializer is AbstractPolymorphicSerializer<*>) {
107+
val type = map[nested("type")]?.toString()
108108
val actualSerializer: DeserializationStrategy<Any> = deserializer.findPolymorphicSerializer(this, type)
109109

110110
@Suppress("UNCHECKED_CAST")

formats/properties/commonTest/src/kotlinx/serialization/properties/SealedClassSerializationFromPropertiesTest.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package kotlinx.serialization.properties
22

3+
import kotlin.reflect.KProperty1
34
import kotlinx.serialization.SerialName
45
import kotlinx.serialization.Serializable
56
import kotlin.test.Test
@@ -21,6 +22,9 @@ class SealedClassSerializationFromPropertiesTest {
2122
@Serializable
2223
data class SecondChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass()
2324

25+
@Serializable
26+
data class CompositeClass(val item: BaseClass)
27+
2428
@Test
2529
fun testPropertiesDeserialization() {
2630
val props = mapOf(
@@ -44,7 +48,71 @@ class SealedClassSerializationFromPropertiesTest {
4448

4549
val instanceProperties = Properties.encodeToMap(instance)
4650

51+
assertEquals("FIRSTCHILD", instanceProperties["type"])
4752
assertEquals(1L, instanceProperties["firstProperty"])
4853
assertEquals("one", instanceProperties["secondProperty"])
4954
}
55+
56+
@Test
57+
fun testWrappedPropertiesDeserialization() {
58+
val props = mapOf(
59+
"0.type" to "FIRSTCHILD",
60+
"0.firstProperty" to 1L,
61+
"0.secondProperty" to "one",
62+
"1.type" to "SECONDCHILD",
63+
"1.firstProperty" to 2L,
64+
"1.secondProperty" to "two"
65+
)
66+
67+
val instances: List<BaseClass> = Properties.decodeFromMap(props)
68+
69+
val expected = listOf(FirstChild(1, "one"), SecondChild(2, "two"))
70+
assertEquals(expected, instances)
71+
}
72+
73+
@Test
74+
fun testWrappedPropertiesSerialization() {
75+
val instances: List<BaseClass> = listOf(
76+
FirstChild(firstProperty = 1L, secondProperty = "one"),
77+
SecondChild(firstProperty = 2L, secondProperty = "two")
78+
)
79+
80+
val instanceProperties = Properties.encodeToMap(instances)
81+
82+
assertEquals("FIRSTCHILD", instanceProperties["0.type"])
83+
assertEquals(1L, instanceProperties["0.firstProperty"])
84+
assertEquals("one", instanceProperties["0.secondProperty"])
85+
assertEquals("SECONDCHILD", instanceProperties["1.type"])
86+
assertEquals(2L, instanceProperties["1.firstProperty"])
87+
assertEquals("two", instanceProperties["1.secondProperty"])
88+
}
89+
90+
@Test
91+
fun testCompositeClassPropertiesDeserialization() {
92+
val props = mapOf(
93+
"item.type" to "SECONDCHILD",
94+
"item.firstProperty" to 7L,
95+
"item.secondProperty" to "nothing"
96+
)
97+
98+
val composite: CompositeClass = Properties.decodeFromMap(props)
99+
100+
assertEquals(CompositeClass(SecondChild(7L, "nothing")), composite)
101+
}
102+
103+
@Test
104+
fun testCompositeClassPropertiesSerialization() {
105+
val composite = CompositeClass(
106+
item = FirstChild(
107+
firstProperty = 5L,
108+
secondProperty = "something"
109+
)
110+
)
111+
112+
val compositeProperties = Properties.encodeToMap(composite)
113+
114+
assertEquals("FIRSTCHILD", compositeProperties["item.type"])
115+
assertEquals(5L, compositeProperties["item.firstProperty"])
116+
assertEquals("something", compositeProperties["item.secondProperty"])
117+
}
50118
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ kotlin.version.snapshot=1.9.255-SNAPSHOT
1313

1414
junit_version=4.12
1515
jackson_version=2.10.0.pr1
16-
dokka_version=1.7.0
16+
dokka_version=1.8.10
1717
native.deploy=
1818
validator_version=0.11.0
1919
knit_version=0.4.0

0 commit comments

Comments
 (0)