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

Add the support for custom serialization #127

Open
orchestr7 opened this issue Mar 16, 2022 · 0 comments
Open

Add the support for custom serialization #127

orchestr7 opened this issue Mar 16, 2022 · 0 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@orchestr7
Copy link
Owner

class CustomSerializerTest {
    object ColorAsStringSerializer : KSerializer<Color> {
        override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Color", PrimitiveKind.STRING)

        override fun serialize(encoder: Encoder, value: Color) {
            TODO()
        }

        override fun deserialize(decoder: Decoder): Color {
            val value = decoder.decodeString()
            return Color(value.toLong())
        }
    }

    @Serializable(with = ColorAsStringSerializer::class)
    data class Color(val rgb: Long)

    @Serializable
    data class Settings(val background: Color, val foreground: Color)

    @Test
    fun testDecodingWithCustomSerializer() {
        println(Toml.decodeFromString<Settings>(
            """
                [background]
                    rgb = 0
                [foreground]
                    rgb = 0
            """.trimIndent()
        ))
    }
}

will cause

This kind of node should not be processed in TomlDecoder.decodeValue(): [background]
com.akuleshov7.ktoml.exceptions.InternalDecodingException: This kind of node should not be processed in TomlDecoder.decodeValue(): [background]
	at app//com.akuleshov7.ktoml.decoders.TomlMainDecoder.decodeKeyValue$ktoml_core(TomlMainDecoder.kt:84)
	at app//com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeString(TomlAbstractDecoder.kt:101)
	at app//com.akuleshov7.ktoml.decoders.CustomSerializerTest$ColorAsStringSerializer.deserialize(CustomSerializerTest.kt:23)
	at app//com.akuleshov7.ktoml.decoders.CustomSerializerTest$ColorAsStringSerializer.deserialize(CustomSerializerTest.kt:15)
	at app//kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:260)
	at app//kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16)
@orchestr7 orchestr7 self-assigned this Mar 16, 2022
@orchestr7 orchestr7 added bug Something isn't working help wanted Extra attention is needed labels Mar 16, 2022
@orchestr7 orchestr7 added this to the 0.2.12 milestone Mar 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant