I have a question on custom deserializer. If I am ...
# serialization
v
I have a question on custom deserializer. If I am expecting either a String or an object when deserializing input, is the following code the way to go:
Copy code
fun load(input: KInput): MyObject {
    val maybeString = input.readNullable(StringSerializer)
    return if (maybeString != null) {
        //do something with string that results in creating of MyObject
    } else {
        input.read<MyObject>()
    }
}
s
not really. your first line will either consume string or null token. there’s no “look-ahead” api in kinput, because some formats need to know which object they are trying to parse. If your input is JSON, you can use intermediate json representation to get what you want: https://github.com/Kotlin/kotlinx.serialization/blob/a9170c305904b10841d2fbe8e3d6b27418e5e6bb/runtime/jvm/src/test/kotlin/kotlinx/serialization/formats/json/JsonTreeAndMapperTest.kt#L35
👏 1
v
thank you!