Available on crate feature
serde only.Expand description
Collection of helper functions for serializing to and deserializing from BSON using Serde.
The submodules here provide converter types that can be used with the #[serde(with = ...)]
annotation. These modules follow a naming convention:
- module name - the base type to be converted
- module
::AsFoo- when serializing/deserializing a field of the base type, store it as a Foo value - module
::FromFoo- when serializing/deserializing a field of type Foo, store it as a value of the base type
For example, the object_id module provides both object_id::AsHexString and
object_id::FromHexString:
use bson::{doc, serde_helpers::object_id, oid::ObjectId};
#[derive(Deserialize, Serialize)]
struct Example {
// No conversions applied; will serialize as the BSON value.
basic: ObjectId,
// In code, an ObjectId; when serialized, a hex string.
#[serde(with = "object_id::AsHexString")]
as_hex: ObjectId,
// In code, a hex string; serializes as a BSON objectid.
#[serde(with = "object_id::FromHexString")]
from_hex: String,
}If the serde_with-3 feature is enabled, these converters can also be used with the
#[serde_as(as = ...)] annotation, which provides similar conversion functionality with the
added flexibility of handling many container types automatically:
use bson::{doc, serde_helpers::object_id, oid::ObjectId};
#[serde_with::serde_as]
#[derive(Deserialize, Serialize)]
struct Example {
#[serde_as(as = "Option<object_id::AsHexString>")]
optional: Option<ObjectId>,
}See the crate documentation for serde_with for more details.
Modules§
- datetime
- Type converters for serializing and deserializing
crate::DateTime. - object_
id - Type converters for serializing and deserializing
crate::oid::ObjectId. - timestamp
- Type converters for serializing and deserializing
crate::Timestamp. - u32
- Type converters for serializing and deserializing
u32. - u64
- Type converters for serializing and deserializing
u64. - uuid_1
uuid-1 - Type converters for serializing and deserializing
uuid::Uuid.
Structs§
- Human
Readable - Wrapping a type in
HumanReadablesignals to the BSON serde integration that it and all recursively contained types should be serialized to and deserialized from their human-readable formats.