Derive Macro zvariant::SerializeDict[][src]

    // Attributes available to this derive:
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.


For structs it works just like serde’s Serialize macros:

use zvariant::{EncodingContext, to_bytes, SerializeDict, TypeDict};

#[derive(SerializeDict, TypeDict)]
struct Struct {
    field1: u16,
    #[zvariant(rename = "another-name")]
    field2: i64,
    optional_field: Option<String>,

The serialized D-Bus version of Struct {42, 77, None} will be {"field1": Value::U16(42), "another-name": Value::I64(77)}.