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 Classes
    Modifier and Type
    Class
    Description
    protected static class 
    A structure describing the document currently being generated
    static enum 
    Defines toggable features
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
    Bit flag composed of bits that indicate which BsonGenerator.Features are enabled.
    protected final DynamicOutputBuffer
    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.
    protected tools.jackson.core.io.CharacterEscapes
    Custom character escapes to use when writing strings (field names and string values)
    Saves information about documents (the main document and embedded ones)
    protected final OutputStream
    The output stream to write to
    protected int[]
    Escape codes to use when writing strings (only valid of _characterEscapes is not null, see setCharacterEscapes(CharacterEscapes))
    protected int
    Saves the position of the type marker for the object currently begin written
    protected tools.jackson.core.json.JsonWriteContext
    The write context used for tracking object/array nesting
    protected boolean
    Indicates 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

    Constructors
    Constructor
    Description
    BsonGenerator(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 Type
    Method
    Description
    protected void
     
    protected void
     
    protected void
     
    protected void
    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)
    protected int
    Write a BSON cstring structure (a null terminated string)
    protected void
     
    protected void
    _writeStartObject(boolean array)
    Creates a new embedded document or array
    protected int
    Write a BSON string structure (a null terminated string prependend by the length of the string)
    void
     
     
    protected String
    Escapes the given string according to _characterEscapes.
    protected String
    Converts a a Java flags word into a BSON options pattern
    void
     
    protected void
    Tries to flush the output buffer if streaming is enabled.
    protected int
    Retrieves and then increases the current position in the array currently being generated
    tools.jackson.core.io.CharacterEscapes
     
    protected boolean
     
    protected boolean
    Checks if a generator feature is enabled
    protected void
    putHeader(int pos)
    Writes the BSON document header to the output buffer at the given position.
    protected void
    Reserves bytes for the BSON document header
    tools.jackson.core.JsonGenerator
    setCharacterEscapes(tools.jackson.core.io.CharacterEscapes esc)
     
    tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamWriteCapability>
     
    tools.jackson.core.TokenStreamContext
     
    int
     
     
    tools.jackson.core.Version
     
    tools.jackson.core.JsonGenerator
    writeBinary(tools.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len)
     
    tools.jackson.core.JsonGenerator
    writeBinary(tools.jackson.core.Base64Variant b64variant, byte subType, byte[] data, int offset, int len)
    Similar to writeBinary(Base64Variant, byte[], int, int), but with the possibility to specify a binary subtype (see BsonConstants).
    tools.jackson.core.JsonGenerator
    writeBoolean(boolean state)
     
    tools.jackson.core.JsonGenerator
    Write a BSON date time
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
     
    protected void
     
    tools.jackson.core.JsonGenerator
    writeJavaScript(JavaScript javaScript, tools.jackson.databind.SerializationContext ctxt)
    Write a BSON JavaScript object
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    writeNumber(double d)
     
    tools.jackson.core.JsonGenerator
    writeNumber(float f)
     
    tools.jackson.core.JsonGenerator
    writeNumber(int v)
     
    tools.jackson.core.JsonGenerator
    writeNumber(long v)
     
    tools.jackson.core.JsonGenerator
    writeNumber(short v)
     
    tools.jackson.core.JsonGenerator
    writeNumber(String encodedValue)
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    Write a BSON ObjectId
    tools.jackson.core.JsonGenerator
    writePropertyId(long id)
     
    tools.jackson.core.JsonGenerator
    writeRaw(char c)
     
    tools.jackson.core.JsonGenerator
    writeRaw(char[] text, int offset, int len)
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    writeRaw(String text, int offset, int len)
     
    tools.jackson.core.JsonGenerator
    writeRawUTF8String(byte[] text, int offset, int length)
     
    tools.jackson.core.JsonGenerator
    Write a BSON regex
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    writeStartArray(Object currentValue)
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    writeStartObject(Object currentValue)
     
    tools.jackson.core.JsonGenerator
    writeString(char[] text, int offset, int len)
     
    tools.jackson.core.JsonGenerator
     
    tools.jackson.core.JsonGenerator
    Write a BSON Symbol object
    tools.jackson.core.JsonGenerator
    Write a MongoDB timestamp
    tools.jackson.core.JsonGenerator
    writeUTF8String(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, writeTree

    Methods 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _bsonFeatures

      protected final int _bsonFeatures
      Bit flag composed of bits that indicate which BsonGenerator.Features are enabled.
    • _out

      protected final OutputStream _out
      The output stream to write to
    • _buffer

      protected final DynamicOutputBuffer _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 _typeMarker
      Saves the position of the type marker for the object currently begin written
    • _currentDocument

      protected BsonGenerator.DocumentInfo _currentDocument
      Saves information about documents (the main document and embedded ones)
    • nextObjectIsEmbeddedInValue

      protected boolean nextObjectIsEmbeddedInValue
      Indicates 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 _writeContext
      The write context used for tracking object/array nesting
    • _characterEscapes

      protected tools.jackson.core.io.CharacterEscapes _characterEscapes
      Custom character escapes to use when writing strings (field names and string values)
    • _outputEscapes

      protected int[] _outputEscapes
      Escape codes to use when writing strings (only valid of _characterEscapes is not null, see setCharacterEscapes(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 context
      ctxt - the IO context
      jsonFeatures - bit flag composed of bits that indicate which StreamWriteFeatures are enabled.
      bsonFeatures - bit flag composed of bits that indicate which BsonGenerator.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:
      setCharacterEscapes in class tools.jackson.core.JsonGenerator
    • getCharacterEscapes

      public tools.jackson.core.io.CharacterEscapes getCharacterEscapes()
      Overrides:
      getCharacterEscapes in class tools.jackson.core.JsonGenerator
    • isEnabled

      protected boolean isEnabled(BsonGenerator.Feature f)
      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()
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class tools.jackson.core.JsonGenerator
    • _releaseBuffers

      protected void _releaseBuffers()
      Specified by:
      _releaseBuffers in class tools.jackson.core.base.GeneratorBase
    • _closeInput

      protected void _closeInput() throws IOException
      Specified by:
      _closeInput in class tools.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • streamWriteCapabilities

      public tools.jackson.core.util.JacksonFeatureSet<tools.jackson.core.StreamWriteCapability> streamWriteCapabilities()
      Specified by:
      streamWriteCapabilities in class tools.jackson.core.JsonGenerator
    • streamWriteOutputBuffered

      public int streamWriteOutputBuffered()
      Specified by:
      streamWriteOutputBuffered in class tools.jackson.core.JsonGenerator
    • streamWriteOutputTarget

      public Object streamWriteOutputTarget()
      Specified by:
      streamWriteOutputTarget in class tools.jackson.core.JsonGenerator
    • version

      public tools.jackson.core.Version version()
      Specified by:
      version in interface tools.jackson.core.Versioned
      Specified by:
      version in class tools.jackson.core.JsonGenerator
    • streamWriteContext

      public tools.jackson.core.TokenStreamContext streamWriteContext()
      Specified by:
      streamWriteContext in class tools.jackson.core.JsonGenerator
    • currentValue

      public Object currentValue()
      Specified by:
      currentValue in class tools.jackson.core.JsonGenerator
    • assignCurrentValue

      public void assignCurrentValue(Object v)
      Specified by:
      assignCurrentValue in class tools.jackson.core.JsonGenerator
    • writeStartArray

      public tools.jackson.core.JsonGenerator writeStartArray(Object currentValue)
      Specified by:
      writeStartArray in class tools.jackson.core.JsonGenerator
    • writeStartArray

      public tools.jackson.core.JsonGenerator writeStartArray()
      Specified by:
      writeStartArray in class tools.jackson.core.JsonGenerator
    • writeEndArray

      public tools.jackson.core.JsonGenerator writeEndArray()
      Specified by:
      writeEndArray in class tools.jackson.core.JsonGenerator
    • writeStartObject

      public tools.jackson.core.JsonGenerator writeStartObject(Object currentValue)
      Specified by:
      writeStartObject in class tools.jackson.core.JsonGenerator
    • writeStartObject

      public tools.jackson.core.JsonGenerator writeStartObject()
      Specified by:
      writeStartObject in class tools.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:
      writeEndObject in class tools.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

      public tools.jackson.core.JsonGenerator writeName(String name)
      Specified by:
      writeName in class tools.jackson.core.JsonGenerator
    • writePropertyId

      public tools.jackson.core.JsonGenerator writePropertyId(long id)
      Specified by:
      writePropertyId in class tools.jackson.core.JsonGenerator
    • _writeFieldName

      protected void _writeFieldName(String name)
    • _verifyValueWrite

      protected void _verifyValueWrite(String typeMsg)
      Specified by:
      _verifyValueWrite in class tools.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

      public tools.jackson.core.JsonGenerator writeString(String text)
      Specified by:
      writeString in class tools.jackson.core.JsonGenerator
    • writeString

      public tools.jackson.core.JsonGenerator writeString(char[] text, int offset, int len)
      Specified by:
      writeString in class tools.jackson.core.JsonGenerator
    • writeRaw

      public tools.jackson.core.JsonGenerator writeRaw(String text)
      Specified by:
      writeRaw in class tools.jackson.core.JsonGenerator
    • writeRaw

      public tools.jackson.core.JsonGenerator writeRaw(String text, int offset, int len)
      Specified by:
      writeRaw in class tools.jackson.core.JsonGenerator
    • writeRaw

      public tools.jackson.core.JsonGenerator writeRaw(char[] text, int offset, int len)
      Specified by:
      writeRaw in class tools.jackson.core.JsonGenerator
    • writeRaw

      public tools.jackson.core.JsonGenerator writeRaw(char c)
      Specified by:
      writeRaw in class tools.jackson.core.JsonGenerator
    • writeBinary

      public tools.jackson.core.JsonGenerator writeBinary(tools.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len)
      Specified by:
      writeBinary in class tools.jackson.core.JsonGenerator
    • writeBinary

      public tools.jackson.core.JsonGenerator writeBinary(tools.jackson.core.Base64Variant b64variant, byte subType, byte[] data, int offset, int len)
      Similar to writeBinary(Base64Variant, byte[], int, int), but with the possibility to specify a binary subtype (see BsonConstants).
      Parameters:
      b64variant - base64 variant to use (will be ignored for BSON)
      subType - the binary subtype
      data - the binary data to write
      offset - the offset of the first byte to write
      len - the number of bytes to write
      Returns:
      this generator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(int v)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(short v)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(long v)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(BigInteger v)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(double d)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(float f)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(BigDecimal dec)
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
    • writeNumber

      public tools.jackson.core.JsonGenerator writeNumber(String encodedValue) throws UnsupportedOperationException
      Specified by:
      writeNumber in class tools.jackson.core.JsonGenerator
      Throws:
      UnsupportedOperationException
    • writeBoolean

      public tools.jackson.core.JsonGenerator writeBoolean(boolean state)
      Specified by:
      writeBoolean in class tools.jackson.core.JsonGenerator
    • writeNull

      public tools.jackson.core.JsonGenerator writeNull()
      Specified by:
      writeNull in class tools.jackson.core.JsonGenerator
    • writeRawUTF8String

      public tools.jackson.core.JsonGenerator writeRawUTF8String(byte[] text, int offset, int length)
      Specified by:
      writeRawUTF8String in class tools.jackson.core.JsonGenerator
    • writeUTF8String

      public tools.jackson.core.JsonGenerator writeUTF8String(byte[] text, int offset, int length)
      Specified by:
      writeUTF8String in class tools.jackson.core.JsonGenerator
    • writeDateTime

      public tools.jackson.core.JsonGenerator writeDateTime(Date date)
      Write a BSON date time
      Parameters:
      date - The date to write
      Returns:
      this generator
    • writeObjectId

      public tools.jackson.core.JsonGenerator writeObjectId(ObjectId objectId)
      Write a BSON ObjectId
      Parameters:
      objectId - The objectId to write
      Returns:
      this generator
    • flagsToRegexOptions

      protected String flagsToRegexOptions(int flags)
      Converts a a Java flags word into a BSON options pattern
      Parameters:
      flags - the Java flags
      Returns:
      the regex options string
    • writeRegex

      public tools.jackson.core.JsonGenerator writeRegex(Pattern pattern)
      Write a BSON regex
      Parameters:
      pattern - The regex to write
      Returns:
      this generator
    • writeTimestamp

      public tools.jackson.core.JsonGenerator writeTimestamp(Timestamp timestamp)
      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 write
      ctxt - The serializer context, for serializing the scope
      Returns:
      this generator
    • writeSymbol

      public tools.jackson.core.JsonGenerator writeSymbol(Symbol symbol)
      Write a BSON Symbol object
      Parameters:
      symbol - The symbol to write
      Returns:
      this generator
    • _writeString

      protected int _writeString(String string)
      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

      protected int _writeCString(String string)
      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

      protected String escapeCharacters(String string)
      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