0

I hava a java node pojo :-

public class Node {
 
  String name;

  List<Node> children;

  List<Object> values;

}

I am trying to convert this to json string.

Two approaches I have tried so far

Initially using simple gson.tojson was giving me OOM exception. Then I tried with this solution (Gson, serialize a tree structure) switched to TypeAdapter as below :-

public class NodeAdapter extends TypeAdapter<Node> {

    @Override
    public void write(JsonWriter jsonWriter, TreeNode node) throws IOException {
        jsonWriter.beginObject()
                .name("name")
                .jsonValue("\"" + node.getName() + "\"")
                .name("value")
                .jsonValue("\"" + node.getValues() + "\"")
                .name("children")
                .beginArray();
        // Recursive call to the children nodes
        for (Node c : node.getChildren()) {
            this.write(jsonWriter, c);
        }
        jsonWriter.endArray()
                .endObject();
    }

    @Override
    public Node read(JsonReader jsonReader) throws IOException {
        return null;
    }
}

Now this TypeAdapter approach works fine when the response is not too big, however, when the response starts to get bigger then it starts throwing OOM exception.

I am trying to convert Java object to json string like this :-

{
          "record": [
             {
               "name" : "Path A",
               "children" : [ 
                    {
                     "name": "Path B",
                     "children" : [
                        {
                            "name": "Path C",
                            "children" : [],
                            "values" : ["val5", "val6" ]
                            }
                     ],
                     "values" : [
                         "val3", "val4"
                     ]
                    }
               ],
               "values" : [
                    "val1", "val2"
               ]
             }
          ]
        
    }

In above json, children can have n childs.

The other approach I looked for is json streaming, but could not find a solution for my problem statement yet.

Any help/suggestion would be highly appreciated.

DEV
  • 19
  • 1
  • 4
  • Have you tried : https://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jacks – Mohit Sep 06 '22 at 06:50

1 Answers1

0

Have you tried increasing the JVM heap size? Using an option such as

-Xmx256m

Make an estimate of how much memory you need. How much data do you have? Make a small sample object, serialise to JSON, how big is that string? I'm guessing that the JSON will be significantly bigger than the raw data. Can you reasonably, in the same process, hold the data and its serialised form?

At some point, if your data is unpredictably large you might do better to write the JSON to a file, then you don't need to keep it all in memory:

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

Try.withResources(() -> new FileWriter("gson.json"))
        .of(writer -> Try.run(() -> gson.toJson(object, writer)).get())
        .get();

(From Tim Mattison

djna
  • 54,992
  • 14
  • 74
  • 117