-
Notifications
You must be signed in to change notification settings - Fork 624
/
example-formats-11.kt
36 lines (27 loc) · 1.01 KB
/
example-formats-11.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// This file was automatically generated from formats.md by Knit tool. Do not edit.
package example.exampleFormats11
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))
}