Class JsonWriter

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class JsonWriter
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Writes JSON as a stream. This class allows plugins to not directly depend on the underlying JSON library.

    How to use

       try (JsonWriter jsonWriter = JsonWriter.of(writer)) {
         jsonWriter
           .beginObject()
           .prop("aBoolean", true)
           .prop("aInt", 123)
           .prop("aString", "foo")
           .beginObject().name("aList")
             .beginArray()
               .beginObject().prop("key", "ABC").endObject()
               .beginObject().prop("key", "DEF").endObject()
             .endArray()
           .endObject()
       }
     

    By default, null objects are not serialized. To enable null serialization, use setSerializeNulls(boolean).

    By default, empty strings are serialized. To disable empty string serialization, use setSerializeEmptys(boolean).

    JsonWriter implements AutoCloseable since version 6.3. The method close() closes the underlying writer.

    Since:
    4.2
    • Method Detail

      • of

        public static JsonWriter of​(java.io.Writer writer)
      • setSerializeNulls

        public JsonWriter setSerializeNulls​(boolean b)
      • setSerializeEmptys

        public JsonWriter setSerializeEmptys​(boolean serializeEmptyStrings)
        Enable/disable serialization of properties which value is an empty String.
      • beginArray

        public JsonWriter beginArray()
        Begins encoding a new array. Each call to this method must be paired with a call to endArray(). Output is [.
        Throws:
        WriterException - on any failure
      • endArray

        public JsonWriter endArray()
        Ends encoding the current array. Output is ].
        Throws:
        WriterException - on any failure
      • beginObject

        public JsonWriter beginObject()
        Begins encoding a new object. Each call to this method must be paired with a call to endObject(). Output is {.
        Throws:
        WriterException - on any failure
      • endObject

        public JsonWriter endObject()
        Ends encoding the current object. Output is }.
        Throws:
        WriterException - on any failure
      • name

        public JsonWriter name​(java.lang.String name)
        Encodes the property name. Output is "theName":.
        Throws:
        WriterException - on any failure
      • value

        public JsonWriter value​(boolean value)
        Encodes value. Output is true or false.
        Throws:
        WriterException - on any failure
      • valueObject

        public JsonWriter valueObject​(@Nullable
                                      java.lang.Object value)
        Encodes an object that can be a :
        • primitive types: String, Number, Boolean
        • java.util.Date: encoded as datetime (see valueDateTime(java.util.Date)
        • Map<Object, Object>. Method toString is called for the key.
        • Iterable
        Throws:
        WriterException - on any failure
      • values

        public JsonWriter values​(java.lang.Iterable<java.lang.String> values)
        Write a list of values in an array, for example:
           writer.beginArray().values(myValues).endArray();
         
        Throws:
        WriterException - on any failure
      • valueDateTime

        public JsonWriter valueDateTime​(@Nullable
                                        java.util.Date value)
      • prop

        public JsonWriter prop​(java.lang.String name,
                               @Nullable
                               java.lang.Number value)
        Encodes the property name and value. Output is for example "theName":123.
        Throws:
        WriterException - on any failure
      • propDate

        public JsonWriter propDate​(java.lang.String name,
                                   @Nullable
                                   java.util.Date value)
        Encodes the property name and date value (ISO format). Output is for example "theDate":"2013-01-24".
        Throws:
        WriterException - on any failure
      • propDateTime

        public JsonWriter propDateTime​(java.lang.String name,
                                       @Nullable
                                       java.util.Date value)
        Encodes the property name and datetime value (ISO format). Output is for example "theDate":"2013-01-24T13:12:45+01".
        Throws:
        WriterException - on any failure
      • prop

        public JsonWriter prop​(java.lang.String name,
                               @Nullable
                               java.lang.String value)
        Throws:
        WriterException - on any failure
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        WriterException - on any failure