- Type Parameters:
T
- the type of object in the array
This could be used if e.g. you are writing JSON to a z/OS dataset with a limited record length, but you do not want the overhead of JSON pretty printing. Using this class means the LRECL only needs to be long enough for the individual array entries.
The JSON for each element is generated using a Gson instance which you have configured as required.
Use from(Iterable, Gson)
or from(Object[], Gson)
to format the
array into a single String.
To handle larger arrays which may be too big for a single String, create a
MultiLineArray
object and use element(Object)
, elements(Iterable)
or elements(Object[])
to generate the array entries. Finally, call endArray()
to end the JSON array.
The Strings returned from each method do not end with a line separator, so they should be
written using System.out.println
or similar. Line separators are inserted between elements by
the methods that process multiple objects.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionGenerate a JSON array element, with begin array or element delimiters as appropriate.Generate JSON array elements with begin array and element delimiters as appropriate.Generate JSON array elements with begin array and element delimiters as appropriate.endArray()
End the JSON array.static <T> String
Generate a JSON array from the input Iterable, with newlines between the top level array elements.static <T> String
Generate a JSON array from the input Array, with newlines between the top level array elements.
-
Constructor Details
-
MultiLineArray
Construct a JSON multiline array formatter.- Parameters:
gson
- A gson instance used to generate JSON for each element.
-
-
Method Details
-
from
Generate a JSON array from the input Iterable, with newlines between the top level array elements.- Type Parameters:
T
- the type of element in the Iterable- Parameters:
src
- the source containing the array elementsgson
- a Gson instance used to generate the JSON for each element- Returns:
- a String containing the JSON array
-
from
Generate a JSON array from the input Array, with newlines between the top level array elements.- Type Parameters:
T
- the type of element in the Array- Parameters:
src
- the source containing the array elementsgson
- a Gson instance used to generate the JSON for each element- Returns:
- a String containing the JSON array
-
element
Generate a JSON array element, with begin array or element delimiters as appropriate. The String is not terminated with a newline, so it should be written using println or similar methods to end the line.- Parameters:
element
- the element to be converted to a JSON array element- Returns:
- a String containing the JSON array element
-
elements
Generate JSON array elements with begin array and element delimiters as appropriate. This can be used to generate JSON for multiple elements at the beginning or in the middle of the array. Newlines will be inserted between elements, but not after the last element.The entire string can be written with println or similar to add a newline after the string, so that each element appears on a separate line.
- Parameters:
src
- the source Iterable containing the array elements- Returns:
- a string containing JSON array elements, with newlines between elements but not after the last element.
-
elements
Generate JSON array elements with begin array and element delimiters as appropriate. This can be used to generate JSON for multiple elements at the beginning or in the middle of the array. Newlines will be inserted between elements, but not after the last element.The entire string can be written with println or similar to add a newline after the string, so that each element appears on a separate line.
- Parameters:
src
- the source array containing the array elements- Returns:
- a string containing JSON array elements, with newlines between elements but not after the last element.
-
endArray
End the JSON array.- Returns:
- a String ending the JSON array. If no elements have been previously generated, it will also contain the beginning of the array.
-