bson-rs
Encoding and decoding support for BSON in Rust
Useful links
Installation
This crate works with Cargo and can be found on
crates.io with a Cargo.toml
like:
[]
= "0.13"
Usage
Link the library in main.rs:
extern crate bson;
Prepare your struct for Serde serialization:
Serialize the struct:
use bson;
let person = Person ;
let serialized_person = to_bson?; // Serialize
if let Document = serialized_person else
Deserialize the struct:
// Read the document from a MongoDB collection
let person_document = mongoCollection.find_one?
.expect;
// Deserialize the document into a Person instance
let person = from_bson?
Breaking Changes
In the BSON specification, unsigned integer types are unsupported; for example, u32
. In the older version of this crate (< v0.8.0
), if you uses serde
to serialize unsigned integer types into BSON, it will store them with Bson::FloatingPoint
type. From v0.8.0
, we removed this behavior and simply returned an error when you want to serialize unsigned integer types to BSON. #72
For backward compatibility, we've provided a mod bson::compat::u2f
to explicitly serialize unsigned integer types into BSON's floating point value as follows:
In this example, we added an attribute #[serde(with = "bson::compat::u2f")]
on field x
, which will tell serde
to use the bson::compat::u2f::serialize
and bson::compat::u2f::deserialize
methods to process this field.