Search code examples
androidjsonkotlingeojsonmoshi

Moshi - Parsing JSON with dynamic parameters


I have this part of my JSON:

{
    "object": [
        {
            "id": "blablabla",
            "name": "object1",
            "type": "GeometryCollection",
            "distancePoints": {
                "type": "FeatureCollection",
                "form": "distancePoint",
                "features": [
                    {
                        "type": "Feature",
                        "form": "feature",
                        "geometry": {
                            "coordinates": [
                                9.999999999999999,
                                0.000000000000000
                            ],
                            "type": "Point"
                        }
                    },
                    {
                        "type": "Feature",
                        "form": "feature",
                        "geometry": {
                            "coordinates": [
                                [
                                    2.222222222222222,
                                    4.444444444444444
                                ],
                                [
                                    3.333333333333333,
                                    5.555555555555555
                                ]
                            ],
                            "type": "LineString"
                        }
                    },
                    {
                        "type": "Feature",
                        "form": "feature",
                        "geometry": {
                            "coordinates": [
                                [
                                    7.777777777777777,
                                    6.666666666666666
                                ],
                                [
                                    7.777777777777777,
                                    6.666666666666666
                                ],
                                [
                                    7.777777777777777,
                                    6.666666666666666
                                ]
                            ],
                            "type": "Polygon"
                        }
                    }
                ]
            },
            "overlays": {
                "type": "FeatureCollection",
                "form": "overlay",
                "features": [
                    {
                        "type": "Feature",
                        "form": "feature",
                        "geometry": {
                            "coordinates": [
                                [
                                    9.999999999999999,
                                    0.000000000000000
                                ],
                                [
                                    3.333333333333333,
                                    5.555555555555555
                                ]
                            ],
                            "type": "LineString"
                        }
                    }
                ]
            }
        }
    ]
}

I need to parse this using Kotlin, but I don't know where to start.

The key infos here are: The model classes have to be constructed based on the type field.

For it to be dynamic I want to create a GeoJSONType data class which would be the type of distancePoints and overlays. E.g.:

data class Object1(
val distancePoints: GeoJSONType,
val overlays: GeoJSONType)

data class GeoJSONType(parameters here)

The objects that would be created from GeoJSONType would be simple data classes using the values of "coordinates". E.g.:

data class Point()
data class LineString()

etc.

What would be the correct way to go about solving this.

Thanks in advance.


Solution

  • Well, after researching for quite a long time I solved this problem using scruffyfox's library: https://github.com/scruffyfox/Android-GeoGson

    He has already created all models and adapters specifically for this use-case with geojson and moshi so I just copied them, modified them to be usable for my custom use-case and now it works perfectly.

    The library follos the strict rules of GeoJson format and every parameter that is not part of the geojson format is put in a "foreign" HashMap<String, Any>.

    Since I already now what will be in my foreign hashmap I just take them and use them wherever I need them.