ModeShape Distribution 3.2.0.Final

org.infinispan.schematic.document
Class Json

java.lang.Object
  extended by org.infinispan.schematic.document.Json

public class Json
extends Object

A utility class for working with JSON documents. This class is able to read and write JSON documents that are in a special modified format.

Modified Format

Any JSON document written in this modified format is still a valid JSON document. However, certain value types supported by BSON are written as nested objects in particular patterns. In fact, it is nearly identical to the JSON serialization used by MongoDB. All standard JSON values are written as expected, but the types unique to BSON are written as follows:

BSON Type Class Format Example
Symbol Symbol "value" "The quick brown fox"
Regular Expression Pattern { "$regex" : "pattern", "$otions" : "flags" } { "$regex" : "[CH]at\sin", "$options" : "im" }
Date Date { "$date" : "yyyy-MM-ddTHH:mm:ssZ" } { "$date" : "2011-06-11T08:44:25Z" }
Timestamp Timestamp { "$ts" : timeValue, "$inc" : incValue } "\/TS("2011-06-11T08:44:25Z")\/"
ObjectId ObjectId { "$oid" : "12bytesOfIdInBase16" } { "$oid" : "0000012c0000c8000900000f" }
Binary Binary { "$type" : typeAsInt, "$base64" : "bytesInBase64" }" { "$type" : 0, "$base64" : "TWFuIGlzIGRpc3R" }"
UUID UUID { "$uuid" : "string-form-of-uuid" } { "$uuid" : "09e0e949-bba4-459c-bb1d-9352e5ee8958" }
Code Code { "$code" : "code" } { "$code" : "244-I2" }
CodeWithScope CodeWithScope { "$code" : "code", "$scope" : scope document } { "$code" : "244-I2", "$scope" : { "name" : "Joe" } }
MinKey MinKey "MinKey" "MinKey"
MaxKey MaxKey "MaxKey" "MaxKey"
Null value n/a null null

Since:
5.1

Nested Class Summary
 class Json.ReservedField
          A set of field names that are reserved for special formatting of non-standard JSON value types as nested objects.
 
Constructor Summary
Json()
           
 
Method Summary
protected static JsonWriter getCompactJsonWriter()
           
protected static JsonWriter getPrettyWriter()
           
protected static JsonReader getReader()
           
static Document read(InputStream stream)
          Read the JSON representation from supplied input stream and construct the Document representation, using the default character set.
static Document read(InputStream stream, Charset charset)
          Read the JSON representation from supplied input stream and construct the Document representation, using the supplied character set.
static Document read(Reader reader)
          Read the JSON representation from supplied input stream and construct the Document representation.
static Document read(String json)
          Read the supplied JSON representation and construct the Document representation.
static Document read(URL url)
          Read the JSON representation from supplied URL and construct the Document representation, using the default character set.
static DocumentSequence readMultiple(InputStream stream)
          Return a DocumentSequence that can be used to pull multiple documents from the stream.
static DocumentSequence readMultiple(Reader reader)
          Return a DocumentSequence that can be used to pull multiple documents from the stream.
static String write(Document bson)
          Return the modified JSON representation for the supplied in-memory Document.
static void write(Document bson, OutputStream stream)
          Write to the supplied writer the modified JSON representation of the supplied in-memory Document.
static void write(Document bson, Writer writer)
          Write to the supplied writer the modified JSON representation of the supplied in-memory Document.
static String write(Object value)
          Return the modified JSON representation for the supplied object value.
static String writePretty(Document bson)
          Return the modified JSON representation for the supplied in-memory Document.
static void writePretty(Document bson, OutputStream stream)
          Write to the supplied writer the modified JSON representation of the supplied in-memory Document.
static void writePretty(Document bson, Writer writer)
          Write to the supplied writer the modified JSON representation of the supplied in-memory Document.
static String writePretty(Object value)
          Return the modified JSON representation for the supplied object value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Json

public Json()
Method Detail

getCompactJsonWriter

protected static JsonWriter getCompactJsonWriter()

getPrettyWriter

protected static JsonWriter getPrettyWriter()

getReader

protected static JsonReader getReader()

read

public static Document read(URL url)
                     throws ParsingException
Read the JSON representation from supplied URL and construct the Document representation, using the default character set.

This method will read standard JSON and modified JSON, and tolerates whitespace and use of several delimeters, including the standard ':' as well as '=' and '=>'.

Parameters:
url - the URL of the JSON document; may not be null and must be resolvable
Returns:
the in-memory Document representation
Throws:
ParsingException - if there was a problem reading from the stream

read

public static Document read(InputStream stream)
                     throws ParsingException
Read the JSON representation from supplied input stream and construct the Document representation, using the default character set.

This method will read standard JSON and modified JSON, and tolerates whitespace and use of several delimeters, including the standard ':' as well as '=' and '=>'.

Parameters:
stream - the input stream; may not be null
Returns:
the in-memory Document representation
Throws:
ParsingException - if there was a problem reading from the stream

read

