Looks like you have a Cars
object with a list of Car List<Car>
and a count property
.
Trouble is, the count property is IN the map. What I did is extend the map, just to add the property. Then, you need to define your own serializer/deserializer for this map
(Source get-nested-json-object-with-gson)
The model:
class Cars {
private MyMap cars = new MyMap();
public MyMap getCars() {
return cars;
}
public void setCars(MyMap cars) {
this.cars = cars;
}
public void add(String name, Car car){
cars.put(name, car);
cars.setCount(cars.getCount()+1);
}
}
class MyMap extends HashMap<String, Car> {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
class Car {
private int length;
private int weight;
public Car() {
}
public Car(int length, int weight) {
this.length = length;
this.weight = weight;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
The custom Serializer/Deserializer:
class MyDeserializer implements JsonDeserializer<MyMap>
{
@Override
public MyMap deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonElement count = json.getAsJsonObject().remove("count");
MyMap myMap = new Gson().fromJson(json.getAsJsonObject().toString(), type);
if(count!=null){
myMap.setCount(count.getAsInt());
}
return myMap;
}
}
class MySerializer implements JsonSerializer<MyMap>
{
@Override
public JsonElement serialize(MyMap src, Type type, JsonSerializationContext context) {
JsonElement serialize = context.serialize(src, Map.class);
serialize.getAsJsonObject().add("count", new JsonPrimitive(src.getCount()));
return serialize;
}
}
The code to read and write the json:
String json = "{\"cars\":{\"jeep\":{\"length\":4670,\"weight\":1450},\"ford\":{\"length\":4460,\"weight\":1880},\"count\":128}}";
Cars cars = new Cars();
cars.add("jeep", new Car(4670, 1450));
cars.add("ford", new Car(4460, 1880));
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyMap.class, new MyDeserializer())
.registerTypeAdapter(MyMap.class, new MySerializer())
.create();
String json2 = gson.toJson(cars);
cars = gson.fromJson(json, Cars.class);
cars = gson.fromJson(json2, Cars.class);