The Google’s GSON library is purely awesome. If you haven’t heard about the library then please come out of the cave you are living in. The library had made the developers work really easy as it parses the json response and provides an POJO (Plain old Java Object). You can check the GitHub link for the library.
The only issue of using the library is that you cannot manipulate data before creating the POJO for it. It creates the objects with the same structure as the JSON response coming from server and sometimes we do need to manipulate data and then to use it. One of the option is to process the data after object is been created i.e. at the time of usage of data. But that will cause a lot of boilerplate code as we will be always performing a certain lines of codes before using the data stored in object. The similar things happen while we have to convert our Objects in Json for any HTTP or connection call.
But GSON has fixed this in the library version 2.7 and above. It has given us an option to implement custom Serialization and De-serialization of data on our end using @JsonAdapter. In this tutorial we will see how we can apply Custom Serialization and De-serialization in GSON.
First and the most simple step is the annotate your POJO class with JsonAdapter and the name of the class that will decide the Serialization and De-serialization.
1 2 3 4 |
@JsonAdapter(ProductSerializerDeserializer.class) public class JsonProducts { ... } |
Now the GSON library will not serialize or de-serialize the object using the default method but will use your defined functions. You can even perform anyone like only Custom Serialization and let GSON do the default De-serialization or vice versa.
In this particular example I will perform both for you. So the product have following values ID, Name, Price, Quantity, Discount. and I want the price to be shown according to the Quantity of product but while sending the product data to server it should only show price of a Single item.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class ProductDeserializer implements JsonDeserializer<JsonProducts>, JsonSerializer<JsonProducts> { @Override public JsonProducts deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject object = json.getAsJsonObject(); int ID = object.get("ID").getAsInt(); String name = object.get("name").getAsString(); float price = object.get("price").getAsFloat(); int quantity = object.get("quantity").getAsInt(); JsonProducts products = new JsonProducts(ID, name, (price * quantity) , quantity); return products; } @Override public JsonElement serialize(JsonProducts src, Type typeOfSrc, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.add("ID", src.getID()); object.add("name", src.getName()); object.add("price", (src.getPrice() / src.getQuantity)); object.add("quantity", src.getQuantity()); return object; } } |
As I have already mentioned earlier you can even implement one of the interfaces and can only Customize a single method, the implementation totally depends on your use cases. The only issue with this approach is that you have to Serialize/De-Serialize the whole object by yourself even for a minute case. If you don’t want to use this or your changes are as minute as mine one you can even try-
Custom JSON Deserializer for initiating your Java Model Class
By this method you can make those smaller changes easily. I hope that this will help you solving all the issues you are getting regarding the Serialization/De-Serailization.