-
Notifications
You must be signed in to change notification settings - Fork 25
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
Adding tests that show errors in deserialization #125
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d180533
Adding tests that show errors in deserialization
orchestr7 5f053b0
Merge branch 'main' of https://github.com/akuleshov7/ktoml into bugfi…
orchestr7 0d1b848
Adding the support of custom serializers for simple cases
orchestr7 f3f235a
Merge branch 'main' into bugfix/invalid-traversal
orchestr7 4bb2a9f
Adding the support of custom serializers for simple cases
orchestr7 9a72a31
Merge branch 'bugfix/invalid-traversal' of https://github.com/akulesh…
orchestr7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
86 changes: 86 additions & 0 deletions
86
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlin.test.Ignore | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CustomSerializerTest { | ||
object SinglePropertyAsStringSerializer : KSerializer<SingleProperty> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("SingleProperty", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: SingleProperty) { | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): SingleProperty { | ||
val string = decoder.decodeString() | ||
return SingleProperty(string.toLong() + 15) | ||
} | ||
} | ||
|
||
@Serializable(with = SinglePropertyAsStringSerializer::class) | ||
data class SingleProperty(val rgb: Long) | ||
|
||
@Test | ||
fun testDecodingWithCustomSerializerSingleProperty() { | ||
assertEquals( | ||
SingleProperty(15), | ||
Toml.decodeFromString( | ||
""" | ||
rgb = "0" | ||
""".trimIndent() | ||
) | ||
) | ||
} | ||
|
||
@Serializable(with = SeveralPropertiesAsStringSerializer::class) | ||
data class SeveralProperties(val rgb: Long, val brg: Long) | ||
|
||
object SeveralPropertiesAsStringSerializer : KSerializer<SeveralProperties> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Color", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: SeveralProperties) { | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): SeveralProperties { | ||
val string = decoder.decodeString() | ||
return SeveralProperties(string.toLong() + 15, string.toLong()) | ||
} | ||
} | ||
|
||
@Test | ||
@Ignore | ||
fun testDecodingWithCustomSerializerSeveralProperties() { | ||
assertEquals( | ||
SeveralProperties(15, 1), | ||
Toml.decodeFromString( | ||
""" | ||
rgb = "0" | ||
brg = "1" | ||
""".trimIndent() | ||
) | ||
) | ||
} | ||
|
||
@Serializable | ||
data class Settings(val background: SingleProperty, val foreground: SingleProperty) | ||
|
||
@Test | ||
@Ignore | ||
fun testDecodingWithCustomSerializer() { | ||
println(Toml.decodeFromString<Settings>( | ||
""" | ||
[background] | ||
rgb = "0" | ||
[foreground] | ||
rgb = "0" | ||
""".trimIndent() | ||
)) | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the example from https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#composite-serializer-via-surrogate Also not working |
||
fun testDecodingWithCustomSerializer() { | ||
println(Toml.decodeFromString<Color>( | ||
""" | ||
r = 5 | ||
g = 6 | ||
b = 7 | ||
""".trimIndent() | ||
)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test does not work, but I actually would have expected it be working