Package de.undercouch.bson4jackson
Class BsonGenerator
java.lang.Object
tools.jackson.core.JsonGenerator
tools.jackson.core.base.GeneratorBase
de.undercouch.bson4jackson.BsonGenerator
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,tools.jackson.core.Versioned
public class BsonGenerator
extends tools.jackson.core.base.GeneratorBase
Writes BSON code to the provided output stream
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA structure describing the document currently being generatedstatic enumDefines toggable features -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intBit flag composed of bits that indicate whichBsonGenerator.Features are enabled.protected final DynamicOutputBufferSince a BSON document's header must include the size of the whole document in bytes, we have to buffer the whole document first, before we can write it to the output stream.protected tools.jackson.core.io.CharacterEscapesCustom character escapes to use when writing strings (field names and string values)protected BsonGenerator.DocumentInfoSaves information about documents (the main document and embedded ones)protected final OutputStreamThe output stream to write toprotected int[]Escape codes to use when writing strings (only valid of_characterEscapesis not null, seesetCharacterEscapes(CharacterEscapes))protected intSaves the position of the type marker for the object currently begin writtenprotected tools.jackson.core.json.JsonWriteContextThe write context used for tracking object/array nestingprotected booleanIndicates that the next object to be encountered is actually embedded inside a value, and not a complete value itself.Fields inherited from class tools.jackson.core.base.GeneratorBase
_closed, _ioContext, _objectWriteContext, _streamWriteConstraints, _streamWriteFeatures, DEFAULT_BINARY_WRITE_CAPABILITIES, DEFAULT_TEXTUAL_WRITE_CAPABILITIES, DEFAULT_WRITE_CAPABILITIES, MAX_BIG_DECIMAL_SCALE, SURR1_FIRST, SURR1_LAST, SURR2_FIRST, SURR2_LAST, WRITE_BINARY, WRITE_BOOLEAN, WRITE_NULL, WRITE_NUMBER, WRITE_RAW, WRITE_STRING -
Constructor Summary
ConstructorsConstructorDescriptionBsonGenerator(tools.jackson.core.ObjectWriteContext writeCtxt, tools.jackson.core.io.IOContext ctxt, int jsonFeatures, int bsonFeatures, OutputStream out) Creates a new generator -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidprotected void_verifyValueWrite(String typeMsg) protected voidIf the generator is currently processing an array, this method writes the field name of the current element (which is just the position of the element in the array)protected int_writeCString(String string) Write a BSON cstring structure (a null terminated string)protected void_writeFieldName(String name) protected void_writeStartObject(boolean array) Creates a new embedded document or arrayprotected int_writeString(String string) Write a BSON string structure (a null terminated string prependend by the length of the string)voidprotected StringescapeCharacters(String string) Escapes the given string according to_characterEscapes.protected StringflagsToRegexOptions(int flags) Converts a a Java flags word into a BSON options patternvoidflush()protected voidTries to flush the output buffer if streaming is enabled.protected intRetrieves and then increases the current position in the array currently being generatedtools.jackson.core.io.CharacterEscapesprotected booleanisArray()protected booleanChecks if a generator feature is enabledprotected voidputHeader(int pos) Writes the BSON document header to the output buffer at the given position.protected voidReserves bytes for the BSON document headertools.jackson.core.JsonGeneratorsetCharacterEscapes(tools.jackson.core.io.CharacterEscapes esc) tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamWriteCapability>tools.jackson.core.TokenStreamContextinttools.jackson.core.Versionversion()tools.jackson.core.JsonGeneratorwriteBinary(tools.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len) tools.jackson.core.JsonGeneratorwriteBinary(tools.jackson.core.Base64Variant b64variant, byte subType, byte[] data, int offset, int len) Similar towriteBinary(Base64Variant, byte[], int, int), but with the possibility to specify a binary subtype (seeBsonConstants).tools.jackson.core.JsonGeneratorwriteBoolean(boolean state) tools.jackson.core.JsonGeneratorwriteDateTime(Date date) Write a BSON date timetools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorprotected voidtools.jackson.core.JsonGeneratorwriteJavaScript(JavaScript javaScript, tools.jackson.databind.SerializationContext ctxt) Write a BSON JavaScript objecttools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorwriteNumber(double d) tools.jackson.core.JsonGeneratorwriteNumber(float f) tools.jackson.core.JsonGeneratorwriteNumber(int v) tools.jackson.core.JsonGeneratorwriteNumber(long v) tools.jackson.core.JsonGeneratorwriteNumber(short v) tools.jackson.core.JsonGeneratorwriteNumber(String encodedValue) tools.jackson.core.JsonGeneratorwriteNumber(BigDecimal dec) tools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorwriteObjectId(ObjectId objectId) Write a BSON ObjectIdtools.jackson.core.JsonGeneratorwritePropertyId(long id) tools.jackson.core.JsonGeneratorwriteRaw(char c) tools.jackson.core.JsonGeneratorwriteRaw(char[] text, int offset, int len) tools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorwriteRawUTF8String(byte[] text, int offset, int length) tools.jackson.core.JsonGeneratorwriteRegex(Pattern pattern) Write a BSON regextools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorwriteStartArray(Object currentValue) tools.jackson.core.JsonGeneratortools.jackson.core.JsonGeneratorwriteStartObject(Object currentValue) tools.jackson.core.JsonGeneratorwriteString(char[] text, int offset, int len) tools.jackson.core.JsonGeneratorwriteString(String text) tools.jackson.core.JsonGeneratorwriteSymbol(Symbol symbol) Write a BSON Symbol objecttools.jackson.core.JsonGeneratorwriteTimestamp(Timestamp timestamp) Write a MongoDB timestamptools.jackson.core.JsonGeneratorwriteUTF8String(byte[] text, int offset, int length) Methods inherited from class tools.jackson.core.base.GeneratorBase
_asString, _checkRangeBoundsForByteArray, _checkRangeBoundsForCharArray, _checkRangeBoundsForString, _constructDefaultPrettyPrinter, _decodeSurrogate, _throwInternal, close, configure, has, ioContext, isClosed, isEnabled, objectWriteContext, streamWriteConstraints, streamWriteFeatures, writeBinary, writeName, writePOJO, writeRawValue, writeRawValue, writeRawValue, writeRawValue, writeStartArray, writeStartObject, writeString, writeString, writeTreeMethods inherited from class tools.jackson.core.JsonGenerator
_constructWriteException, _constructWriteException, _constructWriteException, _constructWriteException, _copyCurrentContents, _copyCurrentContentsExact, _copyCurrentFloatValue, _copyCurrentFloatValueExact, _copyCurrentIntValue, _copyCurrentStringValue, _reportArgumentError, _reportError, _reportUnsupportedOperation, _reportUnsupportedOperation, _verifyOffsets, _wrapIOFailure, _writeTypePrefixUsingNative, _writeTypePrefixUsingWrapper, canOmitProperties, canWriteObjectId, canWriteTypeId, copyCurrentEvent, copyCurrentEventExact, copyCurrentStructure, copyCurrentStructureExact, getHighestNonEscapedChar, getPrettyPrinter, getSchema, writeArray, writeArray, writeArray, writeArray, writeArrayPropertyStart, writeBinary, writeBinary, writeBinary, writeBinaryProperty, writeBooleanProperty, writeEmbeddedObject, writeNullProperty, writeNumber, writeNumberProperty, writeNumberProperty, writeNumberProperty, writeNumberProperty, writeNumberProperty, writeNumberProperty, writeNumberProperty, writeObjectId, writeObjectPropertyStart, writeObjectRef, writeOmittedProperty, writePOJOProperty, writeRaw, writeStringProperty, writeTypeId, writeTypePrefix, writeTypeSuffix
-
Field Details
-
_bsonFeatures
protected final int _bsonFeaturesBit flag composed of bits that indicate whichBsonGenerator.Features are enabled. -
_out
The output stream to write to -
_buffer
Since a BSON document's header must include the size of the whole document in bytes, we have to buffer the whole document first, before we can write it to the output stream. BSON specifies LITTLE_ENDIAN for all tokens. -
_typeMarker
protected int _typeMarkerSaves the position of the type marker for the object currently begin written -
_currentDocument
Saves information about documents (the main document and embedded ones) -
nextObjectIsEmbeddedInValue
protected boolean nextObjectIsEmbeddedInValueIndicates that the next object to be encountered is actually embedded inside a value, and not a complete value itself. This causes things like context validation and writing out the type to be skipped. -
_writeContext
protected tools.jackson.core.json.JsonWriteContext _writeContextThe write context used for tracking object/array nesting -
_characterEscapes
protected tools.jackson.core.io.CharacterEscapes _characterEscapesCustom character escapes to use when writing strings (field names and string values) -
_outputEscapes
protected int[] _outputEscapesEscape codes to use when writing strings (only valid of_characterEscapesis not null, seesetCharacterEscapes(CharacterEscapes))
-
-
Constructor Details
-
BsonGenerator
public BsonGenerator(tools.jackson.core.ObjectWriteContext writeCtxt, tools.jackson.core.io.IOContext ctxt, int jsonFeatures, int bsonFeatures, OutputStream out) Creates a new generator- Parameters:
writeCtxt- the object write contextctxt- the IO contextjsonFeatures- bit flag composed of bits that indicate whichStreamWriteFeatures are enabled.bsonFeatures- bit flag composed of bits that indicate whichBsonGenerator.Features are enabled.out- the output stream to write to
-
-
Method Details
-
setCharacterEscapes
public tools.jackson.core.JsonGenerator setCharacterEscapes(tools.jackson.core.io.CharacterEscapes esc) - Overrides:
setCharacterEscapesin classtools.jackson.core.JsonGenerator
-
getCharacterEscapes
public tools.jackson.core.io.CharacterEscapes getCharacterEscapes()- Overrides:
getCharacterEscapesin classtools.jackson.core.JsonGenerator
-
isEnabled
Checks if a generator feature is enabled- Parameters:
f- the feature- Returns:
- true if the given feature is enabled
-
isArray
protected boolean isArray()- Returns:
- true if the generator is currently processing an array
-
getAndIncCurrentArrayPos
protected int getAndIncCurrentArrayPos()Retrieves and then increases the current position in the array currently being generated- Returns:
- the position (before it has been increased) or -1 if the current document is not an array
-
reserveHeader
protected void reserveHeader()Reserves bytes for the BSON document header -
putHeader
protected void putHeader(int pos) Writes the BSON document header to the output buffer at the given position. Does not increase the buffer's write position.- Parameters:
pos- the position where to write the header
-
flush
public void flush() -
_releaseBuffers
protected void _releaseBuffers()- Specified by:
_releaseBuffersin classtools.jackson.core.base.GeneratorBase
-
_closeInput
- Specified by:
_closeInputin classtools.jackson.core.base.GeneratorBase- Throws:
IOException
-
streamWriteCapabilities
public tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamWriteCapability> streamWriteCapabilities()- Specified by:
streamWriteCapabilitiesin classtools.jackson.core.JsonGenerator
-
streamWriteOutputBuffered
public int streamWriteOutputBuffered()- Specified by:
streamWriteOutputBufferedin classtools.jackson.core.JsonGenerator
-
streamWriteOutputTarget
- Specified by:
streamWriteOutputTargetin classtools.jackson.core.JsonGenerator
-
version
public tools.jackson.core.Version version()- Specified by:
versionin interfacetools.jackson.core.Versioned- Specified by:
versionin classtools.jackson.core.JsonGenerator
-
streamWriteContext
public tools.jackson.core.TokenStreamContext streamWriteContext()- Specified by:
streamWriteContextin classtools.jackson.core.JsonGenerator
-
currentValue
- Specified by:
currentValuein classtools.jackson.core.JsonGenerator
-
assignCurrentValue
- Specified by:
assignCurrentValuein classtools.jackson.core.JsonGenerator
-
writeStartArray
- Specified by:
writeStartArrayin classtools.jackson.core.JsonGenerator
-
writeStartArray
public tools.jackson.core.JsonGenerator writeStartArray()- Specified by:
writeStartArrayin classtools.jackson.core.JsonGenerator
-
writeEndArray
public tools.jackson.core.JsonGenerator writeEndArray()- Specified by:
writeEndArrayin classtools.jackson.core.JsonGenerator
-
writeStartObject
- Specified by:
writeStartObjectin classtools.jackson.core.JsonGenerator
-
writeStartObject
public tools.jackson.core.JsonGenerator writeStartObject()- Specified by:
writeStartObjectin classtools.jackson.core.JsonGenerator
-
_writeStartObject
protected void _writeStartObject(boolean array) Creates a new embedded document or array- Parameters:
array- true if the embedded object is an array
-
writeEndObject
public tools.jackson.core.JsonGenerator writeEndObject()- Specified by:
writeEndObjectin classtools.jackson.core.JsonGenerator
-
writeEndObjectInternal
protected void writeEndObjectInternal() -
_writeArrayFieldNameIfNeeded
protected void _writeArrayFieldNameIfNeeded()If the generator is currently processing an array, this method writes the field name of the current element (which is just the position of the element in the array) -
writeName
- Specified by:
writeNamein classtools.jackson.core.JsonGenerator
-
writePropertyId
public tools.jackson.core.JsonGenerator writePropertyId(long id) - Specified by:
writePropertyIdin classtools.jackson.core.JsonGenerator
-
_writeFieldName
-
_verifyValueWrite
- Specified by:
_verifyValueWritein classtools.jackson.core.base.GeneratorBase
-
flushBuffer
protected void flushBuffer()Tries to flush the output buffer if streaming is enabled. This method is a no-op if streaming is disabled. -
writeString
- Specified by:
writeStringin classtools.jackson.core.JsonGenerator
-
writeString
public tools.jackson.core.JsonGenerator writeString(char[] text, int offset, int len) - Specified by:
writeStringin classtools.jackson.core.JsonGenerator
-
writeRaw
- Specified by:
writeRawin classtools.jackson.core.JsonGenerator
-
writeRaw
- Specified by:
writeRawin classtools.jackson.core.JsonGenerator
-
writeRaw
public tools.jackson.core.JsonGenerator writeRaw(char[] text, int offset, int len) - Specified by:
writeRawin classtools.jackson.core.JsonGenerator
-
writeRaw
public tools.jackson.core.JsonGenerator writeRaw(char c) - Specified by:
writeRawin classtools.jackson.core.JsonGenerator
-
writeBinary
public tools.jackson.core.JsonGenerator writeBinary(tools.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len) - Specified by:
writeBinaryin classtools.jackson.core.JsonGenerator
-
writeBinary
public tools.jackson.core.JsonGenerator writeBinary(tools.jackson.core.Base64Variant b64variant, byte subType, byte[] data, int offset, int len) Similar towriteBinary(Base64Variant, byte[], int, int), but with the possibility to specify a binary subtype (seeBsonConstants).- Parameters:
b64variant- base64 variant to use (will be ignored for BSON)subType- the binary subtypedata- the binary data to writeoffset- the offset of the first byte to writelen- the number of bytes to write- Returns:
- this generator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(int v) - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(short v) - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(long v) - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
- Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(double d) - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(float f) - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
- Specified by:
writeNumberin classtools.jackson.core.JsonGenerator
-
writeNumber
public tools.jackson.core.JsonGenerator writeNumber(String encodedValue) throws UnsupportedOperationException - Specified by:
writeNumberin classtools.jackson.core.JsonGenerator- Throws:
UnsupportedOperationException
-
writeBoolean
public tools.jackson.core.JsonGenerator writeBoolean(boolean state) - Specified by:
writeBooleanin classtools.jackson.core.JsonGenerator
-
writeNull
public tools.jackson.core.JsonGenerator writeNull()- Specified by:
writeNullin classtools.jackson.core.JsonGenerator
-
writeRawUTF8String
public tools.jackson.core.JsonGenerator writeRawUTF8String(byte[] text, int offset, int length) - Specified by:
writeRawUTF8Stringin classtools.jackson.core.JsonGenerator
-
writeUTF8String
public tools.jackson.core.JsonGenerator writeUTF8String(byte[] text, int offset, int length) - Specified by:
writeUTF8Stringin classtools.jackson.core.JsonGenerator
-
writeDateTime
Write a BSON date time- Parameters:
date- The date to write- Returns:
- this generator
-
writeObjectId
Write a BSON ObjectId- Parameters:
objectId- The objectId to write- Returns:
- this generator
-
flagsToRegexOptions
Converts a a Java flags word into a BSON options pattern- Parameters:
flags- the Java flags- Returns:
- the regex options string
-
writeRegex
Write a BSON regex- Parameters:
pattern- The regex to write- Returns:
- this generator
-
writeTimestamp
Write a MongoDB timestamp- Parameters:
timestamp- The timestamp to write- Returns:
- this generator
-
writeJavaScript
public tools.jackson.core.JsonGenerator writeJavaScript(JavaScript javaScript, tools.jackson.databind.SerializationContext ctxt) Write a BSON JavaScript object- Parameters:
javaScript- The javaScript to writectxt- The serializer context, for serializing the scope- Returns:
- this generator
-
writeSymbol
Write a BSON Symbol object- Parameters:
symbol- The symbol to write- Returns:
- this generator
-
_writeString
Write a BSON string structure (a null terminated string prependend by the length of the string)- Parameters:
string- The string to write- Returns:
- The number of bytes written, including the terminating null byte and the size of the string
-
_writeCString
Write a BSON cstring structure (a null terminated string)- Parameters:
string- The string to write- Returns:
- The number of bytes written, including the terminating null byte
-
escapeCharacters
Escapes the given string according to_characterEscapes. If there are no character escapes returns the original string.- Parameters:
string- the string to escape- Returns:
- the escaped string or the original one if there is nothing to escape
-