3

First of all I've seen this question, but I did not see the full answer to my question and this question was asked 2 years ago.

Introduction:

For example we have an JSON with such structure:

{
    "name": "some_name",
    "description": "some_description",
    "price": 123,
    "location": {
        "latitude": 456987,
        "longitude": 963258
    }
}

I can use GSON library for auto parsing this JSON to my object's class.

For this I must create class describing JSON structure, like below:

public class CustomClassDescribingJSON {

    private String name;
    private String description;
    private double price;
    private Location location;

    // Some getters and setters and other methods, fields, etc

    public class Location {
        private long latitude;
        private long longitude;

    }

}

And next I can auto parse JSON to object:

String json; // This object was obtained earlier.
CustomClassDescribingJSON object = new Gson().fromJson(json, CustomClassDescribingJSON.class);

I have a few ways for changing names of fields in my class (for writing more readable code or to follow language guidelines). One of them below:

public class CustomClassDescribingJSON {

    @SerializedName("name")
    private String mName;

    @SerializedName("description")
    private String mDescription;

    @SerializedName("price")
    private double mPrice;

    @SerializedName("location")
    private Location mLocation;

    // Some getters and setters and other methods, fields, etc

    public class Location {

        @SerializedName("latitude")
        private long mLatitude;

        @SerializedName("longitude")
        private long mLongitude;

    }

}

Using same code like above for parsing JSON:

String json; // This object was obtained earlier.
CustomClassDescribingJSON object = new Gson().fromJson(json, CustomClassDescribingJSON.class);

But I could not find a possibility to change the structure of the class. For example, I would like to use next class for parsing the same JSON:

public class CustomClassDescribingJSON {

    private String mName;
    private String mDescription;
    private double mPrice;

    private long mLatitude;
    private long mLongitude;

}

Questions:

  1. Same as in the header: Is there way to associate arbitrary data structure with GSON parser?
  2. Maybe there are another libraries to do what I want?
Community
  • 1
  • 1
Igor Tyulkanov
  • 5,487
  • 2
  • 32
  • 49

2 Answers2

1

Simply convert the JSON string into HashMap<String, Object> then populate any type of custom structure by simply iterating it or create a constructor in each custom object class as shown below to populate the fields.

class CustomClassDescribingJSON {
    public CustomClassDescribingJSON(Map<String, Object> data) {
       // initialize the instance member 
    }
}

Sample code:

Reader reader = new BufferedReader(new FileReader(new File("resources/json12.txt")));
Type type = new TypeToken<HashMap<String, Object>>() {}.getType();
HashMap<String, Object> data = new Gson().fromJson(reader, type);

System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

output:

{
    "price": 123.0,
    "location": {
      "latitude": 456987.0,
      "longitude": 963258.0
    },
    "description": "some_description",
    "name": "some_name"
}
Braj
  • 46,415
  • 5
  • 60
  • 76
1

Would a custom GSON (de-)serializer help? See https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

Ilya Shinkarenko
  • 2,134
  • 18
  • 35