0

Previously my code was just this:

objJson = gson.toJson(objList);
return objJson;

And I got the string JSON with that return value.

But then I started getting famous Out Of Memory errors when the JSON becomes too large.

Then I followed the following post that converts the list of objects into JSON String in an efficient way that OOM errors will be no more:

https://sites.google.com/site/gson/streaming

So, I have followed the above approach here:

public String writeJsonStream(List<MyObj> objList) throws IOException {
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    OutputStreamWriter outputStreamWriter=new OutputStreamWriter(baos,"UTF-8");
    JsonWriter writer = new JsonWriter(outputStreamWriter);
    writer.setIndent("  ");
    writer.beginArray();
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
    for (MyObj myobj : objList) {
        gson.toJson(myobj, MyObj.class, writer);
    }

    String objJson = writer.toString();
    writer.endArray();
    writer.close();
    return objJson;
}

But this returning object com.google.gson.stream.JsonWriter@6a9454cd.

The method gson.toJson(myobj, MyObj.class, writer); is of type void and doesn't returns JSON string. So how can I get the JSON string in this case?

0

3 Answers 3

3

That's because you are getting the JSON String from String objJson = writer.toString();. This is not how you should retrieve it: this code will just call the toString() method on the writer instance. Since toString() is not overriden for this object, the result is the default com.google.gson.stream.JsonWriter@6a9454cd.

What you want to do instead is get the bytes that the JsonWriter wrote to the output stream. In your case, you are using a ByteArrayOutputStream so you can call toString(charsetName) to get the content as a String:

public String writeJsonStream(List<MyObj> objList) throws IOException {
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    OutputStreamWriter outputStreamWriter=new OutputStreamWriter(baos,"UTF-8");
    JsonWriter writer = new JsonWriter(outputStreamWriter);
    writer.setIndent("  ");
    writer.beginArray();
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
    for (MyObj myobj : objList) {
        gson.toJson(myobj, MyObj.class, writer);
    }
    writer.endArray();
    writer.close();
    return baos.toString("UTF-8");
}

As a side note, you could just use a StringWriter instead and get the content with toString():

StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
// rest of code
writer.close();
String json = stringWriter.toString();
Sign up to request clarification or add additional context in comments.

Comments

0

Well, converting stream to a static object unfortunately does not avoid OOM problem. In either way, you are trying to allocate/request memory to construct/write that string. You must pipe the resulting stream to another one, or simply consume all the data in that stream by using BufferedWriter in some loop or else.

Comments

0

Try this. Change ByteArrayOutputStream to OutputStream and get object from System.out

   public String writeJsonStream(List<MyObj> objList) throws IOException {
        OutputStream baos = System.out;
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(baos,"UTF-8");
        JsonWriter writer = new JsonWriter(outputStreamWriter);
        writer.setIndent("  ");
        writer.beginArray();
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
        for (MyObj myobj : objList) {
            gson.toJson(myobj, MyObj.class, writer);
        }

        String objJson = writer.toString();
        writer.endArray();
        writer.close();
        return objJson;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.