public class Json extends Object
Documents and also serializing BSON Documents into JSON text.
Basic JSON types are parsed as follows:
true or false tokenBooleanElement. { a : true } or { a : false }null tokenNullElement. { a : null }SymbolElement:{ a : b }StringElement:{ a : 'b' } or { a : "b" }. or exponent)IntegerElement if within the range [
Integer.MIN_VALUE, Integer.MAX_VALUE], otherwise a
LongElement. Value is parsed by Long.parseLong(String).{ a : 1234 } or { a : 123456789012 }. or exponent)DoubleElement. Value is parsed by
Double.parseDouble(String).{ a : 1.2 } or { a : 1e12 }In addition to the basic JSON types the parser also supports the following standard MongoDB/BSON extensions:
BinaryElement. The first field is the sub-type,
normally zero. The second field is the base64 encoded binary value: { a : BinData(0, "VVU=") } or
{ a : { $binary:"VVU=", $type:0 } }{ a : { $binary:"VVU=", $type: '0x00' } }
{ a : { $binary:"VVU=", $type: '00' } }
BinaryElement. The first field is the sub-type,
normally zero. The second field is the hex encoded binary value: { a : HexData(0, "cafe") }TimestampElement: { a : ISODate("2012-07-14T01:00:00.000") } or
{ a : { $date : "2012-07-14T01:00:00.000" } } or
{ a : { $date : 1234567890 } }MaxKeyElement: { a : MaxKey } or { a : MaxKey() }MinKeyElement: { a : MinKey } or { a : MinKey() }LongElement: { a : NumberLong("123456789") }ObjectIdElement. The string is the hex encoding of the
128 bit value: { a : ObjectId("4e9d87aa5825b60b637815a6") } or
{ a : { $oid : "4e9d87aa5825b60b637815a6" } }RegularExpressionElement: { a : { $regex : 'cat' , $options : 'i' } }MongoTimestampElement. The first value is the seconds
since the UNIX epoch. The second value is an ordinal: { a : Timestamp(0,0) } or
{ a : { $timestamp : { t : 0, i : 0 } } }The following non-standard extensions are also provided. These extensions may be deprecated in future releases if standard extensions are created:
DBPointerElement:{ a : DBPointer("db", 'collection', ObjectId("4e9d87aa5825b60b637815a6") ) }
DBRef convention
Note: Currently serialization/parsing round trip is not supported for
the following Element types:
| Modifier and Type | Method and Description |
|---|---|
static Document |
parse(Reader input)
Parses the document from the reader into a BSON
Document. |
static Document |
parse(String input)
Parses the document from the reader into a BSON
Document. |
static String |
serialize(DocumentAssignable document)
Serializes the
Document to an equivalent JSON document. |
static void |
serialize(DocumentAssignable document,
Writer sink)
Serializes the
Document to an equivalent JSON document. |
public static Document parse(Reader input) throws JsonParseException
Document.
See the class documentation for important limitations on round trip serialization and parsing.
input - The source of the document to read.Document representation of the JSON document.JsonParseException - On a failure to parse the JSON document.public static Document parse(String input) throws JsonParseException
Document.
This method is equivalent to:
parse(new StringReader(input));
See the class documentation for important limitations on round trip serialization and parsing.
input - The source of the document to read.Document representation of the JSON document.JsonParseException - On a failure to parse the JSON document.public static String serialize(DocumentAssignable document) throws JsonException
Document to an equivalent JSON document.
See the class documentation for important limitations on round trip serialization and parsing.
document - The document to conver to a JSON document.JsonException - On a failure to write the JSON document.public static void serialize(DocumentAssignable document, Writer sink) throws JsonException
Document to an equivalent JSON document.
See the class documentation for important limitations on round trip serialization and parsing.
document - The document to conver to a JSON document.sink - The sink for the JSON document text.JsonException - On a failure to write the JSON document.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.