com.hdcookbook.grin.util
Class JsonIO

java.lang.Object
  extended by com.hdcookbook.grin.util.JsonIO

public class JsonIO
extends java.lang.Object

This contains utility methods to read and write JSON-formatted objects. See http://json.org for the extremely simple syntax. In addition to that syntax, the reader discards whitespace, and comments in the "//", "/*" and "#" form.

JSON has a clear mapping to Java types that we use in this class:

        JSON TYPE  ->  Java Type

            Object -> HashMap
             Array -> Object[]
            String -> String
            Number -> java.lang.Number (subclass determined by value)
        true/false -> Boolean
              null -> null
 
For numbers, the reader will produce Integer, Long or Double; the writer will accept Integer, Long, Float or Double.

Author:
Bill Foote (http://jovial.com)

Method Summary
static java.lang.Object readJSON(java.io.Reader rdr)
          Read a JSON object from rdr.
static void writeJSON(java.io.Writer out, java.lang.Object value)
          Write a JSON object to out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeJSON

public static void writeJSON(java.io.Writer out,
                             java.lang.Object value)
                      throws java.io.IOException
Write a JSON object to out. The argument must correspond to the JSON type as described in the class documentation, one of HashMap, Object[], String, Integer, Long, Float, Double, Boolean or null. See the class documentation for other details of the syntax.

Parameters:
out - The stream to write to. A buffered writer is recommended; a UTF-8 character encoding is common for JSON streams.
Throws:
java.io.IOException - if there is an underlying IO exception, or if value contains an invalid type.
See Also:
JsonIO

readJSON

public static java.lang.Object readJSON(java.io.Reader rdr)
                                 throws java.io.IOException
Read a JSON object from rdr. The result will correspond to the JSON type as described in the class documentation, one of HashMap, Object[], String, Integer, Long, Double, Boolean or null. rdr will be positioned one character after the last character of the JSON value. See the class documentation for other details of the syntax.

Parameters:
rdr - The stream to read from. A buffered reader is recommended; a UTF-8 character encoding is common for JSON streams. The reader must be one where markSupported() returns true, e.g. a BufferedReader.
Throws:
java.io.IOException - if there is an underlying IO exception, a syntax error, or if rdr.markSupported is false.
See Also:
JsonIO