Class JsonWriter

  • All Implemented Interfaces:
    java.io.Flushable
    Direct Known Subclasses:
    ContentsJsonWriter

    public class JsonWriter
    extends java.lang.Object
    implements java.io.Flushable
    Converts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. Pretty-printing is disabled by default. The default indentation string is a tab character.

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

    Author:
    Juho Jeong
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonWriter​(java.io.Writer writer)
      Instantiates a new JsonWriter.
      JsonWriter​(java.io.Writer writer, boolean prettyPrint)
      Instantiates a new JsonWriter.
      JsonWriter​(java.io.Writer writer, java.lang.String indentString)
      Instantiates a new JsonWriter.
    • Constructor Detail

      • JsonWriter

        public JsonWriter​(java.io.Writer writer)
        Instantiates a new JsonWriter. Pretty-printing is disabled by default.
        Parameters:
        writer - the character-output stream
      • JsonWriter

        public JsonWriter​(java.io.Writer writer,
                          boolean prettyPrint)
        Instantiates a new JsonWriter. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. The default indentation string is a tab character.
        Parameters:
        writer - the character-output stream
        prettyPrint - enables or disables pretty-printing
      • JsonWriter

        public JsonWriter​(java.io.Writer writer,
                          java.lang.String indentString)
        Instantiates a new JsonWriter. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable.
        Parameters:
        writer - the character-output stream
        indentString - the string that should be used for indentation when pretty-printing is enabled
    • Method Detail

      • write

        public JsonWriter write​(java.lang.Object object)
                         throws java.io.IOException
        Write an object to a character stream.
        Parameters:
        object - the object to write to a character-output stream.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred.
      • indent

        protected void indent()
                       throws java.io.IOException
        Write a tab character to a character stream.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeName

        public JsonWriter writeName​(java.lang.String name)
                             throws java.io.IOException
        Writes a key name to a character stream.
        Parameters:
        name - the string to write to a character-output stream
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeValue

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

        public JsonWriter writeValue​(java.lang.Boolean value)
                              throws java.io.IOException
        Writes a Boolean object to a character stream.
        Parameters:
        value - a Boolean object to write to a character-output stream
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeValue

        public JsonWriter writeValue​(java.lang.Number value)
                              throws java.io.IOException
        Writes a Number object to a character stream.
        Parameters:
        value - a Number object to write to a character-output stream
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeNull

        public JsonWriter writeNull()
                             throws java.io.IOException
        Write a string "null" to a character stream.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • writeComma

        public JsonWriter writeComma()
                              throws java.io.IOException
        Write a comma character to a character stream.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • nextLine

        protected void nextLine()
                         throws java.io.IOException
        Write a new line character to a character stream.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • openCurlyBracket

        public JsonWriter openCurlyBracket()
                                    throws java.io.IOException
        Open a single curly bracket.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • closeCurlyBracket

        public JsonWriter closeCurlyBracket()
                                     throws java.io.IOException
        Close the open curly bracket.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • openSquareBracket

        public JsonWriter openSquareBracket()
                                     throws java.io.IOException
        Open a single square bracket.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • closeSquareBracket

        public JsonWriter closeSquareBracket()
                                      throws java.io.IOException
        Close the open square bracket.
        Returns:
        this JsonWriter
        Throws:
        java.io.IOException - if an I/O error has occurred
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes the writer.
        Throws:
        java.io.IOException - if an I/O error has occurred
      • stringify

        public static java.lang.String stringify​(java.lang.Object object)
                                          throws java.io.IOException
        Converts an object to a JSON formatted string. Pretty-printing is disabled by default.
        Parameters:
        object - an object to convert to a JSON formatted string
        Returns:
        the JSON formatted string
        Throws:
        java.io.IOException - if an I/O error has occurred
      • stringify

        public static java.lang.String stringify​(java.lang.Object object,
                                                 boolean prettyPrint)
                                          throws java.io.IOException
        Converts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable. The default indentation string is a tab character.
        Parameters:
        object - an object to convert to a JSON formatted string
        prettyPrint - enables or disables pretty-printing
        Returns:
        the JSON formatted string
        Throws:
        java.io.IOException - if an I/O error has occurred
      • stringify

        public static java.lang.String stringify​(java.lang.Object object,
                                                 java.lang.String indentString)
                                          throws java.io.IOException
        Converts an object to a JSON formatted string. If pretty-printing is enabled, includes spaces, tabs and new-lines to make the format more readable.
        Parameters:
        object - an object to convert to a JSON formatted string
        indentString - the string that should be used for indentation when pretty-printing is enabled
        Returns:
        the JSON formatted string
        Throws:
        java.io.IOException - if an I/O error has occurred