How to deserialize a geometric polygon with Gson?

1

I'm using Vraptor 4 and the intent is to create a polygon convert class that does deserialization and which serialize. My problem is just deserializing. How to deserialize a geometric polygon with Gson?

Follow the json where I need to deserialize

{
   "perimeter": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -60.908203125,
                        -18.6328125
                    ],
                    [
                        -54.140625,
                        -14.677734375
                    ],
                    [
                        -51.6796875,
                        -23.291015625
                    ],
                    [
                        -58.88671875,
                        -22.1484375
                    ],
                    [
                        -60.908203125,
                        -18.6328125
                    ]
                ]
            ]
        }
    }
    
asked by anonymous 17.10.2014 / 15:44

1 answer

3

First we define your pojo

public class  Perimeter {
  String type;
  List<List<Float>> coordinates;
  /*
  ...
  constructor
  ...
  getters setters
  */
}

GSON has the fromJson that reads a Json (string, buffer, jsonnode) for the desired object.

Perimeter perimeter = gson.fromJson(jsonReader, Perimeter.class);
    
12.11.2014 / 16:36