Field attributes
#[serde(rename = "name")]
Serialize and deserialize this field with the given name instead of its Rust name. This is useful for serializing fields as camelCase or serializing fields with names that are reserved Rust keywords.
Allows specifying independent names for serialization vs deserialization:
#[serde(rename(serialize = "ser_name"))]
#[serde(rename(deserialize = "de_name"))]
#[serde(rename(serialize = "ser_name", deserialize = "de_name"))]
#[serde(alias = "name")]
Deserialize this field from the given name or from its Rust name. May be repeated to specify multiple possible names for the same field.
#[serde(default)]
If the value is not present when deserializing, use the
Default::default()
.#[serde(default = "path")]
If the value is not present when deserializing, call a function to get a default value. The given function must be callable as
fn() -> T
. For exampledefault = "empty_value"
would invokeempty_value()
anddefault = "SomeTrait::some_default"
would invokeSomeTrait::some_default()
.#[serde(flatten)]
Flatten the contents of this field into the container it is defined in.
This removes one level of structure between the serialized representation and the Rust data structure representation. It can be used for factoring common keys into a shared structure, or for capturing remaining fields into a map with arbitrary string keys. The struct flattening page provides some examples.
Note: this attribute is not supported in combination with structs that use
deny_unknown_fields
. Neither the outer nor inner flattened struct should use that attribute.
#[serde(skip)]
Skip this field: do not serialize or deserialize it.
When deserializing, Serde will use
Default::default()
or the function given bydefault = "..."
to get a default value for this field.#[serde(skip_serializing)]
Skip this field when serializing, but not when deserializing.
#[serde(skip_deserializing)]
Skip this field when deserializing, but not when serializing.
When deserializing, Serde will use
Default::default()
or the function given bydefault = "..."
to get a default value for this field.#[serde(skip_serializing_if = "path")]
Call a function to determine whether to skip serializing this field. The given function must be callable as
fn(&T) -> bool
, although it may be generic overT
. For exampleskip_serializing_if = "Option::is_none"
would skip an Option that is None.#[serde(serialize_with = "path")]
Serialize this field using a function that is different from its implementation of
Serialize
. The given function must be callable asfn<S>(&T, S) -> Result<S::Ok, S::Error> where S: Serializer
, although it may also be generic overT
. Fields used withserialize_with
are not required to implementSerialize
.#[serde(deserialize_with = "path")]
Deserialize this field using a function that is different from its implementation of
Deserialize
. The given function must be callable asfn<'de, D>(D) -> Result<T, D::Error> where D: Deserializer<'de>
, although it may also be generic overT
. Fields used withdeserialize_with
are not required to implementDeserialize
.#[serde(with = "module")]
Combination of
serialize_with
anddeserialize_with
. Serde will use$module::serialize
as theserialize_with
function and$module::deserialize
as thedeserialize_with
function.#[serde(borrow)]
and#[serde(borrow = "'a + 'b + ...")]
Borrow data for this field from the deserializer by using zero-copy deserialization. See this example.
#[serde(bound = "T: MyTrait")]
Where-clause for the
Serialize
andDeserialize
impls. This replaces any trait bounds inferred by Serde for the current field.Allows specifying independent bounds for serialization vs deserialization:
#[serde(bound(serialize = "T: MySerTrait"))]
#[serde(bound(deserialize = "T: MyDeTrait"))]
#[serde(bound(serialize = "T: MySerTrait", deserialize = "T: MyDeTrait"))]
#[serde(getter = "...")]
This is used when deriving
Serialize
for a remote type that has one or more private fields.