alloy_json_abi

Module serde_state_mutability_compat

source
Expand description

serde implementation for StateMutability for backwards compatibility with older versions of the JSON ABI.

In particular, this will deserialize the stateMutability field if it is present, and otherwise fall back to the deprecated constant and payable fields.

Since it must be used in combination with #[serde(flatten)], a serialize implementation is also provided, which will always serialize the stateMutability field.

§Examples

Usage: #[serde(default, flatten, with = "serde_state_mutability_compat")] on a StateMutability struct field.

use alloy_sol_type_parser::{serde_state_mutability_compat, StateMutability};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MyStruct {
    #[serde(default, flatten, with = "serde_state_mutability_compat")]
    state_mutability: StateMutability,
}

let json = r#"{"constant":true,"payable":false}"#;
let ms = serde_json::from_str::<MyStruct>(json).expect("failed deserializing");
assert_eq!(ms.state_mutability, StateMutability::View);

let reserialized = serde_json::to_string(&ms).expect("failed reserializing");
assert_eq!(reserialized, r#"{"stateMutability":"view"}"#);

Functions§