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

k_marussy

10/28/2018, 12:08 PM
Is there any way for Jackson-style
As.PROPERTY
polymoprhic deserialization (e.g.,
{"type": "some.class.Name", "property": "value"}
instead of
["some.class.Name", {"property": "value"}]
) with kotlinx.serialization? I actually have some even more polymorphic gnarly (de)serailization requirements, which I could handle by some pretty ugly custom Jackson (de)serializers, but I would be interested a JVM-idependent solution. A key idea seems to be some kind of lookahead, during which Jackson can put tokens into a
TokenBuffer
while searching for the
"type"
attribute, and prefix the remaining token stream with the buffer contents upon calling the concrete deserializer. As the JSON schema I have to handle clearly wasn't designed with ease of parsing in mind, I have to rely a lot on lookahead. I am unsure how to achieve something similar with
KInput
and
KSerialClassDesc
.
1
s

sandwwraith

10/29/2018, 9:26 AM
Unfortunately there is no general mechanism for this for now (because in some formats lookahead is not possible), so this should be implemented directly in JSON. For now, you can create a custom serializer which will cast KInput to particular JsonInput and read values as json tree to map it later (like here: https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/jvm/src/test/kotlin/kotlinx/serialization/formats/json/JsonTreeAndMapperTest.kt#L35)
👍 2
k

k_marussy

10/29/2018, 10:20 AM
Thanks for the reply! That's unfortunate, but completely understandable. Guess I will end up sticking to Jackson in the immediate future.
t

tarek

10/29/2018, 7:19 PM
@k_marussy I did two versions of this use case for demonstration purposes. One straightforward one and a more generic one. You can have a look at them here : https://gist.github.com/tokou/1660906bd103b0a68f2da283a9c6522c
👍 1
k

k_marussy

10/29/2018, 7:33 PM
Thanks! I think now I understand. Your example looks great -- my use case is a bit more complicated, but having access to a tree that I am still able to pass to generated parsers will help a lot.
Unfortunately, this approach is not recursive, i.e., I cannot put a polymorphic object within another polymorphic object: while
JSON
uses
JSON.JsonInput
, inside
JsonTreeMapper#readTree
, you get passed
JsonTreeMapper.JsonTreeInput
, which is private, and thus there is not way to extract the current
JsonElement
😞
t

tarek

10/29/2018, 10:04 PM
Right I didn't think about that...
k

k_marussy

10/29/2018, 10:36 PM
When I'll have a bit of time, I think I'll try to patch
JsonTreeMapper
and try out recursive polymorphic serialization that way.
s

sandwwraith

10/30/2018, 9:44 AM
Maybe this is just a lack of design, if you have feeling that `JsonTreeMapper.JsonTreeInput`should be public, feel free to submit a PR with fix and example.
k

k_marussy

10/30/2018, 2:54 PM
In general, just making it public is insufficient. I am thinking more along the lines of something like this: https://github.com/Kotlin/kotlinx.serialization/pull/254
6 Views