json_safe_fields

Attribute Macro json_safe_fields 

Source
#[json_safe_fields]
Expand description

Attribute macro that adds #[serde(with = "...")] to u64/i64 fields and implements JsonSafeFields for the type (when used inside the dpp crate).

Works on both structs and enums (with named fields in variants).

Matches literal u64, i64, Option<u64>, Option<i64>, and known type aliases (e.g. Credits, TokenAmount, TimestampMillis, BlockHeight). The macro skips fields that already have #[serde(with)].

When serde derives are behind cfg_attr(feature = "..."), the cfg_attr is evaluated by the compiler BEFORE this macro runs. If the feature is off, serde derives aren’t visible and #[serde(with)] is NOT generated — which is correct because serde(with) requires an active serde derive.

§Inside dpp crate (default)

#[json_safe_fields]
pub struct MyStructV0 {
    pub supply: u64,          // → auto-annotated with crate::serialization::json_safe_u64
    pub name: String,         // → untouched
}

§Outside dpp crate

Use crate = "..." to specify the path to the dpp serialization module. When crate is specified, the JsonSafeFields impl is NOT generated.

#[json_safe_fields(crate = "dash_sdk::dpp")]
pub struct MyWasmStruct {
    pub balance: u64,         // → annotated with dash_sdk::dpp::serialization::json_safe_u64
}