Git Product home page Git Product logo

bson's Introduction

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 Type Internal Type Bson Byte Returned internal Returned Dart Notes
double BsonDouble 1 / 0x01 BsonDouble double
String BsonString 2 / 0x02 BsonString String
Map BsonMap 3 / 0x03 BsonMap Map
DbRef BsonDbRef Map convention BsonDbRef DbRef Map containing keys "$id" and "$ref"
List BsonArray 4 / 0x04 BsonArray List
LegacyUuid BsonLegacyUuid 5 / 0x05 BsonLegacyUuid LegacyUuid Sub Type 3 / 0x03
UuidValue BsonUuid 5 / 0x05 BsonUuid UuidValue Sub Type 4 / 0x04
ObjectId BsonObjectId 7 / 0x07 BsonObjectId ObjectId
bool BsonBoolean 8 / 0x08 BsonBoolean bool
DateTime BsonDate 9 / 0x09 BsonDate DateTime
Null BsonNull 10 / 0x0A BsonNull Null
RegExp BsonRegexp 11 / 0x0B BsonRegexp RegExp
DBPointer DBPointer 12 / 0x0C DBPointer DBPointer @Deprecated
JsCode BsonCode 13 / 0x0D BsonCode JsCode
int BsonInt 16 / 0x10 BsonInt int when bitLength <= 31
Int32 BsonInt 16 / 0x10 BsonInt int
Timestamp BsonTimestamp 17 / 0x11 BsonTimestamp Timestamp
int BsonLong 18 / 0x12 BsonLong Int64 when bitLength > 31
Int64 BsonLong 18 / 0x12 BsonLong Int64
Decimal BsonDecimal128 19 / 0x13 BsonDecimal128 Decimal

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:

Class Package
DbRef Bson
LegacyUuid Bson
UuidValue Uuid
ObjectId Bson
DBPointer Bson
JsCode Bson
Timestamp Bson
Int32 Fixnum
Int64 Fixnum
Decimal Decimal

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:

EJson Internal Type Bson Byte Returned internal Returned EJson Notes
"$numberDouble" BsonDouble 1 / 0x01 BsonDouble "$numberDouble"
String BsonString 2 / 0x02 BsonString String
Map BsonMap 3 / 0x03 BsonMap Map
"$ref" - "$id" BsonDbRef Map convention BsonDbRef "$ref" - "$id" Map containing keys "$id" and "$ref"
List BsonArray 4 / 0x04 BsonArray List
"$binary" BsonBinary 5 / 0x05 BsonBinary "$binary" Sub Type 0 / 0x00
"$binary" BsonLegacyUuid 5 / 0x05 BsonLegacyUuid "$binary" Sub Type 3 / 0x03
"$binary" BsonUuid 5 / 0x05 BsonUuid "$binary" Sub Type 4 / 0x04
"$oid" BsonObjectId 7 / 0x07 BsonObjectId "$oid"
bool BsonBoolean 8 / 0x08 BsonBoolean bool
"$date" BsonDate 9 / 0x09 BsonDate "$date"
Null BsonNull 10 / 0x0A BsonNull Null
"$regularExpression" BsonRegexp 11 / 0x0B BsonRegexp "$regularExpression"
"$dbPointer" DBPointer 12 / 0x0C DBPointer "$dbPointer" @Deprecated
"$code" BsonCode 13 / 0x0D BsonCode "$code"
int BsonInt 16 / 0x10 BsonInt "$numberInt" when bitLength <= 31
"$numberInt" BsonInt 16 / 0x10 BsonInt "$numberInt"
"$timestamp" BsonTimestamp 17 / 0x11 BsonTimestamp "$timestamp"
int BsonLong 18 / 0x12 BsonLong "$numberLong" when bitLength > 31
"$numberLong" BsonLong 18 / 0x12 BsonLong "$numberLong"
"$numberDecimal" BsonDecimal128 19 / 0x13 BsonDecimal128 "$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

bson's People

Contributors

a14n avatar advdv avatar alexd1971 avatar analogic avatar davidmartos96 avatar giorgiofran avatar jbaxe2 avatar kaisellgren avatar pacane avatar paulecoyote avatar sestegra avatar vadimtsushko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bson's Issues

Purpose of clientMode in ObjectId

Just a curious about what's the usage of ObjectId(clientMode: true)? A similar method as Mongoose create it without inserting to real database? Can't find any comments or clue in the source code, so I ask here. Hope someone know the concept could share with ๐Ÿ˜„

