Package org.apache.juneau.marshall.bson
Class BsonParser
java.lang.Object
org.apache.juneau.marshall.Context
org.apache.juneau.marshall.MarshallingContextable
org.apache.juneau.marshall.parser.Parser
org.apache.juneau.marshall.parser.InputStreamParser
org.apache.juneau.marshall.bson.BsonParser
- All Implemented Interfaces:
BsonMetaProvider,ArrayRecordReadable,RecordReadable
- Direct Known Subclasses:
BsonParser.Base64,BsonParser.SpacedHex
public class BsonParser
extends InputStreamParser
implements BsonMetaProvider, RecordReadable, ArrayRecordReadable
Parses BSON (Binary JSON) into POJO models.
Content-Type types: application/bson
Media types:
HandlesDescription
BSON-to-Java type mapping:
- BSON document (0x03) → bean or
Map - BSON array (0x04) →
Collectionor Java array (numeric string keys ignored) - BSON string (0x02) →
String; coerced to types parseable from string - BSON boolean (0x08) →
Boolean - BSON int32 (0x10) →
int; coerced tolong,short,byteas needed - BSON int64 (0x12) →
long; coerced tointif target isint - BSON double (0x01) →
double; coerced tofloat - BSON datetime (0x09) →
Date,Calendar,Instant, orlong(millis) - BSON binary (0x05) →
byte[] - BSON decimal128 (0x13) →
BigDecimal - BSON null (0x0A) →
null - BSON ObjectId (0x07) → 24-character hex
String - Deprecated/unsupported types → silently skipped
Notes:
- This class is thread safe and reusable.
Limitations:
- BSON requires a top-level document. Standalone scalars are wrapped as
{value: x} . - Map keys must be strings. Non-string keys use
toString() . Null keys use a configurable placeholder. - ObjectId (0x07) is read-only; parsed as 24-character hex strings.
- Deprecated types (Undefined, DBPointer, Symbol, JS with Scope) are silently skipped.
- MongoDB-internal types (JavaScript, Timestamp, MinKey, MaxKey) are not supported.
- Decimal128 (0x13) NaN and ±Infinity have no
BigDecimalform; they are normalized tonull . - Wire-declared lengths (strings, binary, document/array size prefixes) are bounds-checked against a
configurable maximum (default 16 MiB, see
BsonParser.Builder.maxLength(int)); malformed/adversarial lengths fail with a cleanParseExceptioninstead of an unchecked error. - BSON is binary;
SpacedHex andBase64 subclasses accept text-encoded input.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefault parser, string input encoded as BASE64.static classBuilder class.static classDefault parser, string input encoded as spaced-hex.Nested classes/interfaces inherited from class org.apache.juneau.marshall.parser.InputStreamParser
InputStreamParser.DefaultBuilderNested classes/interfaces inherited from class org.apache.juneau.marshall.parser.Parser
Parser.Null -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BsonParserDefault parser, all default settings.static final BsonParserDefault parser, BASE64 string input.static final BsonParserDefault parser, spaced-hex string input.Fields inherited from class org.apache.juneau.marshall.parser.Parser
autoCloseStreams, consumes, debugOutputLines, listener, nulls, trimStrings, unbufferedFields inherited from class org.apache.juneau.marshall.MarshallingContextable
marshallingContextFields inherited from class org.apache.juneau.marshall.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static BsonParser.Buildercreate()Creates a new builder for this object.Create a session builder based on the properties defined on this context.Returns the language-specific metadata on the specified bean property.getBsonClassMeta(ClassMeta<?> cm) Returns the language-specific metadata on the specified class.intReturns the maximum allowed wire-declared length (in bytes) for strings, binary payloads, and document/array size prefixes.Returns the string used to representnull map keys in BSON documents.Returns a session to use for this context.booleanThe BSON array-record cursor is buffered/RecordAdapter-backed, not O(1) streaming.booleanThe BSON record cursor is buffered/RecordAdapter-backed, not O(1) streaming.readArrayRecords(Object input) Convenience delegator for the buffered array-elementRecordReaderusing default session arguments.readRecords(Object input) Convenience delegator for the whole-valueRecordReaderusing default session arguments.Methods inherited from class org.apache.juneau.marshall.parser.InputStreamParser
getBinaryFormat, isReaderParserMethods inherited from class org.apache.juneau.marshall.parser.Parser
canHandle, createParserBuilder, doRead, getDebugOutputLines, getListener, getMediaTypes, getNulls, getPrimaryMediaType, isAutoCloseStreams, isTrimStrings, isUnbuffered, properties, read, read, read, read, read, read, readArgs, readIntoCollection, readIntoMap, readWithOuterMethods inherited from class org.apache.juneau.marshall.MarshallingContextable
getDurationFormat, getMarshallingContext, getPeriodFormatMethods inherited from class org.apache.juneau.marshall.Context
createBuilder, getAnnotationProvider, init, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.ArrayRecordReadable
readArrayRecords
-
Field Details
-
DEFAULT
Default parser, all default settings. -
DEFAULT_SPACED_HEX
Default parser, spaced-hex string input. -
DEFAULT_BASE64
Default parser, BASE64 string input.
-
-
Constructor Details
-
BsonParser
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
copy
Description copied from class:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
-
createSession
Description copied from class:ContextCreate a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use
Context.getSession()directly.- Overrides:
createSessionin classInputStreamParser- Returns:
- A new session builder.
-
getBsonBeanPropertyMeta
Description copied from interface:BsonMetaProviderReturns the language-specific metadata on the specified bean property.- Specified by:
getBsonBeanPropertyMetain interfaceBsonMetaProvider- Parameters:
bpm- The bean property to return the metadata on.- Returns:
- The metadata.
-
getBsonClassMeta
Description copied from interface:BsonMetaProviderReturns the language-specific metadata on the specified class.- Specified by:
getBsonClassMetain interfaceBsonMetaProvider- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
getSession
Description copied from class:ContextReturns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSessionin classInputStreamParser- Returns:
- A new session object.
-
getNullKeyString
Returns the string used to representnull map keys in BSON documents.- Returns:
- The null key placeholder string.
-
getMaxLength
Returns the maximum allowed wire-declared length (in bytes) for strings, binary payloads, and document/array size prefixes.- Returns:
- The maximum length in bytes.
-
readRecords
Convenience delegator for the whole-valueRecordReaderusing default session arguments. The real implementation lives onBsonParserSession.readRecords(Object).- Specified by:
readRecordsin interfaceRecordReadable- Parameters:
input- The input.- Returns:
- A new
RecordReadercursor. - Throws:
IOException- If a problem occurred opening the underlying input.
-
readArrayRecords
Convenience delegator for the buffered array-elementRecordReaderusing default session arguments. The real implementation lives onBsonParserSession.readArrayRecords(Object).- Specified by:
readArrayRecordsin interfaceArrayRecordReadable- Parameters:
input- The input.- Returns:
- A buffered
RecordReader. - Throws:
IOException- If a problem occurred reading the input.
-
isRecordStreaming
The BSON record cursor is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isRecordStreamingin interfaceRecordReadable- Returns:
- Always
false .
-
isArrayRecordStreaming
The BSON array-record cursor is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isArrayRecordStreamingin interfaceArrayRecordReadable- Returns:
- Always
false .
-