I want to create a custom serializer in GSON that insert a key/value pair in an object, not a new object. For example, supose this:
class Car {
@Expose
String model;
@Expose
Manufacturer manufacturer;
}
class Manufacturer {
@Expose
String name;
@Expose
String from;
}
I want to get then a JSON like this:
"car":{
"model":"beatle",
"manufacturer":"volkswagen",
"country":"Germany"
}
But no matter how I code the serializer, it insists to create a manufacturer object inside "car"
"manufacturer":{
"name":"volkswagen",
"country":"Germany"
}
How can I fix that, to get only key/value pairs?
PS: I cannot make significative changes in classes, because they are mapping the DB. It is just a example to simulate my problem.
I was not possible to make what I want with JsonSerializer. I could only make it works OK with the new API of GSON, since 2.1 (TypeAdapter class).