1- # bson-rs
1+ # bson
22
33[ ![ crates.io] ( https://img.shields.io/crates/v/bson.svg )] ( https://crates.io/crates/bson )
4+ [ ![ docs.rs] ( https://docs.rs/mongodb/badge.svg )] ( https://docs.rs/bson )
45[ ![ crates.io] ( https://img.shields.io/crates/l/bson.svg )] ( https://crates.io/crates/bson )
56
67Encoding and decoding support for BSON in Rust
78
89## Index
10+ - [ Installation] ( #installation )
11+ - [ Requirements] ( #requirements )
12+ - [ Importing] ( #importing )
13+ - [ Feature flags] ( #feature-flags )
14+ - [ Useful links] ( #useful-links )
915- [ Overview of BSON Format] ( #overview-of-bson-format )
1016- [ Usage] ( #usage )
1117 - [ BSON Values] ( #bson-values )
1218 - [ BSON Documents] ( #bson-documents )
1319 - [ Modeling BSON with strongly typed data structures] ( #modeling-bson-with-strongly-typed-data-structures )
20+ - [ Working with datetimes] ( #working-with-datetimes )
21+ - [ Working with UUIDs] ( #working-with-uuids )
1422- [ Contributing] ( #contributing )
1523- [ Running the Tests] ( #running-the-tests )
1624- [ Continuous Integration] ( #continuous-integration )
@@ -20,17 +28,28 @@ Encoding and decoding support for BSON in Rust
2028- [ Serde Documentation] ( https://serde.rs/ )
2129
2230## Installation
23- This crate works with Cargo and can be found on
24- [ crates.io] ( https://crates.io/crates/bson ) with a ` Cargo.toml ` like:
31+ ### Requirements
32+ - Rust 1.48+
33+
34+ ### Importing
35+ This crate is available on [ crates.io] ( https://crates.io/crates/bson ) . To use it in your application, simply add it to your project's ` Cargo.toml ` .
2536
2637``` toml
2738[dependencies ]
2839bson = " 2.0.0-beta.3"
2940```
3041
31- This crate requires Rust 1.48+.
42+ Note that if you are using ` bson ` through the ` mongodb ` crate, you do not need to specify it in your
43+ ` Cargo.toml ` , since the ` mongodb ` crate already re-exports it.
44+
45+ #### Feature Flags
46+
47+ | Feature | Description | Extra dependencies | Default |
48+ | :-------------| :-----------------------------------------------------------------------------------------------| :-------------------| :--------|
49+ | ` chrono-0_4 ` | Enable support for v0.4 of the [ ` chrono ` ] ( docs.rs/chrono/0.4 ) crate in the public API. | n/a | no |
50+ | ` uuid-0_8 ` | Enable support for v0.8 of the [ ` uuid ` ] ( docs.rs/uuid/0.8 ) crate in the public API. | ` uuid ` 0.8 | no |
3251
33- ## Overview of BSON Format
52+ ## Overview of the BSON Format
3453
3554BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents.
3655Like JSON, BSON supports the embedding of documents and arrays within other documents
@@ -186,6 +205,82 @@ separate the "business logic" that operates over the data from the (de)serializa
186205translates the data to/from its serialized form. This can lead to more clear and concise code
187206that is also less error prone.
188207
208+ ### Working with datetimes
209+
210+ The BSON format includes a datetime type, which is modeled in this crate by the
211+ [ ` bson::DateTime ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/struct.DateTime.html ) struct, and the
212+ ` Serialize ` and ` Deserialize ` implementations for this struct produce and parse BSON datetimes when
213+ serializing to or deserializing from BSON. The popular crate [ ` chrono ` ] ( https://docs.rs/chrono ) also
214+ provides a ` DateTime ` type, but its ` Serialize ` and ` Deserialize ` implementations operate on strings
215+ instead, so when using it with BSON, the BSON datetime type is not used. To work around this, the
216+ ` chrono-0_4 ` feature flag can be enabled. This flag exposes a number of convenient conversions
217+ between ` bson::DateTime ` and ` chrono::DateTime ` , including the
218+ [ ` chrono_datetime_as_bson_datetime ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/chrono_datetime_as_bson_datetime/index.html )
219+ serde helper, which can be used to (de)serialize ` chrono::DateTime ` s to/from BSON datetimes, and the
220+ ` From<chrono::DateTime> ` implementation for ` Bson ` , which allows ` chrono::DateTime ` values to be
221+ used in the ` doc! ` and ` bson! ` macros.
222+
223+ e.g.
224+ ``` rust
225+ use serde :: {Serialize , Deserialize };
226+
227+ #[derive(Serialize , Deserialize )]
228+ struct Foo {
229+ // serializes as a BSON datetime.
230+ date_time : bson :: DateTime ,
231+
232+ // serializes as an RFC 3339 / ISO-8601 string.
233+ chrono_datetime : chrono :: DateTime <chrono :: Utc >,
234+
235+ // serializes as a BSON datetime.
236+ // this requires the "chrono-0_4" feature flag
237+ #[serde(with = " bson::serde_helpers::chrono_datetime_as_bson_datetime" )]
238+ chrono_as_bson : chrono :: DateTime <chrono :: Utc >,
239+ }
240+
241+ // this automatic conversion also requires the "chrono-0_4" feature flag
242+ let query = doc! {
243+ " created_at" : chrono :: Utc :: now (),
244+ };
245+ ```
246+
247+ ### Working with UUIDs
248+
249+ The BSON format does not contain a dedicated UUID type, though it does have a "binary" type which
250+ has UUID subtypes. Accordingly, this crate provides a
251+ [ ` Binary ` ] ( https://docs.rs/bson/latest/bson/struct.Binary.html ) struct which models this binary type
252+ and serializes to and deserializes from it. The popular [ ` uuid ` ] ( https://docs.rs/uuid ) crate does
253+ provide a UUID type (` Uuid ` ), though its ` Serialize ` and ` Deserialize ` implementations operate on
254+ strings, so when using it with BSON, the BSON binary type will not be used. To facilitate the
255+ conversion between ` Uuid ` values and BSON binary values, the ` uuid-0_8 ` feature flag can be
256+ enabled. This flag exposes a number of convenient conversions from ` Uuid ` , including the
257+ [ ` uuid_as_binary ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/uuid_as_binary/index.html )
258+ serde helper, which can be used to (de)serialize ` Uuid ` s to/from BSON binaries with the UUID
259+ subtype, and the ` From<Uuid> ` implementation for ` Bson ` , which allows ` Uuid ` values to be used in
260+ the ` doc! ` and ` bson! ` macros.
261+
262+ e.g.
263+
264+ ``` rust
265+ use serde :: {Serialize , Deserialize };
266+
267+ #[derive(Serialize , Deserialize )]
268+ struct Foo {
269+ // serializes as a String.
270+ uuid : Uuid ,
271+
272+ // serializes as a BSON binary with subtype 4.
273+ // this requires the "uuid-0_8" feature flag
274+ #[serde(with = " bson::serde_helpers::uuid_as_binary" )]
275+ uuid_as_bson : uuid :: Uuid ,
276+ }
277+
278+ // this automatic conversion also requires the "uuid-0_8" feature flag
279+ let query = doc! {
280+ " uuid" : uuid :: Uuid :: new_v4 (),
281+ };
282+ ```
283+
189284## Minimum supported Rust version (MSRV)
190285
191286The MSRV for this crate is currently 1.48.0. This will be rarely be increased, and if it ever is,
0 commit comments