Skip to main content
Version: 2.x

Type Mappings

Scala / Java to BSON Type Mappings​

ZIO Bson provides BsonEncoder and BsonDecoder typeclass instances for a wide range of Scala and Java types. The encoder determines the primary BSON type a value is written as. The decoder accepts the primary type and, where it makes sense, additional fallback BSON types.

Scala / Java TypeEncoder → BSONDecoder fallbacks
BooleanBOOLEAN—
ByteINT32INT64 (range check), DOUBLE (lossless), DECIMAL128 (lossless), STRING
ShortINT32INT64 (range check), DOUBLE (lossless), DECIMAL128 (lossless), STRING
IntINT32INT64 (range check), DOUBLE (lossless), DECIMAL128 (lossless), STRING
LongINT64INT32, DOUBLE (lossless), DECIMAL128 (lossless), STRING
FloatDOUBLEINT32, INT64, DECIMAL128 (lossless), STRING
DoubleDOUBLEINT32, INT64, DECIMAL128 (lossless), STRING
java.math.BigDecimalDECIMAL128INT32, INT64, DOUBLE, STRING
BigDecimalDECIMAL128INT32, INT64, DOUBLE, STRING
java.math.BigIntegerSTRINGINT32, INT64, DOUBLE (lossless), DECIMAL128 (lossless)
BigIntSTRINGINT32, INT64, DOUBLE (lossless), DECIMAL128 (lossless)
StringSTRINGSYMBOL
CharSTRINGSYMBOL
scala.SymbolSTRINGSYMBOL
UUIDSTRING—
java.util.CurrencySTRING—
DayOfWeekSTRING—
MonthSTRING—
MonthDaySTRING—
YearINT32INT64 (range check), DOUBLE (lossless), DECIMAL128 (lossless), STRING
YearMonthSTRING—
ZoneIdSTRING—
ZoneOffsetSTRING—
DurationSTRING—
PeriodSTRING—
InstantDATE_TIME—
LocalDateDATE_TIME—
LocalDateTimeDATE_TIME—
LocalTimeDATE_TIME—
OffsetDateTimeDOCUMENT—
OffsetTimeDOCUMENT—
ZonedDateTimeDOCUMENT—
org.bson.types.ObjectIdOBJECT_ID—
Option[A]same as A / NULL—
Array[Byte]BINARY—
Iterable[Byte] subtypeBINARY—
NonEmptyChunk[Byte]BINARY—
Array[A] (non-byte)ARRAY—
Iterable[A] subtype (non-byte)ARRAY—
NonEmptyChunk[A] (non-byte)ARRAY—
Map[K, V]DOCUMENT—
T <: BsonValueany (pass-through)—

Special Cases​

  • Byte and Short are decoded via the Int decoder followed by a range check using validateIntRange. All BSON numeric types and STRING are accepted, but the decoded value must fit within the type's range, otherwise a decoder error is returned.
  • BigInt and java.math.BigInteger encode to STRING but decode from all numeric BSON types. Conversion from DOUBLE and DECIMAL128 requires the value to be a lossless integer (no fractional part).
  • String decoder also accepts SYMBOL for backward compatibility with legacy BSON documents.
  • Char decoder requires the string to be exactly one character long.
  • OffsetDateTime and OffsetTime encode as a DOCUMENT with two fields: date_time (DATE_TIME) and offset (STRING).
  • ZonedDateTime encodes as a DOCUMENT with two fields: date_time (DATE_TIME) and zone_id (STRING).
  • Option[A] encodes None as NULL and Some(a) using the encoder for A. The decoder returns None for NULL and Some(a) for any value accepted by the decoder for A.

BSON Types Without Scala Mappings​

The following BSON types have no dedicated Scala or Java codec in this library. They are accessible only via the T <: BsonValue pass-through encoder/decoder using the underlying MongoDB Java driver:

BSON TypeNotes
SYMBOLDeprecated BSON type; accepted by the String decoder for backward compatibility, but no encoder exists
UNDEFINEDDeprecated BSON type
REGULAR_EXPRESSION—
DB_POINTERDeprecated BSON type
JAVASCRIPT—
JAVASCRIPT_WITH_SCOPE—
TIMESTAMPMongoDB internal replication timestamp
MIN_KEY—
MAX_KEY—
END_OF_DOCUMENTInternal sentinel used by the reader loop; never a value