Support for EJSON parse and stringify (relaxed mode, etc)

Hi guys thank's for this great library. Would it be possible to match the node library and add support for EJSON parse and stringify. For example :

import 'package:bson/ejson.dart';;
var text = '{ "int32": { "$numberInt": "10" } }';
 
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
print(ejson.parse(text, { relaxed: false }));
 
// prints { int32: 10 }
print(ejson.parse(text));

Explicit 64 bit int support

When taking a BsonMap from Map<String,dynamic>, bsonObjectFrom() is called, and it creates a BsonLong() or BsonInt() depending on the value. In some cases it'd be good to have a way to explicitly create a BsonLong (MongoDB, for example).

Index out of range when trying to deserialize

When trying to use the library using the following code

  final serialized = BSON().serialize({'id': 42});
  final deserialized = BSON().deserialize(serialized);

I get

Unhandled Exception: RangeError (index): Index out of range: index should be less than 13: 17
#0      Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:2221:7)
#1      BsonBinary.readByte (package:bson/src/types/binary.dart:256:29)
#2      BsonMap.extractData (package:bson/src/types/map.dart:13:27)
#3      BSON.deserialize (package:bson/src/bson_impl.dart:19:20)
#4      main (package:cool_tests/main.dart:35:31)

Am I using it in a wrong way?

Can not handle empty List

If you have an empty list in your map, BsonCodec.serialize works, but BsonCodec.deserialize throws an error:

  final simpleMap = {'emptyList': []};
  
  final bson = BsonCodec.serialize(simpleMap);
  final map = BsonCodec.deserialize(bson);
  expect(map, simpleMap);
dart:typed_data                                      Uint8List.[]
package:bson/src/types/bson_binary.dart 304:29       BsonBinary.readByte
package:bson/src/types/bson_array.dart 143:34        BsonArrayData._buffer2metaData
package:bson/src/types/bson_array.dart 121:52        BsonArrayData.metaArray
package:bson/src/types/bson_array.dart 19:29         new BsonArray._analyzeBsonArrayData
package:bson/src/types/bson_array.dart 14:17         new BsonArray.fromBuffer
package:bson/src/types/base/bson_object.dart 225:26  new BsonObject.fromTypeByteAndBuffer
package:bson/src/types/bson_map.dart 189:29          BsonMapData._buffer2metaData
package:bson/src/types/bson_map.dart 162:25          BsonMapData.metaDocument
package:bson/src/types/bson_map.dart 32:27           new BsonMap._analyzeBsonMapData
package:bson/src/types/bson_map.dart 28:15           new BsonMap.fromBuffer
package:bson/src/bson_codec.dart 26:20               BsonCodec.deserialize
test/main.dart 41:27                main.<fn>

RangeError (index): Index out of range: no indices are valid: 0

Is there a workaround for this (except for removing empty lists) or am I perhaps missing something?

Convert Endianness references to Endian

The Endianness class (from dart:typed_data) was renamed in Dart 2 to Endian, and corresponding UPPERCASE_CONSTANTS were renamed to lowercast_constant versions. These Dart 2 changes should be updated in this package.

FromHex doesn't error check

First let me say thank you so much for this library. Dart + mongodb + mongo_dart is a match made in heaven and exactly how I want to deal with my backend stuff. Thank you! This library helped free me from PHP finally!

That said, I think there should be some error checking with ObjectId.fromHex (or ObjectId.parse()). Currently I can pass in anything to construct a new ObjectId and I won't get any errors thrown until after I try to query the database. It would be great if ObjectId.parse could tell me as soon as I try to parse the string that it's not a valid hex string for an ObjectId.

Discussion

To get myself started:

  1. would you agree to marking bson_object as abstract explicitely
  2. move all types (extending bson_object) into a subdirectory 'types'

and a question:

what is the purpose of the json_ext.dart?

Update use new cryptographically secure Random

There was recently an update to the Go driver that I thought could be applied to the Dart version as well, with help of the new Random functions. The Go driver issue can be found here.

I tried to find where bson uses random numbers for things such as the bson id's, and found that it seem they come from this file statics.dart.

What do you think @vadimtsushko, I'm not sure if all uses of Random should be updated to use cryptographically secure random, but at least at some places I think it would be beneficial. I don't think there is any big performance differences either, I think the new secure random should be efficient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.