public static Document read(InputStream stream,
                            Charset charset)
                     throws ParsingException
Read the JSON representation from supplied input stream and construct the Document representation, using the supplied character set.

This method will read standard JSON and modified JSON, and tolerates whitespace and use of several delimeters, including the standard ':' as well as '=' and '=>'.

Parameters:
stream - the input stream; may not be null
charset - the character set that should be used; may not be null
Returns:
the in-memory Document representation
Throws:
ParsingException - if there was a problem reading from the stream

read

public static Document read(Reader reader)
                     throws ParsingException
Read the JSON representation from supplied input stream and construct the Document representation.

This method will read standard JSON and modified JSON, and tolerates whitespace and use of several delimeters, including the standard ':' as well as '=' and '=>'.

Parameters:
reader - the IO reader; may not be null
Returns:
the in-memory Document representation
Throws:
ParsingException - if there was a problem reading from the stream

read

public static Document read(String json)
                     throws ParsingException
Read the supplied JSON representation and construct the Document representation.

This method will read standard JSON and modified JSON, and tolerates whitespace and use of several delimeters, including the standard ':' as well as '=' and '=>'.

Parameters:
json - the JSON document string; may not be null
Returns:
the in-memory Document representation
Throws:
ParsingException - if there was a problem reading from the stream

readMultiple

public static DocumentSequence readMultiple(InputStream stream)
Return a DocumentSequence that can be used to pull multiple documents from the stream.

Parameters:
stream - the input stream; may not be null
Returns:
the sequence that can be used to get one or more Document instances from a single input

readMultiple

public static DocumentSequence readMultiple(Reader reader)
Return a DocumentSequence that can be used to pull multiple documents from the stream.

Parameters:
reader - the IO reader; may not be null
Returns:
the sequence that can be used to get one or more Document instances from a single input

write

public static String write(Document bson)
Return the modified JSON representation for the supplied in-memory Document. The resulting JSON will have no embedded line feeds or extra spaces.

This format is compact and easy for software to read, but usually very difficult for people to read anything but very small documents.

Parameters:
bson - the BSON object or BSON value; may not be null
Returns:
the string; may not be null

write

public static String write(Object value)
Return the modified JSON representation for the supplied object value. The resulting JSON will have no embedded line feeds or extra spaces.

This format is compact and easy for software to read, but usually very difficult for people to read anything but very small documents.

Parameters:
value - the BSON object or BSON value; may not be null
Returns:
the string; may not be null

write

public static void write(Document bson,
                         Writer writer)
                  throws IOException
Write to the supplied writer the modified JSON representation of the supplied in-memory Document. The resulting JSON will have no embedded line feeds or extra spaces.

This format is compact and easy for software to read, but usually very difficult for people to read anything but very small documents.

Parameters:
bson - the BSON object or BSON value; may not be null
writer - the writer; may not be null
Throws:
IOException - if there was a problem reading from the stream

write

public static void write(Document bson,
                         OutputStream stream)
                  throws IOException
Write to the supplied writer the modified JSON representation of the supplied in-memory Document. The resulting JSON will have no embedded line feeds or extra spaces.

This format is compact and easy for software to read, but usually very difficult for people to read anything but very small documents.

Parameters:
bson - the BSON object or BSON value; may not be null
stream - the output stream; may not be null
Throws:
IOException - if there was a problem reading from the stream

writePretty

public static String writePretty(Document bson)
Return the modified JSON representation for the supplied in-memory Document. The resulting JSON will be indented for each name/value pair and each array value.

This format is very readable by people and software, but is less compact due to the extra whitespace.

Parameters:
bson - the BSON object or BSON value; may not be null
Returns:
the JSON representation; never null

writePretty

public static String writePretty(Object value)
Return the modified JSON representation for the supplied object value. The resulting JSON will be indented for each name/value pair and each array value.

This format is very readable by people and software, but is less compact due to the extra whitespace.

Parameters:
value - the BSON object or BSON value; may not be null
Returns:
the JSON representation; never null

writePretty

public static void writePretty(Document bson,
                               Writer writer)
                        throws IOException
Write to the supplied writer the modified JSON representation of the supplied in-memory Document. The resulting JSON will be indented for each name/value pair and each array value.

This format is very readable by people and software, but is less compact due to the extra whitespace.

Parameters:
bson - the BSON object or BSON value; may not be null
writer - the writer; may not be null
Throws:
IOException - if there was a problem reading from the stream

writePretty

public static void writePretty(Document bson,
                               OutputStream stream)
                        throws IOException
Write to the supplied writer the modified JSON representation of the supplied in-memory Document. The resulting JSON will be indented for each name/value pair and each array value.

This format is very readable by people and software, but is less compact due to the extra whitespace.

Parameters:
bson - the BSON object or BSON value; may not be null
stream - the output stream; may not be null
Throws:
IOException - if there was a problem reading from the stream

ModeShape Distribution 3.2.0.Final

Copyright © 2008-2013 JBoss, a division of Red Hat. All Rights Reserved.