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,29 @@ 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
+ | ` u2i ` | Attempt to serialize unsigned integer types found in ` Serialize ` structs to signed BSON types. | n/a | yes |
50
+ | ` chrono-0_4 ` | Enable support for v0.4 of the [ ` chrono ` ] ( docs.rs/chrono/0.4 ) crate in the public API. | n/a | no |
51
+ | ` 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
52
33
- ## Overview of BSON Format
53
+ ## Overview of the BSON Format
34
54
35
55
BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents.
36
56
Like JSON, BSON supports the embedding of documents and arrays within other documents
@@ -186,6 +206,82 @@ separate the "business logic" that operates over the data from the (de)serializa
186
206
translates the data to/from its serialized form. This can lead to more clear and concise code
187
207
that is also less error prone.
188
208
209
+ ### Working with datetimes
210
+
211
+ The BSON format includes a datetime type, which is modeled in this crate by the
212
+ [ ` bson::DateTime ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/struct.DateTime.html ) struct, and the
213
+ ` Serialize ` and ` Deserialize ` implementations for this struct produce and parse BSON datetimes when
214
+ serializing to or deserializing from BSON. The popular crate [ ` chrono ` ] ( https://docs.rs/chrono ) also
215
+ provides a ` DateTime ` type, but its ` Serialize ` and ` Deserialize ` implementations operate on strings
216
+ instead, so when using it with BSON, the BSON datetime type is not used. To work around this, the
217
+ ` chrono-0_4 ` feature flag can be enabled. This flag exposes a number of convenient conversions
218
+ between ` bson::DateTime ` and ` chrono::DateTime ` , including the
219
+ [ ` chrono_datetime_as_bson_datetime ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/chrono_datetime_as_bson_datetime/index.html )
220
+ serde helper, which can be used to (de)serialize ` chrono::DateTime ` s to/from BSON datetimes, and the
221
+ ` From<chrono::DateTime> ` implementation for ` Bson ` , which allows ` chrono::DateTime ` values to be
222
+ used in the ` doc! ` and ` bson! ` macros.
223
+
224
+ e.g.
225
+ ``` rust
226
+ use serde :: {Serialize , Deserialize };
227
+
228
+ #[derive(Serialize , Deserialize )]
229
+ struct Foo {
230
+ // serializes as a BSON datetime.
231
+ date_time : bson :: DateTime ,
232
+
233
+ // serializes as an RFC 3339 / ISO-8601 string.
234
+ chrono_datetime : chrono :: DateTime <chrono :: Utc >,
235
+
236
+ // serializes as a BSON datetime.
237
+ // this requires the "chrono-0_4" feature flag
238
+ #[serde(with = " bson::serde_helpers::chrono_datetime_as_bson_datetime" )]
239
+ chrono_as_bson : chrono :: DateTime <chrono :: Utc >,
240
+ }
241
+
242
+ // this automatic conversion also requires the "chrono-0_4" feature flag
243
+ let query = doc! {
244
+ " created_at" : chrono :: Utc :: now (),
245
+ };
246
+ ```
247
+
248
+ ### Working with UUIDs
249
+
250
+ The BSON format does not contain a dedicated UUID type, though it does have a "binary" type which
251
+ has UUID subtypes. Accordingly, this crate provides a
252
+ [ ` Binary ` ] ( https://docs.rs/bson/latest/bson/struct.Binary.html ) struct which models this binary type
253
+ and serializes to and deserializes from it. The popular [ ` uuid ` ] ( https://docs.rs/uuid ) crate does provide a UUID type
254
+ (` Uuid ` ), though its ` Serialize ` and ` Deserialize ` implementations operate on strings, so when using
255
+ it with BSON, the BSON binary type will not be used. To facilitate the conversion between ` Uuid `
256
+ values and BSON binary values, the ` uuid-0_8 ` feature flag can be enabled. This flag exposes a
257
+ number of convenient conversions from ` Uuid ` , including the
258
+ [ ` uuid_as_bson_binary ` ] ( https://docs.rs/bson/2.0.0-beta.3/bson/serde_helpers/uuid_as_binary/index.html )
259
+ serde helper, which can be used to (de)serialize ` Uuid ` s to/from BSON binaries with the UUID
260
+ subtype, and the ` From<Uuid> ` implementation for ` Bson ` , which allows ` Uuid ` values to be used in
261
+ the ` doc! ` and ` bson! ` macros.
262
+
263
+ e.g.
264
+
265
+ ``` rust
266
+ use serde :: {Serialize , Deserialize };
267
+
268
+ #[derive(Serialize , Deserialize )]
269
+ struct Foo {
270
+ // serializes as a String.
271
+ uuid : Uuid ,
272
+
273
+ // serializes as a BSON binary with subtype 4.
274
+ // this requires the "uuid-0_8" feature flag
275
+ #[serde(with = " bson::serde_helpers::uuid_as_binary" )]
276
+ uuid_as_bson : uuid :: Uuid ,
277
+ }
278
+
279
+ // this automatic conversion also requires the "uuid-0_8" feature flag
280
+ let query = doc! {
281
+ " uuid" : uuid :: Uuid :: new_v4 (),
282
+ };
283
+ ```
284
+
189
285
## Contributing
190
286
191
287
We encourage and would happily accept contributions in the form of GitHub pull requests. Before opening one, be sure to run the tests locally; check out the [ testing section] ( #running-the-tests ) for information on how to do that. Once you open a pull request, your branch will be run against the same testing matrix that we use for our [ continuous integration] ( #continuous-integration ) system, so it is usually sufficient to only run the integration tests locally against a standalone. Remember to always run the linter tests before opening a pull request.
0 commit comments