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

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–