Expand description
Derivation of Serialize and Deserialize that replaces struct keys with numerical indices.
§Usage example
§Struct attributes
auto_index: Automatically assign indices to the fields based on the order in the source code. It is recommended to instead use theindexattribute for all fields to explicitly assign indices.offset = ?: Ifauto_indexis set, use the given index for the first field instead of starting with zero.
§Field attributes
index = ?: Set the index for this field to the given field. This attribute is required unlessauto_indexis set. It cannot be used together withauto_index.skip: Never serialize or deserialize this field. This field still increases the assigned index ifauto_indexis used.skip(no_increment): Never serialize or deserialize this field and don’t increment the assigned index for this field if used together with theauto_indexattribute.
serde-indexed also supports these serde attributes:
§Generated code example
cargo expand --test basics exercises the macros using serde_cbor.
§Examples
Explicit index assignment:
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
pub struct SomeKeys {
#[serde(index = 1)]
pub number: i32,
#[serde(index = 2)]
pub option: Option<u8>,
#[serde(skip)]
pub ignored: bool,
#[serde(index = 3)]
pub bytes: [u8; 7],
}Automatic index assignment:
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[serde(auto_index)]
pub struct SomeKeys {
// index 1
pub number: i32,
// index 2
pub option: Option<u8>,
// index 3 (but skipped)
#[serde(skip)]
pub ignored: bool,
// index 4
pub bytes: [u8; 7],
}Automatic index assignment with skip(no_increment):
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[serde(auto_index)]
pub struct SomeKeys {
// index 1
pub number: i32,
// index 2
pub option: Option<u8>,
#[serde(skip(no_increment))]
pub ignored: bool,
// index 3
pub bytes: [u8; 7],
}Automatic index assignment with offset:
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[serde(auto_index, offset = 42)]
pub struct SomeKeys {
// index 42
pub number: i32,
// index 43
pub option: Option<u8>,
// index 44
pub bytes: [u8; 7],
}Skip serializing a field based on a condition with skip_serializing_if:
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
pub struct SomeKeys {
#[serde(index = 1)]
pub number: i32,
#[serde(index = 2, skip_serializing_if = "Option::is_none")]
pub option: Option<u8>,
#[serde(index = 3)]
pub bytes: [u8; 7],
}Change the serialization or deserialization format with deserialize_with, serialize_with or with:
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
pub struct SomeKeys<'a> {
#[serde(index = 1, serialize_with = "serde_bytes::serialize")]
pub one: &'a [u8],
#[serde(index = 2, deserialize_with = "serde_bytes::deserialize")]
pub two: &'a [u8],
#[serde(index = 3, with = "serde_bytes")]
pub three: &'a [u8],
}