Class BsonParser

All Implemented Interfaces:
BsonMetaProvider, ArrayRecordReadable, RecordReadable
Direct Known Subclasses:
BsonParser.Base64, BsonParser.SpacedHex

Parses BSON (Binary JSON) into POJO models.
Media types:
Handles Content-Type types: application/bson
Description

BSON-to-Java type mapping:

  • BSON document (0x03) → bean or Map
  • BSON array (0x04) → Collection or 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 to long, short, byte as needed
  • BSON int64 (0x12) → long; coerced to int if target is int
  • BSON double (0x01) → double; coerced to float
  • BSON datetime (0x09) → Date, Calendar, Instant, or long (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 BigDecimal form; they are normalized to null.
  • 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 clean ParseException instead of an unchecked error.
  • BSON is binary; SpacedHex and Base64 subclasses accept text-encoded input.
See Also: