I have a class
public class Car {
public String color = null;
public String brand = null;
public HashMap<String,String> attributes;
public Car() {
this.attributes = new HashMap<>();
}
public static void main( String[] args ) {
Car car = new Car();
car.color = "Red";
car.brand = "Hyundai";
car.attributes.put("key1", "value1");
car.attributes.put("key2", "value1");
Gson gson = new Gson();
String json = gson.toJson(car);
System.out.println(json);
}
}
This currently serializes my car
object into
{"color":"Red","brand":"Hyundai","attributes":{"key1":"value1","key2":"value1"}}
But I would like to unpack and serialize the attributes
Dictionary from Car
class as individual properties rather dictionary.
Ideally, i would like my json to be,
{"color":"Red","brand":"Hyundai","key1":"value1","key2":"value1"}
How do I achieve the same in GSON?