How to deserialize a geometric polygon with Gson?

Asked

Viewed 162 times

1

I am using Vraptor 4 and the intention is to create a converting class of Polygon that does the deserialization and serializes. My problem is only in deserialize. How to deserialize a geometric polygon with Gson?

Follow the json 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
                    ]
                ]
            ]
        }
    }
  • would not be Json ?

  • @Otto No: https://code.google.com/p/google-gson/

  • @if possible, edit the question with the problem you are having in the deserialization.

  • You can let me do that, thank you

1 answer

3

First we define your pojo

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

The GSON has the method fromJson which reads a Json (string, buffer, jsonnode) to the desired object.

Perimeter perimeter = gson.fromJson(jsonReader, Perimeter.class);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.