You are working on a legacy app that keeps track of local businesses using a Business class. You are making a custom serializer that implements the JsonSerializer<T> interface. Some of the field names are a bit wordy, so you are tasked with streamlining the names when they get serialized to JSON. In particular, the field whatKindOfBusiness seems a bit excessive.
public class Business {
private String whatKindOfBusiness;
public String getWhatKindOfBusiness() {
return whatKindOfBusiness;
}
// other various fields, getters, setters, constructor, methods, etc...
}
Given a Business object business that is being serialized into a JsonObject called businessJson, add to it the category property that will be assigned the value stored in whatKindOfBusiness.
public class BusinessGsonSerializer implements JsonSerializer<Business> {
@Override
public JsonElement serialize(Business business, Type type,
JsonSerializationContext jsonSerializationContext) {
JsonObject businessJson = new JsonObject();
// Add line of code to customize the field name when serialized
return businessJson;
}
}