Class JsonAppendableWriter

java.lang.Object
com.grack.nanojson.JsonAppendableWriter
All Implemented Interfaces:
JsonSink<JsonAppendableWriter>

public final class JsonAppendableWriter extends Object implements JsonSink<JsonAppendableWriter>
JSON writer that emits JSON to a Appendable. Create this class with JsonWriter.on(Appendable) or JsonWriter.on(OutputStream).
 OutputStream out = ...;
 JsonWriter
     .indent("  ")
     .on(out)
     .object()
         .array("a")
             .value(1)
             .value(2)
         .end()
         .value("b", false)
         .value("c", true)
     .end()
 .done();
 
  • Field Details

    • appendable

      protected final Appendable appendable
    • out

      protected final OutputStream out
    • utf8

      protected final boolean utf8
  • Method Details

    • done

      public void done() throws JsonWriterException
      Closes this JSON writer and flushes the underlying Appendable if it is also Flushable.
      Throws:
      JsonWriterException - if the underlying Flushable Appendable failed to flush.
    • array

      public JsonAppendableWriter array(Collection<?> c)
      Description copied from interface: JsonSink
      Emits the start of an array.
      Specified by:
      array in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • array

      public JsonAppendableWriter array(String key, Collection<?> c)
      Description copied from interface: JsonSink
      Emits the start of an array with a key.
      Specified by:
      array in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • object

      public JsonAppendableWriter object(Map<?,?> map)
      Description copied from interface: JsonSink
      Emits the start of an object.
      Specified by:
      object in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • object

      public JsonAppendableWriter object(String key, Map<?,?> map)
      Description copied from interface: JsonSink
      Emits the start of an object with a key.
      Specified by:
      object in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • nul

      public JsonAppendableWriter nul()
      Description copied from interface: JsonSink
      Emits a 'null' token.
      Specified by:
      nul in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • nul

      public JsonAppendableWriter nul(String key)
      Description copied from interface: JsonSink
      Emits a 'null' token with a key.
      Specified by:
      nul in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(Object o)
      Description copied from interface: JsonSink
      Emits an object if it is a JSON-compatible type, otherwise throws an exception.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, Object o)
      Description copied from interface: JsonSink
      Emits an object with a key if it is a JSON-compatible type, otherwise throws an exception.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String s)
      Description copied from interface: JsonSink
      Emits a string value (or null).
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(int i)
      Description copied from interface: JsonSink
      Emits an integer value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(long l)
      Description copied from interface: JsonSink
      Emits a long value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(boolean b)
      Description copied from interface: JsonSink
      Emits a boolean value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(double d)
      Description copied from interface: JsonSink
      Emits a double value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(float d)
      Description copied from interface: JsonSink
      Emits a float value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(Number n)
      Description copied from interface: JsonSink
      Emits a Number value.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, String s)
      Description copied from interface: JsonSink
      Emits a string value (or null) with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, int i)
      Description copied from interface: JsonSink
      Emits an integer value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, long l)
      Description copied from interface: JsonSink
      Emits a long value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, boolean b)
      Description copied from interface: JsonSink
      Emits a boolean value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, double d)
      Description copied from interface: JsonSink
      Emits a double value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, float d)
      Description copied from interface: JsonSink
      Emits a float value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • value

      public JsonAppendableWriter value(String key, Number n)
      Description copied from interface: JsonSink
      Emits a Number value with a key.
      Specified by:
      value in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • array

      public JsonAppendableWriter array()
      Description copied from interface: JsonSink
      Starts an array.
      Specified by:
      array in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • object

      public JsonAppendableWriter object()
      Description copied from interface: JsonSink
      Starts an object.
      Specified by:
      object in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • array

      public JsonAppendableWriter array(String key)
      Description copied from interface: JsonSink
      Starts an array within an object, prefixed with a key.
      Specified by:
      array in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • object

      public JsonAppendableWriter object(String key)
      Description copied from interface: JsonSink
      Starts an object within an object, prefixed with a key.
      Specified by:
      object in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • end

      public JsonAppendableWriter end()
      Description copied from interface: JsonSink
      Ends the current array or object.
      Specified by:
      end in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • key

      public JsonAppendableWriter key(String key)
      Description copied from interface: JsonSink
      Writes the key of a key/value pair.
      Specified by:
      key in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • doneInternal

      protected void doneInternal()
      Ensures that the object is in the finished state.
      Throws:
      JsonWriterException - if the written JSON is not properly balanced, ie: all arrays and objects that were started have been properly ended.