Class JsonGenerator

java.lang.Object
uk.blankaspect.common.json.JsonGenerator

public class JsonGenerator extends Object
This class implements a generator that transforms a tree of values that are represented by nodes into JSON text. The generator operates in one of several modes, which control the way in which whitespace is written between the tokens of the JSON text.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    This is an enumeration of the modes that control the way in which whitespace is written between the tokens of the JSON text.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of a JSON generator with default values for the mode (NORMAL), the opening bracket on the same line flag (false), the indent increment (2) and the maximum line length (80).
    Creates a new instance of a JSON generator with the specified mode.
    JsonGenerator(JsonGenerator.Mode mode, boolean openingBracketOnSameLine)
    Creates a new instance of a JSON generator with the specified mode and opening bracket on the same line flag.
    JsonGenerator(JsonGenerator.Mode mode, boolean openingBracketOnSameLine, int indentIncrement, int maxLineLength)
    Creates a new instance of a JSON generator with the specified mode, opening bracket on the same line flag, indent increment and maximum line length.
  • Method Summary

    Modifier and Type
    Method
    Description
    Generates and returns JSON text for the specified JSON value in accordance with the properties of this generator: mode, opening bracket on the same line flag, indent increment, maximum line length.

    Methods inherited from class java.lang.Object

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

    • JsonGenerator

      public JsonGenerator()
      Creates a new instance of a JSON generator with default values for the mode (NORMAL), the opening bracket on the same line flag (false), the indent increment (2) and the maximum line length (80).
    • JsonGenerator

      public JsonGenerator(JsonGenerator.Mode mode)
      Creates a new instance of a JSON generator with the specified mode. Default values will be used for the opening bracket on the same line flag (false), the indent increment (2) and the maximum line length (80), which are applicable only in a multi-line mode (NORMAL or EXPANDED).
      Parameters:
      mode - the mode of the generator, which controls the way in which whitespace is written between the tokens of the JSON text.
    • JsonGenerator

      public JsonGenerator(JsonGenerator.Mode mode, boolean openingBracketOnSameLine)
      Creates a new instance of a JSON generator with the specified mode and opening bracket on the same line flag. Default values will be used for the indent increment (2) and the maximum line length (80), which are applicable only in a multi-line mode (NORMAL or EXPANDED).
      Parameters:
      mode - the mode of the generator, which controls the way in which whitespace is written between the tokens of the JSON text.
      openingBracketOnSameLine - if true, the opening bracket of a JSON object or array will be written on the same line as the non-whitespace text that precedes it.
    • JsonGenerator

      public JsonGenerator(JsonGenerator.Mode mode, boolean openingBracketOnSameLine, int indentIncrement, int maxLineLength)
      Creates a new instance of a JSON generator with the specified mode, opening bracket on the same line flag, indent increment and maximum line length.
      Parameters:
      mode - the mode of the generator, which controls the way in which whitespace is written between the tokens of the JSON text.
      openingBracketOnSameLine - if true, the opening bracket of a JSON object or array will be written on the same line as the non-whitespace text that precedes it.
      indentIncrement - the number of spaces by which indentation will be increased from one level to the next. This parameter has no effect in a single-line mode (DENSE or COMPACT).
      maxLineLength - the maximum length of a line of JSON text. This parameter has no effect in a single-line mode (DENSE or COMPACT).
  • Method Details

    • generate

      public String generate(AbstractNode value)
      Generates and returns JSON text for the specified JSON value in accordance with the properties of this generator:
      • mode,
      • opening bracket on the same line flag,
      • indent increment,
      • maximum line length.
      Parameters:
      value - the JSON value for which JSON text will be generated.
      Returns:
      JSON text for value.
      Throws:
      NodeTypeException - if any node in the tree whose root is value does not represent a JSON value.