Skip to content

RUST-1748 Convert serde helpers to serde_with::(De)SerializeAs implementations #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exclude = [
]

[features]
default = ["compat-3-0-0"]
default = ["compat-3-0-0", "chrono-0_4", "serde_with-3", "serde"]
compat-3-0-0 = []
# if enabled, include API for interfacing with chrono 0.4
chrono-0_4 = ["dep:chrono"]
Expand All @@ -46,7 +46,7 @@ time-0_3 = []
serde_path_to_error = ["dep:serde_path_to_error"]
# if enabled, include serde_with interop.
# should be used in conjunction with chrono-0_4 or uuid-0_8.
serde_with-3 = ["dep:serde_with"]
serde_with-3 = ["dep:serde_with", "dep:serde"]
serde = ["dep:serde"]

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

e.g.
``` rust
use serde::{Serialize, Deserialize};
use serde_with::serde_as;

#[serde_as]
#[derive(Serialize, Deserialize)]
struct Foo {
// serializes as a BSON datetime.
Expand All @@ -248,7 +250,7 @@ struct Foo {

// serializes as a BSON datetime.
// this requires the "chrono-0_4" feature flag
#[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
#[serde_as(as = "bson::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime")]
chrono_as_bson: chrono::DateTime<chrono::Utc>,
}

Expand Down
17 changes: 11 additions & 6 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ use crate::error::{Error, Result};
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
/// various datetime types to and from different formats. For example, to serialize a
/// [`chrono::DateTime`] as a BSON datetime, you can use
/// [`crate::serde_helpers::chrono_datetime_as_bson_datetime`]. Similarly, to serialize a BSON
/// [`DateTime`] to a string, you can use [`crate::serde_helpers::bson_datetime_as_rfc3339_string`].
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
/// offered by the crate.
/// [`crate::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime`].
/// Similarly, to serialize a BSON [`DateTime`] to a string, you can use
/// [`crate::serde_helpers::date_time::BsonDateTimeAsRfc3339String`]. Check out the
/// [`crate::serde_helpers`] module documentation for a list of all of the helpers offered by the
/// crate.
///
/// ```rust
/// # #[cfg(feature = "chrono-0_4")]
/// # {
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use bson::serde_helpers::date_time::BsonDateTimeAsRfc3339String;
/// use bson::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime;
///
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Foo {
/// // serializes as a BSON datetime.
Expand All @@ -130,11 +135,11 @@ use crate::error::{Error, Result};
///
/// // serializes as a BSON datetime.
/// // this requires the "chrono-0_4" feature flag
/// #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
/// #[serde_as(as = "ChronoDateTimeAsBsonDateTime")]
/// chrono_as_bson: chrono::DateTime<chrono::Utc>,
///
/// // serializes as an RFC 3339 / ISO-8601 string.
/// #[serde(with = "bson::serde_helpers::bson_datetime_as_rfc3339_string")]
/// #[serde_as(as = "BsonDateTimeAsRfc3339String")]
/// bson_as_string: bson::DateTime,
/// }
/// # }
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
//! on strings instead, so when using it with BSON, the BSON datetime type is not used. To work
//! around this, the `chrono-0_4` feature flag can be enabled. This flag exposes a number of
//! convenient conversions between [`bson::DateTime`](crate::DateTime) and [`chrono::DateTime`],
//! including the [`serde_helpers::chrono_datetime_as_bson_datetime`]
//! including the [`serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime`]
//! serde helper, which can be used to (de)serialize [`chrono::DateTime`]s to/from BSON datetimes,
//! and the `From<chrono::DateTime>` implementation for [`Bson`], which allows [`chrono::DateTime`]
//! values to be used in the `doc!` and `bson!` macros.
Expand All @@ -243,8 +243,10 @@
//! # #[cfg(feature = "chrono-0_4")]
//! # {
//! use serde::{Serialize, Deserialize};
//! use serde_with::serde_as;
//! use bson::doc;
//!
//! #[serde_as]
//! #[derive(Serialize, Deserialize)]
//! struct Foo {
//! // serializes as a BSON datetime.
Expand All @@ -255,7 +257,7 @@
//!
//! // serializes as a BSON datetime.
//! // this requires the "chrono-0_4" feature flag
//! #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
//! #[serde_as(as = "bson::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime")]
//! chrono_as_bson: chrono::DateTime<chrono::Utc>,
//! }
//!
Expand Down
7 changes: 5 additions & 2 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,25 @@ static OID_COUNTER: Lazy<AtomicUsize> =
/// The `bson` crate provides a number of useful helpers for serializing and deserializing
/// various types to and from different formats. For example, to serialize an
/// [`ObjectId`] as a hex string, you can use
/// [`crate::serde_helpers::serialize_object_id_as_hex_string`].
/// [`crate::serde_helpers::object_id::ObjectIdAsHexString`].
/// Check out the [`crate::serde_helpers`] module documentation for a list of all of the helpers
/// offered by the crate.
///
/// e.g.
/// ```rust
/// use serde::{Serialize, Deserialize};
/// use serde_with::serde_as;
/// use bson::oid::ObjectId;
/// use bson::serde_helpers::object_id::ObjectIdAsHexString;
///
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Foo {
/// // Serializes as a BSON ObjectId or extJSON in non-BSON formats
/// oid: ObjectId,
///
/// // Serializes as a hex string in all formats
/// #[serde(serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string")]
/// #[serde_as(as = "ObjectIdAsHexString")]
/// oid_as_hex: ObjectId,
/// }
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
Expand Down
Loading