-
-
Save PaulWoitaschek/c650466b8bbc0c44797b6b10951918a5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package sandbox.serializationtest | |
import kotlinx.serialization.KSerializer | |
import kotlinx.serialization.Serializable | |
import kotlinx.serialization.json.Json | |
@Serializable(with = XYSerializer::class) | |
interface XY { | |
val x: Int | |
val y: Int | |
} | |
object XYSerializer : KSerializer<XY> by Point.serializer() as KSerializer<XY> | |
@Serializable | |
data class Point( | |
override val x: Int, | |
override val y: Int | |
) : XY | |
@Serializable | |
data class Car( | |
val position: XY | |
) | |
fun main() { | |
val originalCar = Car(Point(42, 24)) | |
val carJson = Json.encodeToString(Car.serializer(), originalCar) | |
val decodedCar = Json.decodeFromString(Car.serializer(), carJson) | |
check(originalCar == decodedCar) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment