public class JsonSerializationVisitor extends Object implements Visitor
Modifier and Type | Field and Description |
---|---|
static String |
NL
The platforms new line string.
|
static Pattern |
SYMBOL_PATTERN
A pattern to detect valid "symbol" names.
|
static TimeZone |
UTC
The default time zone.
|
Constructor and Description |
---|
JsonSerializationVisitor(Writer sink,
boolean oneLine)
Creates a new JsonSerializationVisitor.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
isSuppressNames()
Returns if the visitor is currently suppressing the names of elements.
|
protected void |
nl()
Writes a new line if
myOneLine is false and indents to the
myIndentLevel . |
protected void |
setSuppressNames(boolean suppressNames)
Sets the value for if the visitor is currently suppressing the names of
elements.
|
void |
visit(List<Element> elements)
Visit the root document.
|
void |
visitArray(String name,
List<Element> elements)
Visits an array of elements.
|
void |
visitBinary(String name,
byte subType,
byte[] data)
Visits a binary element.
|
void |
visitBoolean(String name,
boolean value)
Visits a boolean element.
|
void |
visitDBPointer(String name,
String databaseName,
String collectionName,
ObjectId id)
Visits a deprecated DBPointer element.
|
void |
visitDocument(String name,
List<Element> elements)
Visits a sub-document element.
|
void |
visitDouble(String name,
double value)
Visits a double element.
|
void |
visitInteger(String name,
int value)
Visits a integer (32-bit signed) element.
|
void |
visitJavaScript(String name,
String code)
Visits a JavaScript element.
|
void |
visitJavaScript(String name,
String code,
Document scope)
Visits a JavaScript with Scope element.
|
void |
visitLong(String name,
long value)
Visits a long (64-bit signed) element.
|
void |
visitMaxKey(String name)
Visits a minimum key value element.
|
void |
visitMinKey(String name)
Visits a minimum key value element.
|
void |
visitMongoTimestamp(String name,
long value)
Visits a MongoDB Timestamp element.
|
void |
visitNull(String name)
Visits a
null valued element. |
void |
visitObjectId(String name,
ObjectId id)
Visits an ObjectId element.
|
void |
visitRegularExpression(String name,
String pattern,
String options)
Visits a regular expression element.
|
void |
visitString(String name,
String value)
Visits a string element.
|
void |
visitSymbol(String name,
String symbol)
Visits a symbol element.
|
void |
visitTimestamp(String name,
long timestamp)
Visits a timestamp element.
|
protected void |
writeName(String name)
Writes the name if
mySuppressNames is false. |
protected void |
writeObjectId(ObjectId id)
Writes the
ObjectId . |
protected void |
writeQuotedString(String string)
Writes the
string as a quoted string. |
public static final String NL
public static final Pattern SYMBOL_PATTERN
public static final TimeZone UTC
public JsonSerializationVisitor(Writer sink, boolean oneLine)
sink
- The Writer to write to.oneLine
- If true then the visitor will write the document to 1 line,
otherwise the visitor will write the document accross multiple
lines with indenting.public void visit(List<Element> elements)
Implementations of Document
may see a significant performance
enhancement if they ensure that the list of elements is the same list.
(Identify check instead of Object.equals(Object)
.
Overridden to create a JSON representation of the document's elements to the writer provided when this object was created.
public void visitArray(String name, List<Element> elements)
The ArrayElement
implementation ensures that the list of elements
is always the same list. Visitors may use this fact to cache intermediate
results.
Overridden to append a JSON representation of the array's elements to the writer provided when this object was created.
visitArray
in interface Visitor
name
- The name of the element.elements
- The elements in the array.public void visitBinary(String name, byte subType, byte[] data)
Overridden to append a JSON representation of the binary element to the writer provided when this object was created. This method generates the MongoDB standard BinData(...) JSON extension.
visitBinary
in interface Visitor
name
- The name of the element.subType
- The binary data sub type.data
- The binary data.public void visitBoolean(String name, boolean value)
Overridden to append a JSON representation of the boolean element to the writer provided when this object was created.
visitBoolean
in interface Visitor
name
- The name of the element.value
- The boolean value.public void visitDBPointer(String name, String databaseName, String collectionName, ObjectId id)
Overridden to append a JSON representation of the DBPointer element to the writer provided when this object was created. This method generates the non-standard DBPointer(...) JSON extension.
visitDBPointer
in interface Visitor
name
- The name of the element.databaseName
- The name of the database containing the document.collectionName
- The name of the collection containing the document.id
- The id for the document.public void visitDocument(String name, List<Element> elements)
The DocumentElement
implementation ensures that the list of
elements is always the same list. Visitors may use this fact to cache
intermediate results.
Overridden to append a JSON representation of the sub-document element to the writer provided when this object was created.
visitDocument
in interface Visitor
name
- The name of the element.elements
- The sub elements of the document.public void visitDouble(String name, double value)
Overridden to append a JSON representation of the double element to the writer provided when this object was created.
visitDouble
in interface Visitor
name
- The name of the element.value
- The double value.public void visitInteger(String name, int value)
Overridden to append a JSON representation of the integer element to the writer provided when this object was created.
visitInteger
in interface Visitor
name
- The name of the element.value
- The integer value.public void visitJavaScript(String name, String code)
Overridden to append a JSON representation of the JavaScript element to
the writer provided when this object was created. This method writes the
elements as a { $code : <code> }
sub-document.
visitJavaScript
in interface Visitor
name
- The name of the element.code
- The java script code.public void visitJavaScript(String name, String code, Document scope)
Overridden to append a JSON representation of the JavaScript element to
the writer provided when this object was created. This method writes the
elements as a
{ $code : <code>, $scope : <scope> }
sub-document.
visitJavaScript
in interface Visitor
name
- The name of the element.code
- The java script code.scope
- The scope for the JacaScript code.public void visitLong(String name, long value)
Overridden to append a JSON representation of the binary element to the writer provided when this object was created. This method generates the MongoDB standard NumberLong(...) JSON extension.
public void visitMaxKey(String name)
Overridden to append a JSON representation of the DBPointer element to the writer provided when this object was created. This method generates the non-standard MaxKey() JSON extension.
visitMaxKey
in interface Visitor
name
- The name of the element.public void visitMinKey(String name)
Overridden to append a JSON representation of the DBPointer element to the writer provided when this object was created. This method generates the non-standard MinKey() JSON extension.
visitMinKey
in interface Visitor
name
- The name of the element.public void visitMongoTimestamp(String name, long value)
Overridden to append a JSON representation of the binary element to the writer provided when this object was created. This method generates the MongoDB standard Timestamp(...) JSON extension.
visitMongoTimestamp
in interface Visitor
name
- The name of the element.value
- The mongoDB timstamp value.public void visitNull(String name)
null
valued element.
Overridden to append a JSON representation of the null element to the writer provided when this object was created.
public void visitObjectId(String name, ObjectId id)
Overridden to append a JSON representation of the binary element to the writer provided when this object was created. This method generates the MongoDB standard ObjectId(...) JSON extension.
visitObjectId
in interface Visitor
name
- The name of the element.id
- The object id.public void visitRegularExpression(String name, String pattern, String options)
Overridden to append a JSON representation of the JavaScript element to
the writer provided when this object was created. This method writes the
elements as a
{ $regex : <pattern>, $options : <options> }
sub-document.
visitRegularExpression
in interface Visitor
name
- The name of the element.pattern
- The pattern for the regular expression.options
- The regular expression options. See the BSON specification for
details.public void visitString(String name, String value)
Overridden to append a JSON representation of the string element to the writer provided when this object was created.
visitString
in interface Visitor
name
- The name of the element.value
- The string value.public void visitSymbol(String name, String symbol)
Overridden to append a JSON representation of the symbol element to the writer provided when this object was created.
visitSymbol
in interface Visitor
name
- The name of the element.symbol
- The symbol value.public void visitTimestamp(String name, long timestamp)
Overridden to append a JSON representation of the binary element to the writer provided when this object was created. This method generates the MongoDB standard ISODate(...) JSON extension.
visitTimestamp
in interface Visitor
name
- The name of the element.timestamp
- The number of milliseconds since the Unix epoch.protected boolean isSuppressNames()
protected void nl() throws IOException
myOneLine
is false and indents to the
myIndentLevel
.IOException
- On a failure to write the new line.protected void setSuppressNames(boolean suppressNames)
suppressNames
- The new value for if names should be suppressed.protected void writeName(String name) throws IOException
mySuppressNames
is false.name
- The name to write, if not suppressed.IOException
- On a failure to write the new line.protected void writeObjectId(ObjectId id) throws IOException
ObjectId
.id
- The ObjectId
to write.IOException
- On a failure writing to the sink.protected void writeQuotedString(String string) throws IOException
string
as a quoted string.string
- The String to write.IOException
- On a failure writing the String.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.