Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties Format: Support wrapped sealed classes #2255

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public sealed class Properties(
if (serializer is AbstractPolymorphicSerializer<*>) {
val casted = serializer as AbstractPolymorphicSerializer<Any>
val actualSerializer = casted.findPolymorphicSerializer(this, value as Any)
encodeTaggedString(nested("type"), actualSerializer.descriptor.serialName)

return actualSerializer.serialize(this, value)
}
Expand Down Expand Up @@ -102,9 +103,8 @@ public sealed class Properties(
}

final override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
val type = map["type"]?.toString()

if (deserializer is AbstractPolymorphicSerializer<*>) {
val type = map[nested("type")]?.toString()
val actualSerializer: DeserializationStrategy<Any> = deserializer.findPolymorphicSerializer(this, type)

@Suppress("UNCHECKED_CAST")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kotlinx.serialization.properties

import kotlin.reflect.KProperty1
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.test.Test
Expand All @@ -21,6 +22,9 @@ class SealedClassSerializationFromPropertiesTest {
@Serializable
data class SecondChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass()

@Serializable
data class CompositeClass(val item: BaseClass)

@Test
fun testPropertiesDeserialization() {
val props = mapOf(
Expand All @@ -44,7 +48,71 @@ class SealedClassSerializationFromPropertiesTest {

val instanceProperties = Properties.encodeToMap(instance)

assertEquals("FIRSTCHILD", instanceProperties["type"])
assertEquals(1L, instanceProperties["firstProperty"])
assertEquals("one", instanceProperties["secondProperty"])
}

@Test
fun testWrappedPropertiesDeserialization() {
val props = mapOf(
"0.type" to "FIRSTCHILD",
"0.firstProperty" to 1L,
"0.secondProperty" to "one",
"1.type" to "SECONDCHILD",
"1.firstProperty" to 2L,
"1.secondProperty" to "two"
)

val instances: List<BaseClass> = Properties.decodeFromMap(props)

val expected = listOf(FirstChild(1, "one"), SecondChild(2, "two"))
assertEquals(expected, instances)
}

@Test
fun testWrappedPropertiesSerialization() {
val instances: List<BaseClass> = listOf(
FirstChild(firstProperty = 1L, secondProperty = "one"),
SecondChild(firstProperty = 2L, secondProperty = "two")
)

val instanceProperties = Properties.encodeToMap(instances)

assertEquals("FIRSTCHILD", instanceProperties["0.type"])
assertEquals(1L, instanceProperties["0.firstProperty"])
assertEquals("one", instanceProperties["0.secondProperty"])
assertEquals("SECONDCHILD", instanceProperties["1.type"])
assertEquals(2L, instanceProperties["1.firstProperty"])
assertEquals("two", instanceProperties["1.secondProperty"])
}

@Test
fun testCompositeClassPropertiesDeserialization() {
val props = mapOf(
"item.type" to "SECONDCHILD",
"item.firstProperty" to 7L,
"item.secondProperty" to "nothing"
)

val composite: CompositeClass = Properties.decodeFromMap(props)

assertEquals(CompositeClass(SecondChild(7L, "nothing")), composite)
}

@Test
fun testCompositeClassPropertiesSerialization() {
val composite = CompositeClass(
item = FirstChild(
firstProperty = 5L,
secondProperty = "something"
)
)

val compositeProperties = Properties.encodeToMap(composite)

assertEquals("FIRSTCHILD", compositeProperties["item.type"])
assertEquals(5L, compositeProperties["item.firstProperty"])
assertEquals("something", compositeProperties["item.secondProperty"])
}
}