Deserialize

Derive Macro Deserialize 

Source
#[derive(Deserialize)]
{
    // Attributes available to this derive:
    #[xelement]
    #[xattribute]
    #[xgroup]
    #[xvalue]
}
Expand description

Derives the Deserialize trait for a type.

This macro supports deriving deserialization from elements, attributes and values.

One of the following can be applied to the root of a type:

  • #[xelement(...)] - Specifies that the type can be deserialized as an element.
  • #[xvalue(...)] - Specifies that the type can be deserialized as a value.
  • #[xattribute(...)] - Specifies that the type can be deserialized as an attribute.
  • No attribute/default behavior - Specifies that the type is a composite type. Can be deserialized from a sequence of elements.

§Modes of deserialization

Modes of deserialization depends on the type of data structure you are deserializing.

§Deserialize as an element - #[xelement(...)] on the root of a type

The #[xelement(...)] attribute can be applied to the root of a type to specify that the type can be deserialized from an element.

§Root Options
Name Type Description
name String Element name.
namespace String Must be a valid namespace string.
namespace_expr Expr Element namespace expression. This should be a value of type `xmlity::XmlNamespace`.
allow_unknown_children "any", "at_end", "none" Allow unknown children when deserializing.
- "any": Allow any unknown children.
- "at_end" (default): Allow unknown children only at the end of the element.
- "none": Do not allow unknown children at all.
allow_unknown_attributes "any", "at_end", "none" Allow unknown attributes when deserializing.
- "any": Allow any unknown attributes.
- "at_end" (default): Allow unknown attributes only at the end of the element.
- "none": Do not allow unknown attributes at all.
deserialize_any_name bool Allow any name for the element when deserializing.
attribute_order "strict", "none" Set if the order of attributes is important when deserializing.
- "strict": The order of attributes must match the order in the struct or enum variant.
- "none" (default): The order of attributes does not matter, but the attributes must be present.
children_order "strict", "none" Set if the order of children is important when deserializing.
- "strict": The order of children must match the order in the struct or enum variant.
- "none" (default): The order of children does not matter, but the children must be present.
ignore_whitespace "any", "none" Set if whitespace should be ignored when deserializing.
- "any" (default): Ignore any whitespace.
- "none": Do not ignore whitespace.
ignore_comments "any", "none" Set if comments should be ignored when deserializing.
- "any" (default): Ignore any comments.
- "none": Do not ignore comments.

§Deserialize from a sequence - structs with #[xvalue(...)] on the root of a type or no root attribute

The #[xvalue(...)] attribute can be applied to the root of a type to specify that the type can be deserialized from a text or CDATA node.

§Root options
Name Type Description
value String The text value to serialize to and deserialize from. If the type is a unit struct, this attribute can be used to specify a text value to deserialize from.
ignore_whitespace "any", "none" Set if whitespace should be ignored when deserializing.
- "any" (default): Ignore any whitespace.
- "none": Do not ignore whitespace.
ignore_comments "any", "none" Set if comments should be ignored when deserializing.
- "any" (default): Ignore any comments.
- "none": Do not ignore comments.
allow_unknown "any", "at_end", "none" Allow unknown values when deserializing.
- "any": Allow any unknown values.
- "at_end" (default): Allow unknown values only at the end of the element.
- "none": Do not allow unknown values at all.
order "strict", "none" Set if the order of values is important when deserializing.
- "strict": The order of values must match the order in the struct or enum variant.
- "none" (default): The order of values does not matter, but the values must be present.
with Path The path to the module that provides the serialization and deserialization functions. ::serialize and ::deserialize will be appended to this path and used as the serialize_with and deserialize_with functions.
serialize_with Expr Use function to serialize the value. Should have signature like pub fn serialize<S: xmlity::Serializer>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
deserialize_with Expr Use function to deserialize the value. Should have signature like fn deserialize<'de, D: xmlity::Deserializer<'de>>(deserializer: D) -> Result<T, D::Error>

§Deserialize from an attribute - structs with #[xattribute(...)] on the root of a type

The #[xattribute(...)] attribute can be applied to the root of a type to specify that the type can be deserialized from an attribute.

§Options
Name Type Description
name String Attribute name. If not specified, the name of the struct is used.
namespace String The namespace of the attribute, defined as a string. This is exclusive with namespace_expr. If none of these are specified, the absence of a namespace is assumed. Must be a valid namespace string.
namespace_expr Expr The namespace of the attribute given as an expression to an xmlity::XmlNamespace value. This is exclusive with namespace. If none of these are specified, the absence of a namespace is assumed.
preferred_prefix String The preferred prefix for the attribute, defined as a string. This is exclusive with enforce_prefix. If none of these are specified, the absence of a prefix is assumed. Must be a valid XML prefix. (Serialize only)
enforce_prefix bool Always set the prefix of the attribute to the prefix set in preferred_prefix. (Serialize only)
deserialize_any_name bool Allow any name for the attribute when deserializing.

§Deserialize as one of several types - enums with #[xvalue(...)] on the root of a type or no root attribute

The #[xvalue(...)] attribute can be applied to the root of an enum to specify that the type can be deserialized to one of several types.

§Root options
Name Type Description
rename_all "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE", "kebab-case", "SCREAMING-KEBAB-CASE" The text casing to use for unit variants when deserializing if they don't have values specified.
with Path The path to the module that provides the serialization and deserialization functions. ::serialize and ::deserialize will be appended to this path and used as the serialize_with and deserialize_with functions.
serialize_with Expr Use function to serialize the value. Should have signature like pub fn serialize<S: xmlity::Serializer>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
deserialize_with Expr Use function to deserialize the value. Should have signature like fn deserialize<'de, D: xmlity::Deserializer<'de>>(deserializer: D) -> Result<T, D::Error>
§Variant options

Variants have the same options as struct roots, and indeed work the same way.