| // This file was automatically generated from formats.md by Knit tool. Do not edit. |
| package example.exampleFormats10 |
| |
| import kotlinx.serialization.* |
| import kotlinx.serialization.descriptors.* |
| import kotlinx.serialization.encoding.* |
| import kotlinx.serialization.modules.* |
| |
| class ListEncoder : AbstractEncoder() { |
| val list = mutableListOf<Any>() |
| |
| override val serializersModule: SerializersModule = EmptySerializersModule() |
| |
| override fun encodeValue(value: Any) { |
| list.add(value) |
| } |
| } |
| |
| fun <T> encodeToList(serializer: SerializationStrategy<T>, value: T): List<Any> { |
| val encoder = ListEncoder() |
| encoder.encodeSerializableValue(serializer, value) |
| return encoder.list |
| } |
| |
| inline fun <reified T> encodeToList(value: T) = encodeToList(serializer(), value) |
| |
| @Serializable |
| data class Project(val name: String, val owner: User, val votes: Int) |
| |
| @Serializable |
| data class User(val name: String) |
| |
| fun main() { |
| val data = Project("kotlinx.serialization", User("kotlin"), 9000) |
| println(encodeToList(data)) |
| } |