Class JSONWriter<W extends Writer>

java.lang.Object
com.leastfixedpoint.json.JSONWriter<W>

public class JSONWriter<W extends Writer> extends Object
Writes certain Java objects as JSON to a Writer.

  • java.lang.String becomes a JSON string.
  • java.lang.Boolean become JSON booleans.
  • JSONNull.INSTANCE becomes JSON null.
  • java.lang.Number and subclasses become JSON numbers (via BigDecimal).
  • Java arrays and Iterable objects become JSON arrays.
  • java.util.Map objects become JSON objects/maps.

If a Map has a non-String key, or an unsupported object is discovered while writing, JSONSerializationError will be thrown.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected static final char[]
     
    protected int
     
    protected boolean
     
    protected boolean
     
    protected W
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    JSONWriter(W writer)
    Construct a JSONWriter that will output on the given Writer, by default without pretty indentation.
    JSONWriter(W writer, boolean indenting)
    Construct a JSONWriter that will output on the given Writer, with optional pretty indentation.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    array(Object object)
     
    protected void
    bool(boolean b)
     
    protected void
    emit(char c)
     
    protected void
    emit(Object obj)
     
    protected void
    emitUnicode(char c)
     
    boolean
    Answers true iff the writer allows java's null value to be written as JSON null.
    boolean
    Answers true iff pretty indentation is turned on for this instance.
    boolean
    Answers true iff the writer sorts keys when writing objects, to ensure deterministic ordering.
    Retrieve the underlying Writer.
    protected void
     
    protected void
    map(Map<?,?> map)
     
    void
    If pretty indentation is turned on, outputs a newline character followed by a number of spaces corresponding to the current indentation level (which is not under direct control of any public methods)
    protected void
     
    void
    setAllowJavaNull(boolean value)
    If given true, allows java's null value to be written as JSON null; otherwise, requires nulls to be represented with instances of JSONNull.
    void
    setIndentMode(boolean value)
    Alters the pretty indentation mode of this instance for future writes.
    void
    setSortKeys(boolean value)
    If given true, enables object key-sorting on write; false disables sorting.
    protected void
     
    void
    write(Object object)
    Emit the given object as JSON to the embedded Writer.
    static void
    writeTo(Writer w, Object value)
    Serializes value as JSON, outputting to writer, without pretty indentation.
    static <W extends Writer>
    void
    writeTo(W w, Object value, boolean indenting)
    Serializes value as JSON, outputting to writer, with optional pretty indentation.
    static String
    Returns JSON text corresponding to value, without pretty indentation.
    static String
    writeToString(Object value, boolean indenting)
    Returns JSON text corresponding to value, with optional pretty indentation.
    static String
    Returns JSON text corresponding to value.

    Methods inherited from class java.lang.Object

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

    • indentLevel

      protected int indentLevel
    • writer

      protected W extends Writer writer
    • indentMode

      protected boolean indentMode
    • sortKeys

      protected boolean sortKeys
    • allowJavaNull

      protected boolean allowJavaNull
    • hex

      protected static final char[] hex
  • Constructor Details

    • JSONWriter

      public JSONWriter(W writer)
      Construct a JSONWriter that will output on the given Writer, by default without pretty indentation.
    • JSONWriter

      public JSONWriter(W writer, boolean indenting)
      Construct a JSONWriter that will output on the given Writer, with optional pretty indentation.
  • Method Details

    • writeTo

      public static void writeTo(Writer w, Object value) throws IOException
      Serializes value as JSON, outputting to writer, without pretty indentation.
      Throws:
      IOException
    • writeTo

      public static <W extends Writer> void writeTo(W w, Object value, boolean indenting) throws IOException
      Serializes value as JSON, outputting to writer, with optional pretty indentation.
      Throws:
      IOException
    • writeToString

      public static String writeToString(Object value) throws JSONSerializationError
      Returns JSON text corresponding to value, without pretty indentation.
      Throws:
      JSONSerializationError
    • writeToString

      public static String writeToString(Object value, boolean indenting) throws JSONSerializationError
      Returns JSON text corresponding to value, with optional pretty indentation.
      Throws:
      JSONSerializationError
    • writeToString

      public static String writeToString(String value)
      Returns JSON text corresponding to value. Unlike the overload writeToString(Object), never throws JSONSerializationError.
    • getWriter

      public W getWriter()
      Retrieve the underlying Writer.
    • getIndentMode

      public boolean getIndentMode()
      Answers true iff pretty indentation is turned on for this instance.
    • setIndentMode

      public void setIndentMode(boolean value)
      Alters the pretty indentation mode of this instance for future writes.
    • getSortKeys

      public boolean getSortKeys()
      Answers true iff the writer sorts keys when writing objects, to ensure deterministic ordering.
    • setSortKeys

      public void setSortKeys(boolean value)
      If given true, enables object key-sorting on write; false disables sorting.
    • getAllowJavaNull

      public boolean getAllowJavaNull()
      Answers true iff the writer allows java's null value to be written as JSON null.
    • setAllowJavaNull

      public void setAllowJavaNull(boolean value)
      If given true, allows java's null value to be written as JSON null; otherwise, requires nulls to be represented with instances of JSONNull.
    • newline

      public void newline() throws IOException
      If pretty indentation is turned on, outputs a newline character followed by a number of spaces corresponding to the current indentation level (which is not under direct control of any public methods)
      Throws:
      IOException
    • write

      public void write(Object object) throws IOException
      Emit the given object as JSON to the embedded Writer.
      Throws:
      IOException
    • number

      protected void number(Number n) throws IOException
      Throws:
      IOException
    • map

      protected void map(Map<?,?> map) throws IOException
      Throws:
      IOException
    • iterable

      protected void iterable(Iterable<?> it) throws IOException
      Throws:
      IOException
    • array

      protected void array(Object object) throws IOException
      Throws:
      IOException
    • bool

      protected void bool(boolean b) throws IOException
      Throws:
      IOException
    • string

      protected void string(Object obj) throws IOException
      Throws:
      IOException
    • emit

      protected void emit(Object obj) throws IOException
      Throws:
      IOException
    • emit

      protected void emit(char c) throws IOException
      Throws:
      IOException
    • emitUnicode

      protected void emitUnicode(char c) throws IOException
      Throws:
      IOException