https://kotlinlang.org logo
#serialization
Title
# serialization
p

pablisco

10/20/2018, 5:14 PM
Another kinda related question. Is there a way to get the name of the current element from the KInput? I guess this may only apply to JSON though.
s

sandwwraith

10/22/2018, 12:18 PM
you can get current index, and then map it using method of
KSerialClassDesc
. It is equal to property name by default.
p

pablisco

10/22/2018, 12:36 PM
But that's order dependent right?
s

sandwwraith

10/22/2018, 2:07 PM
Dependent on order inside class declaration in source file. JSON parser will determine current element even if they’re out-of-order in JSON and return correct index.
p

pablisco

10/22/2018, 3:58 PM
Since I've got you here 🙂 what's the best way to parse a field that depends on information from another?
s

sandwwraith

10/22/2018, 4:07 PM
You can try something like this https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/jvm/src/test/kotlin/kotlinx/serialization/formats/json/JsonTreeAndMapperTest.kt#L35 , or if you don’t want to be tied to JSON, just write a custom serializer and home that independent element would go first, so you can make a right decision when dependent index would be encountered
p

pablisco

10/22/2018, 5:04 PM
The Json input casting sounds like what I was thinking that I would need. What's the second option? I'm already doing a custom serialiser.
2 Views