1
- # bson-rs
1
+ # bson
2
2
3
3
[ ![ 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 )
4
5
[ ![ crates.io] ( https://img.shields.io/crates/l/bson.svg )] ( https://crates.io/crates/bson )
5
6
6
7
Encoding and decoding support for BSON in Rust
7
8
8
9
## Index
10
+ - [ Installation] ( #installation )
11
+ - [ Requirements] ( #requirements )
12
+ - [ Importing] ( #importing )
13
+ - [ Feature flags] ( #feature-flags )
14
+ - [ Useful links] ( #useful-links )
9
15
- [ Overview of BSON Format] ( #overview-of-bson-format )
10
16
- [ Usage] ( #usage )
11
17
- [ BSON Values] ( #bson-values )
12
18
- [ BSON Documents] ( #bson-documents )
13
19
- [ 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 )
14
22
- [ Contributing] ( #contributing )
15
23
- [ Running the Tests] ( #running-the-tests )
16
24
- [ Continuous Integration] ( #continuous-integration )
@@ -20,17 +28,28 @@ Encoding and decoding support for BSON in Rust
20
28
- [ Serde Documentation] ( https://serde.rs/ )
21
29
22
30
## 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 ` .
25
36
26
37
``` toml
27
38
[dependencies ]
28
39
bson = " 2.0.0-beta.3"
29
40
```
30
41
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 |
32
51
33
- ## Overview of BSON Format
52
+ ## Overview of the BSON Format
34
53
35
54
BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents.
36
55
Like 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
186
205
translates the data to/from its serialized form. This can lead to more clear and concise code
187
206
that is also less error prone.
188
207
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
+
189
284
## Minimum supported Rust version (MSRV)
190
285
191
286
The MSRV for this crate is currently 1.48.0. This will be rarely be increased, and if it ever is,
0 commit comments