bson 5.0.7

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

Bson library for Dart. BSON is a binary-encoded serialization of JSON-like documents. The package allows also eJson and object serialization/deserialization.

bson

Bson library for Dart programming language

Version 5.0.0 has breaking API changes. See changelog for details.

Package

This package allows the conversion of a map of dart elements into a binary Bson representation and viceversa. The input can be a standard dart map with key name of the value and the value itself, or an ejson representation of the value or a mixed source. It is also possible to serialize objects that uses the BsonSerializable mixin.

There is a Codec class that allows to define which kind of serialization we want to perform (depending on the source) + three specialized classes that preset the parameters dependig on the source that we are providing. These classes (shortcuts for the Codec one) are:

  • BsonCodec
  • EjsonCodec
  • ObjectCode

BSON

This is the most important converter. It converts from a document of objects into the BSON format. The objects must be of a supported type. The actually managed objects are:

Dart TypeInternal TypeBson ByteReturned internalReturned DartNotes
doubleBsonDouble1 / 0x01BsonDoubledouble
StringBsonString2 / 0x02BsonStringString
MapBsonMap3 / 0x03BsonMapMap
DbRefBsonDbRefMap conventionBsonDbRefDbRefMap containing keys "$id" and "$ref"
ListBsonArray4 / 0x04BsonArrayList
LegacyUuidBsonLegacyUuid5 / 0x05BsonLegacyUuidLegacyUuidSub Type 3 / 0x03
UuidValueBsonUuid5 / 0x05BsonUuidUuidValueSub Type 4 / 0x04
ObjectIdBsonObjectId7 / 0x07BsonObjectIdObjectId
boolBsonBoolean8 / 0x08BsonBooleanbool
DateTimeBsonDate9 / 0x09BsonDateDateTime
NullBsonNull10 / 0x0ABsonNullNull
RegExpBsonRegexp11 / 0x0BBsonRegexpRegExp
DBPointerDBPointer12 / 0x0CDBPointerDBPointer@Deprecated
JsCodeBsonCode13 / 0x0DBsonCodeJsCode
intBsonInt16 / 0x10BsonIntintwhen bitLength <= 31
Int32BsonInt16 / 0x10BsonIntint
TimestampBsonTimestamp17 / 0x11BsonTimestampTimestamp
intBsonLong18 / 0x12BsonLongInt64when bitLength > 31
Int64BsonLong18 / 0x12BsonLongInt64
DecimalBsonDecimal12819 / 0x13BsonDecimal128Decimal

The objects expected are those of the column "Dart Type", while the objects returned are those of the column "Returned Dart". The other types are intended to be used internally. The Bson Types are eventually accepted in input instead of the corresponding Dart types. Most of the types expected are from the Dart language itself, with these exceptions:

ClassPackage
DbRefBson
LegacyUuidBson
UuidValueUuid
ObjectIdBson
DBPointerBson
JsCodeBson
TimestampBson
Int32Fixnum
Int64Fixnum
DecimalDecimal

To serialize an bson map you have to use the serialize method of the BsonCodec class. Ex. var bsonBinary = BsonCodec.serialize(bsonDocument); You can see a serialization example here

To deserialize an bson map you have to use the deserialize method of the BsonCodec class. Ex. var result = BsonCodec.deserialize(bsonBinary); You can see a deserialization example here

EJSON

It converts from a document in format ejson into the BSON format. Only ejson version 2 is supported. The objects must be of a supported type. The actually managed objects are:

EJsonInternal TypeBson ByteReturned internalReturned EJsonNotes
"$numberDouble"BsonDouble1 / 0x01BsonDouble"$numberDouble"
StringBsonString2 / 0x02BsonStringString
MapBsonMap3 / 0x03BsonMapMap
"$ref" - "$id"BsonDbRefMap conventionBsonDbRef"$ref" - "$id"Map containing keys "$id" and "$ref"
ListBsonArray4 / 0x04BsonArrayList
"$binary"BsonBinary5 / 0x05BsonBinary"$binary"Sub Type 0 / 0x00
"$binary"BsonLegacyUuid5 / 0x05BsonLegacyUuid"$binary"Sub Type 3 / 0x03
"$binary"BsonUuid5 / 0x05BsonUuid"$binary"Sub Type 4 / 0x04
"$oid"BsonObjectId7 / 0x07BsonObjectId"$oid"
boolBsonBoolean8 / 0x08BsonBooleanbool
"$date"BsonDate9 / 0x09BsonDate"$date"
NullBsonNull10 / 0x0ABsonNullNull
"$regularExpression"BsonRegexp11 / 0x0BBsonRegexp"$regularExpression"
"$dbPointer"DBPointer12 / 0x0CDBPointer"$dbPointer"@Deprecated
"$code"BsonCode13 / 0x0DBsonCode"$code"
intBsonInt16 / 0x10BsonInt"$numberInt"when bitLength <= 31
"$numberInt"BsonInt16 / 0x10BsonInt"$numberInt"
"$timestamp"BsonTimestamp17 / 0x11BsonTimestamp"$timestamp"
intBsonLong18 / 0x12BsonLong"$numberLong"when bitLength > 31
"$numberLong"BsonLong18 / 0x12BsonLong"$numberLong"
"$numberDecimal"BsonDecimal12819 / 0x13BsonDecimal128"$numberDecimal"

To serialize an ejson map you have to use the serialize method of the EjsonCodec class. Serialization accept relaxed values, if present. Ex. var bsonBinary = EJsonCodec.serialize(ejsonMap); You can see a serialization example here

To deserialize an ejson map you have to use the deserialize method of the EjsonCodec class. Ex. var result = EJsonCodec.deserialize(bsonBinary); You can see a deserialization example here
To extract the ejson in "relaxed" format you have to set the relaxed parameter to true. Ex var result = EJsonCodec.deserialize(bsonBinary, relaxed: true);

There are also two convenient methods that you can use to convert from a ejson map into a Bson Map (EJsonCodec.eJson2Doc(ejsonMap)) and viceversa (EJsonCodec.doc2eJson(document)).

There are also two convenient method to transform an ejson map into a string (EJsonCodec.stringify) and vice-versa (EJsonCodec.parse).

Dart Object

You can also convert dart objects into BSON Format. The requirements are the following:

  • The object must use the BsonSerializable mixin.
  • Override the toBson method where, for each field, the corresponding value must be given. The value of one of the Bson managed ones or another object with the BsonSerializable mixin
  • Register the class with a unique number and the method you use for recreating the instance

To serialize a BsonSerializable object you have to use the serialize method of the object instance (inherithed from BsonSerializable). Ex. BsonBinary result = < bsonSerializable >.serialize(); You can see a serialization example here

To deserialize BsonSerializable object you have to use the deserialize method of the ObjectCodec class. Ex. ObjectCodec.deserialize(bsonBinary) You can see a deserialization example here