Skip to content

Commit 95774f9

Browse files
RUST-1748 Convert serde helpers to serde_with::(De)SerializeAs implementations for some converters between ObjectId, String, and DateTime (#559)
1 parent 59c347f commit 95774f9

File tree

7 files changed

+766
-353
lines changed

7 files changed

+766
-353
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ time-0_3 = []
4646
serde_path_to_error = ["dep:serde_path_to_error"]
4747
# if enabled, include serde_with interop.
4848
# should be used in conjunction with chrono-0_4 or uuid-0_8.
49-
serde_with-3 = ["dep:serde_with"]
49+
serde_with-3 = ["dep:serde_with", "dep:serde"]
5050
serde = ["dep:serde"]
5151

5252
[lib]

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,17 @@ provides a `DateTime` type, but its `Serialize` and `Deserialize` implementation
229229
instead, so when using it with BSON, the BSON datetime type is not used. To work around this, the
230230
`chrono-0_4` feature flag can be enabled. This flag exposes a number of convenient conversions
231231
between `bson::DateTime` and `chrono::DateTime`, including the
232-
[`chrono_datetime_as_bson_datetime`](https://docs.rs/bson/latest/bson/serde_helpers/chrono_datetime_as_bson_datetime/index.html)
232+
[`bson_datetime::FromChronoDateTime`](https://docs.rs/bson/latest/bson/serde_helpers/bson_datetime/struct.FromChronoDateTime/index.html)
233233
serde helper, which can be used to (de)serialize `chrono::DateTime`s to/from BSON datetimes, and the
234234
`From<chrono::DateTime>` implementation for `Bson`, which allows `chrono::DateTime` values to be
235235
used in the `doc!` and `bson!` macros.
236236

237237
e.g.
238238
``` rust
239239
use serde::{Serialize, Deserialize};
240+
use serde_with::serde_as;
240241

242+
#[serde_as]
241243
#[derive(Serialize, Deserialize)]
242244
struct Foo {
243245
// serializes as a BSON datetime.
@@ -248,7 +250,7 @@ struct Foo {
248250

249251
// serializes as a BSON datetime.
250252
// this requires the "chrono-0_4" feature flag
251-
#[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
253+
#[serde_as(as = "bson::serde_helpers::bson_datetime::FromChronoDateTime")]
252254
chrono_as_bson: chrono::DateTime<chrono::Utc>,
253255
}
254256

src/datetime.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,20 @@ use crate::error::{Error, Result};
110110
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
111111
/// various datetime types to and from different formats. For example, to serialize a
112112
/// [`chrono::DateTime`] as a BSON datetime, you can use
113-
/// [`crate::serde_helpers::chrono_datetime_as_bson_datetime`]. Similarly, to serialize a BSON
114-
/// [`DateTime`] to a string, you can use [`crate::serde_helpers::bson_datetime_as_rfc3339_string`].
115-
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
116-
/// offered by the crate.
113+
/// [`crate::serde_helpers::bson_datetime::FromChronoDateTime`].
114+
/// Similarly, to serialize a BSON [`DateTime`] to a string, you can use
115+
/// [`crate::serde_helpers::bson_datetime::AsRfc3339String`]. Check out the
116+
/// [`crate::serde_helpers`] module documentation for a list of all of the helpers offered by the
117+
/// crate.
117118
///
118119
/// ```rust
119120
/// # #[cfg(feature = "chrono-0_4")]
120121
/// # {
121122
/// use serde::{Serialize, Deserialize};
123+
/// use serde_with::serde_as;
124+
/// use bson::serde_helpers::bson_datetime;
122125
///
126+
/// #[serde_as]
123127
/// #[derive(Serialize, Deserialize)]
124128
/// struct Foo {
125129
/// // serializes as a BSON datetime.
@@ -130,11 +134,11 @@ use crate::error::{Error, Result};
130134
///
131135
/// // serializes as a BSON datetime.
132136
/// // this requires the "chrono-0_4" feature flag
133-
/// #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
137+
/// #[serde_as(as = "bson_datetime::FromChronoDateTime")]
134138
/// chrono_as_bson: chrono::DateTime<chrono::Utc>,
135139
///
136140
/// // serializes as an RFC 3339 / ISO-8601 string.
137-
/// #[serde(with = "bson::serde_helpers::bson_datetime_as_rfc3339_string")]
141+
/// #[serde_as(as = "bson_datetime::AsRfc3339String")]
138142
/// bson_as_string: bson::DateTime,
139143
/// }
140144
/// # }

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
//! on strings instead, so when using it with BSON, the BSON datetime type is not used. To work
234234
//! around this, the `chrono-0_4` feature flag can be enabled. This flag exposes a number of
235235
//! convenient conversions between [`bson::DateTime`](crate::DateTime) and [`chrono::DateTime`],
236-
//! including the [`serde_helpers::chrono_datetime_as_bson_datetime`]
236+
//! including the [`serde_helpers::bson_datetime::FromChronoDateTime`]
237237
//! serde helper, which can be used to (de)serialize [`chrono::DateTime`]s to/from BSON datetimes,
238238
//! and the `From<chrono::DateTime>` implementation for [`Bson`], which allows [`chrono::DateTime`]
239239
//! values to be used in the `doc!` and `bson!` macros.
@@ -243,8 +243,11 @@
243243
//! # #[cfg(feature = "chrono-0_4")]
244244
//! # {
245245
//! use serde::{Serialize, Deserialize};
246+
//! use serde_with::serde_as;
246247
//! use bson::doc;
248+
//! use bson::serde_helpers::bson_datetime;
247249
//!
250+
//! #[serde_as]
248251
//! #[derive(Serialize, Deserialize)]
249252
//! struct Foo {
250253
//! // serializes as a BSON datetime.
@@ -255,7 +258,7 @@
255258
//!
256259
//! // serializes as a BSON datetime.
257260
//! // this requires the "chrono-0_4" feature flag
258-
//! #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
261+
//! #[serde_as(as = "bson_datetime::FromChronoDateTime")]
259262
//! chrono_as_bson: chrono::DateTime<chrono::Utc>,
260263
//! }
261264
//!

src/oid.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,25 @@ static OID_COUNTER: Lazy<AtomicUsize> =
6262
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
6363
/// various types to and from different formats. For example, to serialize an
6464
/// [`ObjectId`] as a hex string, you can use
65-
/// [`crate::serde_helpers::serialize_object_id_as_hex_string`].
65+
/// [`crate::serde_helpers::object_id::AsHexString`].
6666
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
6767
/// offered by the crate.
6868
///
6969
/// e.g.
7070
/// ```rust
7171
/// use serde::{Serialize, Deserialize};
72+
/// use serde_with::serde_as;
7273
/// use bson::oid::ObjectId;
74+
/// use bson::serde_helpers::object_id;
7375
///
76+
/// #[serde_as]
7477
/// #[derive(Serialize, Deserialize)]
7578
/// struct Foo {
7679
/// // Serializes as a BSON ObjectId or extJSON in non-BSON formats
7780
/// oid: ObjectId,
7881
///
7982
/// // Serializes as a hex string in all formats
80-
/// #[serde(serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string")]
83+
/// #[serde_as(as = "object_id::AsHexString")]
8184
/// oid_as_hex: ObjectId,
8285
/// }
8386
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

0 commit comments

Comments
 (0)