Skip to content

RUST-1748 Convert serde helpers to serde_with::(De)SerializeAs implementations for some converters between ObjectId, String, and DateTime #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

Merged
merged 23 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
379ac68
Convert to serde_with helpers for ObjectId and DateTime to String
JamieTsai1024 Jun 24, 2025
5ecd554
Update tests
JamieTsai1024 Jun 24, 2025
4d6dddf
Update rustdoc
JamieTsai1024 Jun 24, 2025
f4c4d8d
Fix typo
JamieTsai1024 Jun 24, 2025
b408e0d
Update tests for optional and vector struct fields
JamieTsai1024 Jun 25, 2025
32c6e39
Add serde_with converter for Chrono to BSON DateTime
JamieTsai1024 Jun 26, 2025
ba9ae6f
Put new implementations behind feature flag, add additional tests for…
JamieTsai1024 Jun 27, 2025
09689f4
Fix imports
JamieTsai1024 Jun 27, 2025
832d582
Fix lint
JamieTsai1024 Jun 27, 2025
e351243
Remove clone
JamieTsai1024 Jun 27, 2025
35af075
Update imports for documentation
JamieTsai1024 Jun 27, 2025
092c270
Fix clippy and rustdoc
JamieTsai1024 Jun 27, 2025
94a489f
Merge branch 'main' into RUST-1748
JamieTsai1024 Jun 27, 2025
b69b21a
Add serde as a dependency for serde_with-3
JamieTsai1024 Jun 27, 2025
6193860
Simplify documentation import
JamieTsai1024 Jun 30, 2025
d6d9de6
Add error path tests for string conversions
JamieTsai1024 Jul 1, 2025
f1d8d50
Remove tests for empty optional fields with skip_serializing_if
JamieTsai1024 Jul 1, 2025
f4a8857
Remove optional converter for Chrono and BSON DateTime
JamieTsai1024 Jul 1, 2025
11568ac
Simplify module and struct names for better ergonomics
JamieTsai1024 Jul 1, 2025
e26879e
Refactor serialized vector tests
JamieTsai1024 Jul 2, 2025
68fefb4
Remove default feature flags
JamieTsai1024 Jul 2, 2025
d59c7d5
Update import in readme
JamieTsai1024 Jul 2, 2025
d31833b
Fix import documentation
JamieTsai1024 Jul 2, 2025
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 @@ -44,7 +44,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 = []

[lib]
name = "bson"
Expand All @@ -61,7 +61,7 @@ base64 = "0.22.1"
once_cell = "1.5.1"
uuid = { version = "1.1.2", features = ["serde", "v4"] }
serde_bytes = "0.11.5"
serde_with = { version = "3.1.0", optional = true }
serde_with = { version = "3.1.0" }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
bitvec = "1.0.1"
serde_path_to_error = { version = "0.1.16", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ 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.
Expand All @@ -247,7 +247,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(with = "bson::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime")]
chrono_as_bson: chrono::DateTime<chrono::Utc>,
}

Expand Down
14 changes: 8 additions & 6 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,18 @@ use serde::{Deserialize, Deserializer, Serialize};
/// 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::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;
///
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
/// struct Foo {
/// // serializes as a BSON datetime.
Expand All @@ -131,11 +133,11 @@ use serde::{Deserialize, Deserializer, Serialize};
///
/// // serializes as a BSON datetime.
/// // this requires the "chrono-0_4" feature flag
/// #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
/// #[serde(with = "bson::serde_helpers::chrono_datetime_and_bson_datetime::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 = "bson::serde_helpers::BsonDatetimeAsRfc3339String")]
/// bson_as_string: bson::DateTime,
/// }
/// # }
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,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 @@ -254,7 +254,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(with = "bson::serde_helpers::chrono_datetime_and_bson_datetime::ChronoDateTimeAsBsonDateTime")]
//! chrono_as_bson: chrono::DateTime<chrono::Utc>,
//! }
//!
Expand Down
6 changes: 4 additions & 2 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,24 @@ impl error::Error for Error {}
/// 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::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;
///
/// #[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 = "bson::serde_helpers::ObjectIdAsHexString")]
/// oid_as_hex: ObjectId,
/// }
/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
Expand Down
Loading