| // This file was automatically generated from serializers.md by Knit tool. Do not edit. |
| package example.exampleSerializer17 |
| |
| import kotlinx.serialization.* |
| import kotlinx.serialization.json.* |
| import kotlinx.serialization.encoding.* |
| import kotlinx.serialization.descriptors.* |
| |
| @Serializable(with = BoxSerializer::class) |
| data class Box<T>(val contents: T) |
| |
| class BoxSerializer<T>(private val dataSerializer: KSerializer<T>) : KSerializer<Box<T>> { |
| override val descriptor: SerialDescriptor = dataSerializer.descriptor |
| override fun serialize(encoder: Encoder, value: Box<T>) = dataSerializer.serialize(encoder, value.contents) |
| override fun deserialize(decoder: Decoder) = Box(dataSerializer.deserialize(decoder)) |
| } |
| |
| @Serializable |
| data class Project(val name: String) |
| |
| fun main() { |
| val box = Box(Project("kotlinx.serialization")) |
| val string = Json.encodeToString(box) |
| println(string) |
| println(Json.decodeFromString<Box<Project>>(string)) |
| } |