Native Shopify Mobile App with 20 new features
Kickstart your hyperlocal marketplace in Corona pandemic with a starter guide
Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 220.127.116.11 release ready!
Owner and Founder. Opencart
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.
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.
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.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
The support is fast and do everything in their power to make it perfect. If you need customization it's also done right away. I had massive research to select Mobikul and now I'm sure it was the right one.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.