alloy_serde/
bool.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// Serde serialization and deserialization for [`bool`] as `0x0` or `0x1`.
#[deprecated = "use `quantity::bool` instead"]
pub mod quantity_bool {
    use alloy_primitives::aliases::U1;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    /// Deserializes a [`bool`] via a [U1] quantity.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<bool, D::Error>
    where
        D: Deserializer<'de>,
    {
        U1::deserialize(deserializer).map(|val| val.to())
    }

    /// Serializes a [`bool`] via a [U1] quantity.
    pub fn serialize<S: Serializer>(val: &bool, s: S) -> Result<S::Ok, S::Error> {
        if *val {
            "0x1".serialize(s)
        } else {
            "0x0".serialize(s)
        }
    }
}