#[derive(SerializeDict)]
{
// Attributes available to this derive:
#[zbus]
#[zvariant]
}
Expand description
Adds Serialize
implementation to structs to be serialized as a{sv}
type.
This macro serializes the deriving struct as a D-Bus dictionary type, where keys are strings and values are generic values. Such dictionary types are very commonly used with D-Bus and GVariant.
§Alternative Approaches
There are two approaches to serializing structs as dictionaries:
- Using this macro (simpler, but less control).
- Using the
Serialize
derive withzvariant::as_value
(more verbose, but more control).
See the example below and the relevant FAQ entry in our book for more details on the alternative approach.
§Example
§Approach #1
use zvariant::{SerializeDict, Type};
#[derive(Debug, Default, SerializeDict, Type)]
#[zvariant(signature = "a{sv}", rename_all = "PascalCase")]
pub struct MyStruct {
field1: Option<u32>,
field2: String,
}
§Approach #2
use serde::Serialize;
use zvariant::{Type, as_value};
#[derive(Debug, Default, Serialize, Type)]
#[zvariant(signature = "a{sv}")]
#[serde(default, rename_all = "PascalCase")]
pub struct MyStruct {
#[serde(with = "as_value::optional", skip_serializing_if = "Option::is_none")]
field1: Option<u32>,
#[serde(with = "as_value")]
field2: String,
}