Derive Macro DeserializeDict

Source
#[derive(DeserializeDict)]
{
    // Attributes available to this derive:
    #[zbus]
    #[zvariant]
}
Expand description

Adds Deserialize implementation to structs to be deserialized from a{sv} type.

This macro deserializes a D-Bus dictionary type as a struct, 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 deserializing dictionaries as structs:

  1. Using this macro (simpler, but less control).
  2. Using the Deserialize derive with zvariant::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::{DeserializeDict, Type};

#[derive(Debug, Default, DeserializeDict, Type)]
#[zvariant(signature = "a{sv}", rename_all = "PascalCase")]
pub struct MyStruct {
    field1: Option<u32>,
    field2: String,
}

§Approach #2

use serde::Deserialize;
use zvariant::{Type, as_value};

#[derive(Debug, Default, Deserialize, Type)]
#[zvariant(signature = "a{sv}")]
#[serde(default, rename_all = "PascalCase")]
pub struct MyStruct {
    #[serde(with = "as_value::optional")]
    field1: Option<u32>,
    #[serde(with = "as_value")]
    field2: String,
}