Custom JSON Deserializer for initiating your Java Model Class

Updated 14 May 2018

Save

In this blog, we will learn about using Custom JSON Deserializer for initiating your Java Model Class from some JSON Object (or Http response or File or any Source).

What this blog contains?

This blog will help you parse a JSON Object to your relevant Java model class,

or in better words a list of Objects of your Java Model class.

Well, I was recently working on some Android project and am using Retrofit with Gson for easily parsing and allocating the objects received from APIs.

All was going very well, until recently, I faced an issue with the parsing of ArrayList of some submodels in my Model class. The exception log was something like

Well, the reason for this was clearly mentioned, the GsonTypeConverter, added in my Retrofit class was actually expecting an Array but the API Returned an Object.

But in our test APIs were returning an array only.

When we looked further in the core execution of the API return values,

we found that if there is a single object then an instance of Object will be returned and when there are more than one objects then an array will be returned and this working cannot be modified for unknown reasons.

Ahhh!!!!!!!!

This blog aims to resolve the issue mentioned above.

And I have two solutions for you to look and use :

  1. Making the Object as some generic Object and then converting it to ArrayList on the runtime.
  2. Making a Custom Deserializer that will extend the JsonDeserializer ( the class that Gson uses and using as per your case).

I tried both the options mentioned above and will share the code for both of them, but I personally liked the first option.

Let’s discuss the first solution in detail first.

APPROACH

  1. Declare the object in ambiguity as an object of JsonElement class.
  2. In you getter, where you were fetching the list from now you need to modify the getter type to JsonElement ( Pretty Obvious)
  3. You need to fetch the list of the Objects with some other function and this getter should be something shown in the snippet below.

CODE

Old (Problematic) Model class :

New Modified And Updated Model Class :

 

The second approach is shared in this blog.

Custom GSON Serializer & De-serializer with JsonAdapter

Stay tuned, Stay Super.

Keep Coding and Keep Sharing 🙂

Some other useful articles :

For Retrofit Basics : 

Using Retrofit library to make Http Requests

Android Retrofit 2 custom callback

For other response types from APIs, you can check these articles :

Handle the XML Response from HTTP Request using Retrofit.

 

 

Handle the HTML Response from HTTP Request using Retrofit

Get Response as String from your Retrofit Call

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


8 comments

  • sm
    • anchit (Moderator)
  • Thawzintoe
    • anchit (Moderator)
      • ThawZinToe
        • anchit (Moderator)
  • ThawZinToe
    • anchit (Moderator)
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home