Question

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;
    ...
}
Was it helpful?

Solution

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.*

OTHER TIPS

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.

De-Serialize the example JSON into a full Example object, use the name properties of the Example object to build a String of the things you want, forget about the Example object.

I don't really understand the second question completely, but if you have a full Test1 object will all the fields/properties then you can create a Test2 object which takes the fields from Test1 which it wants. E.g your Test2 object can accept Test1 as a parameter in its constructor and take only the properties which it needs ignoring the rest.

While you're streaming in the Jsons over http, you can simply Discard the text and only store your custom objects. In this case, you will be continually discarding the unneeded information.

While stream not empty  

    Read the next block into a new string

    Deserialize the string to the your object

    Store the object in a myArrayList

Note : reading the whole JSON and consuming it, as a whole, is most likely necessary if you want your application to be robust. Unless you want to read the JSON as a raw character stream ( I doubt, unless your JSON is really, prohibitively big, that this shortcut is necessary).

Neverthelss , reading the input stream without imposing and JSON well formedness requirements, could be done without ever having to write and unnecessary data structures to memory. This could work if you only want a small subset of the data- I.e. You just want people's names, or urls in the JSON. But it would break down if you want more complex data structures. Nevertheless :

// example parse urls from JSON lines without storing the whole data structure

While input stream is not empty

     String x = nextLine

     If x contains "http"

         myArrayList.add(parseUrl(x)

Final thoughts :

Bur ultimately, Jsons REST request are not like SQL- you cannot generucally and arbitrarily ignore certain fields.Perhaps , if you really want lighter weight Jsons, the more natural approach is to tell or check if your RESt service provider can simply broaden the types of RESt requests parameters to accommodate your use case.

I would propose to use Jackson library. It is provided with Apache license, so you can use it for commercial use free. In tutorial look the capital "Streaming API Example". It is very easy, and you control the streaming process totally. So, you can take what you want and ignore all other things. Jackson library is divided in some jars. The jar that supports streaming API, is the smallest and doesn't use any other. It is the answer, I think.

Jackson can provide even more. In this article you can find the method to read the JSON file on the higher level, as elements, but with PREVIOUSLY setting what objects you need and what not. So, you can have as a result of parsing immediately only the elements you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top