Class JsonWriter

All Implemented Interfaces:
ExtendedHierarchicalStreamWriter, HierarchicalStreamWriter
Direct Known Subclasses:
JsonHierarchicalStreamWriter

public class JsonWriter extends AbstractJsonWriter
A simple writer that outputs JSON in a pretty-printed indented stream. Arrays, Lists and Sets rely on you NOT using XStream.addImplicitCollection(..).
Since:
1.3.1
  • Field Details

    • writer

      protected final QuickWriter writer
    • format

      protected final JsonWriter.Format format
    • depth

      private int depth
    • newLineProposed

      private boolean newLineProposed
  • Constructor Details

    • JsonWriter

      public JsonWriter(Writer writer, char[] lineIndenter, String newLine)
      Deprecated.
      As of 1.4 use instead
    • JsonWriter

      public JsonWriter(Writer writer, char[] lineIndenter)
      Deprecated.
      As of 1.4 use instead
    • JsonWriter

      public JsonWriter(Writer writer, String lineIndenter, String newLine)
      Deprecated.
      As of 1.4 use instead
    • JsonWriter

      public JsonWriter(Writer writer, String lineIndenter)
      Deprecated.
      As of 1.4 use instead
    • JsonWriter

      public JsonWriter(Writer writer)
    • JsonWriter

      public JsonWriter(Writer writer, char[] lineIndenter, String newLine, int mode)
      Deprecated.
      As of 1.4 use instead
      Since:
      1.3.1
    • JsonWriter

      public JsonWriter(Writer writer, int mode)
      Create a JsonWriter where the writer mode can be chosen.
      Parameters:
      writer - the Writer where the JSON is written to
      mode - the JsonWriter mode
      Since:
      1.3.1
      See Also:
    • JsonWriter

      public JsonWriter(Writer writer, JsonWriter.Format format)
      Create a JsonWriter where the format is provided.
      Parameters:
      writer - the Writer where the JSON is written to
      format - the JSON format definition
      Since:
      1.4
      See Also:
    • JsonWriter

      public JsonWriter(Writer writer, int mode, JsonWriter.Format format)
      Create a JsonWriter where the writer mode can be chosen and the format definition is provided.

      Following constants can be used as bit mask for the mode:

      Parameters:
      writer - the Writer where the JSON is written to
      mode - the JsonWriter mode
      format - the JSON format definition
      Since:
      1.4
    • JsonWriter

      public JsonWriter(Writer writer, int mode, JsonWriter.Format format, int bufferSize)
      Create a JsonWriter.
      Parameters:
      writer - the Writer where the JSON is written to
      mode - the JsonWriter mode
      format - the JSON format definition
      bufferSize - the buffer size of the internally used QuickWriter
      Since:
      1.4
      See Also:
  • Method Details

    • flush

      public void flush()
      Description copied from interface: HierarchicalStreamWriter
      Flush the writer, if necessary.
    • close

      public void close()
      Description copied from interface: HierarchicalStreamWriter
      Close the writer, if necessary.
    • underlyingWriter

      public HierarchicalStreamWriter underlyingWriter()
      Description copied from class: AbstractWriter
      Return the underlying HierarchicalStreamWriter implementation.

      If a Converter needs to access methods of a specific HierarchicalStreamWriter implementation that are not defined in the HierarchicalStreamWriter interface, it should call this method before casting. This is because the writer passed to the Converter is often wrapped/decorated by another implementation to provide additional functionality (such as XPath tracking).

      For example:

      MySpecificWriter mySpecificWriter = (MySpecificWriter)writer; // INCORRECT!
       mySpecificWriter.doSomethingSpecific();
      MySpecificWriter mySpecificWriter = (MySpecificWriter)writer.underlyingWriter();  // CORRECT!
       mySpecificWriter.doSomethingSpecific();

      Implementations of HierarchicalStreamWriter should return 'this', unless they are a decorator, in which case they should delegate to whatever they are wrapping.

      Specified by:
      underlyingWriter in interface HierarchicalStreamWriter
      Overrides:
      underlyingWriter in class AbstractWriter
    • startObject

      protected void startObject()
      Start a JSON object.
      Specified by:
      startObject in class AbstractJsonWriter
    • addLabel

      protected void addLabel(String name)
      Add a label to a JSON object.
      Specified by:
      addLabel in class AbstractJsonWriter
      Parameters:
      name - the label's name
    • addValue

      protected void addValue(String value, AbstractJsonWriter.Type type)
      Add a value to a JSON object's label or to an array.
      Specified by:
      addValue in class AbstractJsonWriter
      Parameters:
      value - the value itself
      type - the JSON type
    • startArray

      protected void startArray()
      Start a JSON array.
      Specified by:
      startArray in class AbstractJsonWriter
    • nextElement

      protected void nextElement()
      Prepare a JSON object or array for another element.
      Specified by:
      nextElement in class AbstractJsonWriter
    • endArray

      protected void endArray()
      End the JSON array.
      Specified by:
      endArray in class AbstractJsonWriter
    • endObject

      protected void endObject()
      End the JSON object.
      Specified by:
      endObject in class AbstractJsonWriter
    • startNewLine

      private void startNewLine()
    • endNewLine

      private void endNewLine()
    • writeNewLine

      private void writeNewLine()
    • writeText

      private void writeText(String text)