Skip to content
Advertisement

Copy all values List to Jackson JsonGenerator Array directly instead of looping over it

I am creating a JSON file using the Jackson JsonGenerator for which I need to add the elements from List<String>. This List consists of the JSON Fragments which needs to be added to my final JSON file.

As of now, I am looping over the List<String> and adding them to my Jackson JsonGenerator writeRaw one-by-one. This is working fine as of now. However, my List can have 1000s or more values then I do not wish to loop and add one by one. Rather than that, I am finding a way to directly add the array to the JsonGeneator array. I wanted to know if there is a way to directly copy the array something like this:

JavaScript

Following is the code I have currently:

JavaScript

As you can observe from the above code, I am looping over the List<String> and adding them to JsonGeneator one-by-one. Can someone please suggest if there is a way to directly add the List<String> to JsonGenerator so that I can skip the looping part.

Something similar is available in JSONObject here we can directly add the list:

JavaScript

Any help or suggestion would be really appreciated.

Advertisement

Answer

As indicated by @Thomas I did the following to add the elements from LIST to my JsonGenerator:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement