My question is what’s the best way to convert multiple lists back to back into a single JsonArray. The lists are coming on the fly, so I don’t want to or I can’t have all the lists(list1 – listn) merged in a big list, then use Jackson to write the merged list to a JsonArray.
Convert
ArrayList<Event> list1 = new ArrayList<Event>();
list1.add(new Event("a1","a2"));
list1.add(new Event("b1","b2"));
ArrayList<Event> list2 = new ArrayList<Event>();
list2.add(new Event("c1","c2"));
list2.add(new Event("d1","d2"));
......
......listn
To a single jsonArray:
[
{"field1":"a1", "field2":"a2"},
{"field1":"b1", "field2":"b2"},
{"field1":"c1", "field2":"c2"},
{"field1":"d1", "field2":"d2"},
......
{"field1":"n1", "field2":"n2"}
]
At the end of the day, I found out that there is no such way in Jackson allowing you directly write two lists back to back into a JsonArray. You can either implement JsonGenerator with your own implementation writeStartArray() writeEndArray() OR even easier, you can just define the startArray and endArray, and construct the jsonArray by yourself
so code will roughly look like: