-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the support of custom serializers for simple cases
- Loading branch information
Showing
4 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
### Customizing ktoml serialization | ||
This page will be useful ONLY for those who plan to customize the serialization and deserialization logic of ktoml. | ||
|
||
### Custom Deserializer | ||
We suggest to use custom deserializer only for **very primitive cases**. | ||
In case you **really** need proper custom serializer, you will need to have something like this (this is a real generated code for the serialization): | ||
|
||
```kotlin | ||
override fun deserialize(decoder: Decoder): Color { | ||
val serialDescriptor = descriptor | ||
var bl = true | ||
var n = 0 | ||
var l = 0L | ||
// to read TOML AST properly - you need to run beginStructure first | ||
val compositeDecoder = decoder.beginStructure(serialDescriptor) | ||
block4@ while (bl) { | ||
val n2 = compositeDecoder.decodeElementIndex(serialDescriptor) | ||
when (n2) { | ||
// decoding done: | ||
-1 -> { | ||
bl = false | ||
continue@block4 | ||
} | ||
// element index | ||
0 -> { | ||
l = compositeDecoder.decodeLongElement(serialDescriptor, 0) | ||
n = n or 1 | ||
continue@block4 | ||
} | ||
} | ||
throw IllegalArgumentException() | ||
} | ||
compositeDecoder.endStructure(serialDescriptor) | ||
return Color(l) | ||
} | ||
``` | ||
|
||
for the following data class: | ||
|
||
```kotlin | ||
@Serializable(with = ColorAsStringSerializer::class) | ||
data class Color(val rgb: Long) | ||
``` | ||
|
||
### Simple cases for Deserialization | ||
Imaging you have your class Color: | ||
```kotlin | ||
@Serializable(with = ColorAsStringSerializer::class) | ||
data class Color(val rgb: Long) | ||
``` | ||
|
||
We have several small workarounds, that would let you override deserializers for your class and using ktoml-native parser and AST: | ||
|
||
```kotlin | ||
object ColorAsStringSerializer : KSerializer<Color> { | ||
override fun deserialize(decoder: Decoder): Color { | ||
// please note, that the decoder should match with the real type of a variable: | ||
// for string in the input - decodeString, for long - decodeLong, etc. | ||
val string = decoder.decodeString() | ||
return Color(string.toLong() + 15) | ||
} | ||
} | ||
``` | ||
|
||
```kotlin | ||
Toml.decodeFromString<Color>( | ||
""" | ||
rgb = "0" | ||
""".trimIndent() | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 55 additions & 14 deletions
69
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/CustomSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/SurrogateSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlin.test.Ignore | ||
import kotlin.test.Test | ||
|
||
class SurrogateTest { | ||
object ColorSerializer : KSerializer<Color> { | ||
override val descriptor: SerialDescriptor = ColorSurrogate.serializer().descriptor | ||
|
||
override fun serialize(encoder: Encoder, value: Color) { | ||
val surrogate = ColorSurrogate((value.rgb shr 16) and 0xff, (value.rgb shr 8) and 0xff, value.rgb and 0xff) | ||
encoder.encodeSerializableValue(ColorSurrogate.serializer(), surrogate) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Color { | ||
val surrogate = decoder.decodeSerializableValue(ColorSurrogate.serializer()) | ||
return Color((surrogate.r shl 16) or (surrogate.g shl 8) or surrogate.b) | ||
} | ||
} | ||
|
||
@Serializable | ||
@SerialName("Color") | ||
private class ColorSurrogate(val r: Int, val g: Int, val b: Int) { | ||
init { | ||
require(r in 0..255 && g in 0..255 && b in 0..255) | ||
} | ||
} | ||
|
||
@Serializable(with = ColorSerializer::class) | ||
class Color(val rgb: Int) | ||
|
||
@Test | ||
@Ignore | ||
fun testDecodingWithCustomSerializer() { | ||
println(Toml.decodeFromString<Color>( | ||
""" | ||
r = 5 | ||
g = 6 | ||
b = 7 | ||
""".trimIndent() | ||
)) | ||
} | ||
} |