Class MultiLineArray<T>

java.lang.Object
com.blackhillsoftware.json.util.MultiLineArray<T>
Type Parameters:
T - the type of object in the array

public class MultiLineArray<T> extends Object
Write a JSON array with line separators between elements.

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

    Constructors
    Constructor
    Description
    Construct a JSON multiline array formatter.
  • Method Summary

    Modifier and Type
    Method
    Description
    element(Object element)
    Generate a JSON array element, with begin array or element delimiters as appropriate.
    Generate JSON array elements with begin array and element delimiters as appropriate.
    elements(T[] src)
    Generate JSON array elements with begin array and element delimiters as appropriate.
    End the JSON array.
    static <T> String
    from(Iterable<T> src, Gson gson)
    Generate a JSON array from the input Iterable, with newlines between the top level array elements.
    static <T> String
    from(T[] src, Gson gson)
    Generate a JSON array from the input Array, with newlines between the top level array elements.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MultiLineArray

      public MultiLineArray(Gson gson)
      Construct a JSON multiline array formatter.
      Parameters:
      gson - A gson instance used to generate JSON for each element.
  • Method Details

    • from

      public static <T> String from(Iterable<T> src, Gson gson)
      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 elements
      gson - a Gson instance used to generate the JSON for each element
      Returns:
      a String containing the JSON array
    • from

      public static <T> String from(T[] src, Gson gson)
      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 elements
      gson - a Gson instance used to generate the JSON for each element
      Returns:
      a String containing the JSON array
    • element

      public String element(Object 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

      public String elements(Iterable<T> src)
      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

      public String elements(T[] src)
      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

      public String 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.