Deal with huge JSON responses

I think I need to rewrite some modules of my app because when the number of entities that are rendered increases, it fails and errors too. At this moment, I’m using Jackson and HttpClient. As much as I trust in Jackson, something tells me that the problem is the second lib. Can HttpClient deal with large responses? (e.g. this one that is about 400 lines)

Besides that, in my app, the way I parse the request goes something like this:

public Object handle(HttpResponse response, String rootName) {
    try {
        String json = EntityUtils.toString(response.getEntity()); 
        // better "new BasicResponseHandler().handleResponse(response)" ????
        int statusCode = response.getStatusLine().getStatusCode();
        if ( statusCode >= 200 && statusCode < 300 ) {
            return createObject(json, rootName);
        }
        else{
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public Object createObject (String json, String rootName) {
    try {
        this.root = this.mapper.readTree(json);
        String className = Finder.findClassName(rootName);
        Class clazz = this.getObjectClass(className);
        return mapper.treeToValue(root.get(rootName), clazz);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

How I can improve this piece of code to be more efficient with large responses?

Thanks in advance!

Answer

There’s no need to create the String json, as ObjectMapper#readTree can accept an InputStream as well. For example, this will be slightly more efficient:

public Object handle(HttpResponse response, String rootName) {
    try {
        int statusCode = response.getStatusLine().getStatusCode();
        if ( statusCode >= 200 && statusCode < 300 ) {
            return createObject(response.getEntity().getContent(), rootName);
        }
        else{
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

public Object createObject (InputStream json, String rootName) {
    try {
        this.root = this.mapper.readTree(json);
        String className = Finder.findClassName(rootName);
        Class clazz = this.getObjectClass(className);
        return mapper.treeToValue(root.get(rootName), clazz);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Leave a Reply

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