I’m trying to find a way of dealing with GeoJson (...
# serialization
g
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
Unfortunately, current implementation can’t make decisions how to deserialize object depending on a field inside the object
g
Thanks for the answer.
g
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
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
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
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
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
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
Not sure, but I think using custom serilizers you can even read type and then deserialize only “coordinates” content separately