Class JsonWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    ContentsJsonWriter

    public class JsonWriter
    extends java.lang.Object
    implements java.io.Flushable, java.io.Closeable
    Converts an object to a JSON formatted string.

    If pretty-printing is enabled, the JsonWriter will add newlines and indentation to the written data. Pretty-printing is disabled by default.

    Created: 2008. 06. 12 PM 8:20:54

    Author:
    Juho Jeong
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonWriter()
      Instantiates a new JsonWriter.
      JsonWriter​(java.io.Writer out)
      Instantiates a new JsonWriter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginArray()
      Begins encoding a new array.
      void beginObject()
      Begins encoding a new object.
      void close()  
      <T extends JsonWriter>
      T
      dateFormat​(java.lang.String dateFormat)  
      <T extends JsonWriter>
      T
      dateTimeFormat​(java.lang.String dateTimeFormat)  
      void endArray()
      Ends encoding the current array.
      void endObject()
      Ends encoding the current object.
      void flush()
      Ensures all buffered data is written to the underlying Writer and flushes that writer.
      <T extends JsonWriter>
      T
      indentString​(java.lang.String indentString)  
      <T extends JsonWriter>
      T
      nullWritable​(boolean nullWritable)  
      <T extends JsonWriter>
      T
      prettyPrint​(boolean prettyPrint)  
      java.lang.String toString()  
      <T extends JsonWriter>
      T
      write​(java.lang.Object object)
      Writes an object to the writer.
      void writeJson​(java.lang.String json)
      Writes a string directly to the writer stream without quoting or escaping.
      void writeName​(java.lang.String name)
      Writes a key name to the writer.
      void writeNull()
      Writes a "null" string to the writer.
      void writeNull​(boolean force)
      Writes a "null" string to the writer.
      void writeValue​(java.lang.Boolean value)
      Writes a Boolean object to the writer.
      void writeValue​(java.lang.Number value)
      Writes a Number object to the writer.
      void writeValue​(java.lang.String value)
      Writes a string to the writer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JsonWriter

        public JsonWriter()
        Instantiates a new JsonWriter. Pretty printing is enabled by default, and the indent string is set to " " (two spaces).
      • JsonWriter

        public JsonWriter​(java.io.Writer out)
        Instantiates a new JsonWriter. Pretty printing is enabled by default, and the indent string is set to " " (two spaces).
        Parameters:
        out - the character-output stream
    • Method Detail

      • prettyPrint

        public <T extends JsonWriter> T prettyPrint​(boolean prettyPrint)
      • indentString

        public <T extends JsonWriter> T indentString​(java.lang.String indentString)
      • dateFormat

        public <T extends JsonWriter> T dateFormat​(java.lang.String dateFormat)
      • dateTimeFormat

        public <T extends JsonWriter> T dateTimeFormat​(java.lang.String dateTimeFormat)
      • nullWritable

        public <T extends JsonWriter> T nullWritable​(boolean nullWritable)
      • write

        public <T extends JsonWriter> T write​(java.lang.Object object)
                                       throws java.io.IOException
        Writes an object to the writer.
        Parameters:
        object - the object to write to the writer.
        Throws:
        java.io.IOException - if an I/O error has occurred.
      • writeName

        public void writeName​(java.lang.String name)
        Writes a key name to the writer.
        Parameters:
        name - the string to write to the writer
      • writeValue

        public void writeValue​(java.lang.String value)
                        throws java.io.IOException
        Writes a string to the writer. If value is null, write a null string ("").
        Parameters:
        value - the string to write to the writer
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeValue

        public void writeValue​(java.lang.Boolean value)
                        throws java.io.IOException
        Writes a Boolean object to the writer.
        Parameters:
        value - a Boolean object to write to the writer
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeValue

        public void writeValue​(java.lang.Number value)
                        throws java.io.IOException
        Writes a Number object to the writer.
        Parameters:
        value - a Number object to write to the writer
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeNull

        public void writeNull()
                       throws java.io.IOException
        Writes a "null" string to the writer.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeNull

        public void writeNull​(boolean force)
                       throws java.io.IOException
        Writes a "null" string to the writer.
        Parameters:
        force - true if forces should be written null value
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeJson

        public void writeJson​(java.lang.String json)
                       throws java.io.IOException
        Writes a string directly to the writer stream without quoting or escaping.
        Parameters:
        json - the string to write to the writer
        Throws:
        java.io.IOException - if an I/O error has occurred
      • beginObject

        public void beginObject()
                         throws java.io.IOException
        Begins encoding a new object.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • endObject

        public void endObject()
                       throws java.io.IOException
        Ends encoding the current object.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • beginArray

        public void beginArray()
                        throws java.io.IOException
        Begins encoding a new array.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • endArray

        public void endArray()
                      throws java.io.IOException
        Ends encoding the current array.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • flush

        public void flush()
                   throws java.io.IOException
        Ensures all buffered data is written to the underlying Writer and flushes that writer.
        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.io.IOException - if an I/O error has occurred
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object