Using GSON is if it is possible to specify multiple JsonSerializers for a single object to allow for different data to be exposed with it programmatically?
For example I have this POJO
public class Person {
private Long id;
private String name;
private String email;
private Set<Addresses> addresses;
}
and this corresponding JsonSerializer:
public static final JsonSerializer<Person> serializer1 =
(Person src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) -> {
JsonObject personJson = new JsonObject();
personJson.addProperty("name", src.getName());
personJson.addProperty("email", src.getEmail());
return personJson;
}
Now is it possible for me to add a second JsonSerializer like this to expose alternative data? Also if so how would you tell Gson to differentiate between the 2?
public static final JsonSerializer<Person> serializer2 =
(Person src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) -> {
JsonObject personJson = new JsonObject();
personJson.addProperty("name", src.getName());
personJson.addProperty("email", src.getEmail());
personJson.add("addresses", toJsonArray(src.getAddresses()))
return personJson;
}