Class JsonStringWriter

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

public final class JsonStringWriter extends Object
JSON writer that emits JSON to a String. Create this class using JsonWriter.string().
 String json = JsonEmitter
     .indent("  ")
     .string()
     .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 String done()
      Completes this JSON writing session and returns the internal representation as a String.
    • array

      public JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter 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 JsonStringWriter array()
      Description copied from interface: JsonSink
      Starts an array.
      Specified by:
      array in interface JsonSink<SELF extends com.grack.nanojson.JsonWriterBase<SELF>>
    • object

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

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