1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use alloy_primitives::U256;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

pub fn serialize<S>(num: &U256, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    num.to_string().serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    U256::from_str_radix(&s, 10).map_err(|e| de::Error::custom(format!("Invalid U256 string: {e}")))
}

#[cfg(test)]
mod test {
    use alloy_primitives::U256;
    use serde::{Deserialize, Serialize};
    use serde_json;

    #[derive(Debug, PartialEq, Serialize, Deserialize)]
    #[serde(transparent)]
    struct Wrapper {
        #[serde(with = "super")]
        val: U256,
    }

    #[test]
    fn encoding() {
        assert_eq!(
            &serde_json::to_string(&Wrapper { val: U256::from(0) }).unwrap(),
            "\"0\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper { val: U256::from(1) }).unwrap(),
            "\"1\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper {
                val: U256::from(256)
            })
            .unwrap(),
            "\"256\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper {
                val: U256::from(65)
            })
            .unwrap(),
            "\"65\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper {
                val: U256::from(1024)
            })
            .unwrap(),
            "\"1024\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper {
                val: U256::MAX - U256::from(1)
            })
            .unwrap(),
            "\"115792089237316195423570985008687907853269984665640564039457584007913129639934\""
        );
        assert_eq!(
            &serde_json::to_string(&Wrapper { val: U256::MAX }).unwrap(),
            "\"115792089237316195423570985008687907853269984665640564039457584007913129639935\""
        );
    }

    #[test]
    fn decoding() {
        assert_eq!(
            serde_json::from_str::<Wrapper>("\"0\"").unwrap(),
            Wrapper { val: U256::from(0) },
        );
        assert_eq!(
            serde_json::from_str::<Wrapper>("\"65\"").unwrap(),
            Wrapper {
                val: U256::from(65)
            },
        );
        assert_eq!(
            serde_json::from_str::<Wrapper>("\"1024\"").unwrap(),
            Wrapper {
                val: U256::from(1024)
            },
        );
        assert_eq!(
            serde_json::from_str::<Wrapper>(
                "\"115792089237316195423570985008687907853269984665640564039457584007913129639934\""
            )
            .unwrap(),
            Wrapper {
                val: U256::MAX - U256::from(1)
            },
        );
        assert_eq!(
            serde_json::from_str::<Wrapper>(
                "\"115792089237316195423570985008687907853269984665640564039457584007913129639935\""
            )
            .unwrap(),
            Wrapper {
                val: U256::MAX
            },
        );
        serde_json::from_str::<Wrapper>("\"0x0\"").unwrap_err();
        serde_json::from_str::<Wrapper>("\"0x0400\"").unwrap_err();
        serde_json::from_str::<Wrapper>("\"-1\"").unwrap_err();
        serde_json::from_str::<Wrapper>("\"ff\"").unwrap_err();
    }
}