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

gaetan

02/05/2018, 12:05 PM
I’m trying to find a way of dealing with GeoJson (https://tools.ietf.org/html/rfc7946) in a multiplatform way. The specification indicates that the type of the property
coordinates
depends on the first property
type
. Expamples:
Copy code
{
  "type": "Point",
  "coordinates": [
    100,
    0
  ]
}
Copy code
{
  "type": "LineString",
  "coordinates": [
    [
      100,
      0
    ],
    [
      101,
      1
    ]
  ]
}
s

sandwwraith

02/05/2018, 10:31 PM
Unfortunately, current implementation can’t make decisions how to deserialize object depending on a field inside the object
g

gaetan

02/06/2018, 8:47 AM
Thanks for the answer.
g

gildor

02/08/2018, 12:28 AM
Oh, this format is really bad :(
Maybe somehow possible to deserialize to array of dynamics, or to your own class with custom deserializer (kotlinx.serialization support that, as I understand), because I just do not understand how you can map such structure to class, maybe only to some generic Json structure
I mean this problem is not specific for Kotlin serialization, but for any mapping
g

gaetan

02/08/2018, 8:32 AM
I agree with you about the format but it’s a specification so it should be a use case taked in account. It’s possible with jackson: https://github.com/opendatalab-de/geojson-jackson.
g

gildor

02/08/2018, 8:35 AM
Using JsonSubTypes?
It’s still custom parsing, but with help of Jackson
my point that you probably should to write custom adapter for kotlinx.serialization, that handled this case.
g

gaetan

02/08/2018, 8:38 AM
I don’t know if kotlinx.serialization is still a help for the moment. I think i have to do all the work by custom code and then copy data into some data class.
g

gildor

02/08/2018, 8:38 AM
Not sure that this is possible for abstract classes how for Jackson (probably make sense to ask about such feature), but at least you can have custom non-abstract wrapper above GeoJsonObject
Yes, but you can write custom serializer, so you can copy data to any data class https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md
g

gaetan

02/08/2018, 8:44 AM
Maybe I should do it in two times: a first object that is used only for the
type
field. Then, when I know the type, I can use the right specialized class.
g

gildor

02/08/2018, 9:01 AM
Not sure, but I think using custom serilizers you can even read type and then deserialize only “coordinates” content separately
9 Views