Interface JSONWriter

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Closeable, java.io.Flushable

    public interface JSONWriter
    extends java.io.Flushable, java.io.Closeable
    Writes JSON content to a character stream.

    Examples

    Writing a basic object

     final JSONStreams json = ...; // acquire the JSONStreams service
     final Appendable output = ...; // create a stream to write to
     ...
     final JSONWriter writer = json.jsonWriter(output);
     
     writer.startObject().property("foo","bar").endObject(); //writes: {"foo":"bar"}
     

    Writing a nested object

     final JSONStreams json = ...; // acquire the JSONStreams service
     final Appendable output = ...; // create a stream to write to
     ...
     final JSONWriter writer = json.jsonWriter(output);
     
     writer.startObject()
           .propertyName("nested")
            .startObject()
             .property("foo","bar")
            .endObject()
           .endObject(); //writes: {"nested":{"foo":"bar"}}
     

    Writing an array value

     final JSONStreams json = ...; // acquire the JSONStreams service
     final Appendable output = ...; // create a stream to write to
     ...
     final JSONWriter writer = json.jsonWriter(output);
     
     writer.startObject()
           .propertyName("items")
            .startArray()
             .value("first")
             .value("second")
             .nullValue()
            .endObject()
           .endObject(); //writes: {"items":["first","second",null]}
     
    Author:
    cdivilly
  • <section role="region">
    • Method Detail

      • propertyName

        JSONWriter propertyName​(java.lang.String name)
                         throws JSONIOException
        Emit the name of a property
        Parameters:
        name - The name of the property
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
      • property

        JSONWriter property​(java.lang.String name,
                            java.lang.CharSequence value)
                     throws JSONIOException
        Emit a property with a textual value.
        Parameters:
        name - Name of the property
        value - The value of the property
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
      • value

        JSONWriter value​(java.lang.Boolean value)
                  throws JSONIOException
        Emit a boolean value, mapping it to the unquoted values: true or false
        Parameters:
        value - boolean value
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
      • value

        JSONWriter value​(java.lang.CharSequence value)
                  throws JSONIOException
        Emit a textual value, quoting and escaping the string
        Parameters:
        value - textual value
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
      • value

        JSONWriter value​(java.lang.Number value)
                  throws JSONIOException
        Emit a numeric value. Note that the special Double values:
        • Double.NaN
        • Double.POSITIVE_INFINITY
        • Double.NEGATIVE_INFINITY
        are quoted, because the JSON syntax does not define any equivalent values.
        Parameters:
        value - Numeric value
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
      • value

        JSONWriter value​(java.lang.Readable value)
                  throws JSONIOException
        Emit a textual value, quoting and escaping the string.
        Parameters:
        value - textual value
        Returns:
        self
        Throws:
        JSONIOException - if an IO error occurs
    </section>