Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON : custom object deserialization

Tags:

java

android

gson

Ok so I edited the question because it wasn't clear enough.

Edit 2 : updated the JSON file.

I'm using GSON in an Android app, and I need to parse JSON files, that come from a server, and are a little too complexes. I don't want to have my object structure too heavy, so I would like to simplify the contents : so the structure of my object won't be the structure of the JSON file.

For example, if in the JSON I have this :

{
    "object1":{
        "attribute1" : "test1",
        "attribute40" : "test40",
        "user":{
            "id":1,
            "name":"foo"
        }
        ,"example":{
            "total":10,
            "list":[
            {
                "tag":"tag1",
                "name":"object name 1",
                "pos":1
            },
            {
                "tag":"tag10",
                "name":"object name 10",
                "pos":10
            }
        ]
        }
    }
    "object2":{
        "attribute1":"test..."
    }
}

I don't want to keep in my current object structure, an object Example, that contains an ArrayList and an int "total". But I would like to keep only a simple String with the value "object name 1;object name 2;...".

Moreover, I would like to store only the user Id, not the complete User, because I already have the complete user stored somewhere else, with an other server API call.

So my class class would be something like :

class Foo{
    int userId;
    String example; //"object name 1;object name 2;..."
    ...
}

So I suppose that we can achieve this with a custom deserializer, but I don't find how. I would like if possible to minimize the memory, so I don't think that having a full object example, and then use it to build my String example is a correct way.

In the worst case, if it's too complicated, I would like to be able to store at least only the list of Tag items when I parse the Example Object : so I need a custom deserializer to get rid off the int total.

So I would have :

class Foo{
    int userId;
    ArrayList<Tag> example;
    ...
}
like image 715
Chayy Avatar asked Dec 16 '11 10:12

Chayy


2 Answers

I adopted the answer to present the full solution designed in chat and to fit to the changed JSON string. The code assumes that the string json holds exactly the (updated) JSON from the question. The requirement is to fill the following class (setter and toString ommitted):

class Object1
{
    private String attribute1;
    private String attribute40;
    private int userId;
    private String  nameList;
}

GSON supports (as the most other REST-libs) three modes:

  • GSON_DOM
    Reads the whole JSON via JsonParser.parse() and builds a DOM tree in memory (object model access). Therefore this solution is good for small JSON files.
  • GSON_STREAM
    Reads only chunks of the JSON via JsonReader. Code is more complicated, but it is suited for large JSON files. As of Android 3.0 Honeycomb, GSON's streaming parser is included as android.util.JsonReader.
  • GSON_BIND
    Databinding directly to classes via reflection, minimizes the code significantely. GSON allows mixed mode, which means to combine GSON_DOM and GSON_BIND or GSON_STREAM and GSON_BIND which this answer should show.

To fill the class Object1 via GSON_DOM and GSON_BIND the implementation looks like:

private static void deserializeViaObjectAccess(final String json)
{
    Gson gson = new Gson();

    // Read the whole JSON into meomory via GSON_DOM
    JsonParser parser = new JsonParser();
    JsonObject object1 = parser.parse(json).getAsJsonObject().getAsJsonObject("object1");

    // map the Object1 class via GSON_BIND
    // (bind common attributes which exist in JSON and as properties in the class)
    // mapper acts as factory
    Object1 result = gson.fromJson(object1, Object1.class);

    // manually read the attribute from the user object
    int userId = object1.getAsJsonObject("user").getAsJsonPrimitive("id").getAsInt();
    result.setUserId(userId);

    // manually read the attributes from the example object
    String names = "";
    JsonArray list = object1.getAsJsonObject("example").getAsJsonArray("list");
    for (int i = 0; i < list.size(); ++i)
    {
        JsonObject entry = list.get(i).getAsJsonObject();
        String name = entry.getAsJsonPrimitive("name").getAsString();

        names = i == 0 ? name : names + "; " + name;
    }
    result.setNameList(names);

    // Output the result
    log.debug(result.toString());
}

To fill the class Object1 via GSON_STREAM and GSON_BIND the implementation looks like:

At the moment, this is only possible when a node is completly loaded via GSON_BIND or GSON_STREAM. This example needs that a node itself should be splitted. This is only possible with the upcoming version 2.2. I will hand the code in later when GSON 2.2 is available.*

like image 97
15 revs Avatar answered Sep 23 '22 08:09

15 revs


One option would be to parse the JSON string using the parser built inside Gson as detailed here. You would do something like this:

com.google.gson.JsonParser parser = new JsonParser();
JsonObject object = parser.parse(data).getAsJsonObject();
JsonObject example = object.getAsJsonObject("example");
JsonArray list = example.getAsJsonArray("list");

JsonObject and JsonArray are part of Gson itself.

After using these you can use functions like getAsInt to parse individual fields and build and return whatever object you want.

Edit 1

It does seem like that you can use gson.fromJson on a custom class too and not just the generic Java types as given in this example. So you have to parse your JSON string using parse and call fromJson on one of the inner objects or arrays.

like image 41
Abhinav Avatar answered Sep 20 '22 08:09

Abhinav