ejay
ejay

Reputation: 1144

Android - GSON lib - Object deserializing Object

I use GSON lib to deserialize my objects when I get result from WS. For example my web service response contains :

"id" : "myid"
"code" : 200
"data" : can be anything (String, JSONObject, JSONArray...)

So here my class :

public class MyClass 
{
    private Object id;
    private int code;
    private Object data;
    private String type;

    public MyClass() 
    {
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public Object getId()
    {
        return id;
    }

    public void setId(Object id)
    {
        this.id = id;
    }

    public int getCode() {
        return code;
    }

    public Object getData() {
        return data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

As you can see, I don't know which type of data "data" would be, so I used the Object type here.

So when I get response I use :

GsonBuilder gsonBuilder = new GsonBuilder();        
Gson gson = gsonBuilder.create();

And used this method to convert my class :

public <T> T deserialize(Type pClass, JSONObject pJsonObject)
    {
        try
        {
            return gson.fromJson(pJsonObject.toString(), pClass);
        }
        catch (JsonSyntaxException e)
        {

        }

        return null;
    }

Everything works well. But I have a little problem. "data" field can be everything (a String, an Integer, a JSONObject, ...), and for my example, data is a JSONObject when I get result here. So "data" (which is Object) is a JSONObject, but converted into a LinkedTreeMap automatically here and not a JSONObject, so when I want to parse "data", I have a CastException :/

So the real problem here is if the user has sent a JSONObject to the API and I get this result when I call web service, GSON converts this JSONObject into a LinkedTreeMap, but I want to retrieve the same type that has been sent by the user.

Is it possible ?

Thanks to all !

J.

Upvotes: 1

Views: 128

Answers (1)

Pararth
Pararth

Reputation: 8134

You can add a Generic Model Class where "data" would be a String or Json Object..

It will help if you definitely know what is the "data" going to be, say for eg. it'l be a json object..then: (or can keep it as String and then check through code and map it accordingly)

private String id;
private int code;
private JsonObject data;
private String type;

And can use Annotation, from Gson lib, in the model class:

@SerializedName("responseVariableStringHere")  
private String responseVariable;

Then,

modelClassObject = gson.fromJson(responseContentString,ModelClass.class);  

after that,

JsonObject job = modelClassObject.getData();  

And repeat the mapping process..this time with the model class object which "data" has in response

objectFromData = gson.fromJson(job.get("tagfromtheJsonObject"), ObjectFromData.class);

Can avoid writing the:

public <T> T deserialize(Type pClass, JSONObject pJsonObject)

Upvotes: 1

Related Questions