alloy_primitives/bytes/
serde.rs1use crate::Bytes;
2use alloc::vec::Vec;
3use core::fmt;
4use serde::de::{self, Visitor};
5
6impl serde::Serialize for Bytes {
7 #[inline]
8 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
9 if serializer.is_human_readable() {
10 hex::serialize(self, serializer)
11 } else {
12 serializer.serialize_bytes(self.as_ref())
13 }
14 }
15}
16
17impl<'de> serde::Deserialize<'de> for Bytes {
18 #[inline]
19 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
20 struct BytesVisitor;
21
22 impl<'de> Visitor<'de> for BytesVisitor {
23 type Value = Bytes;
24
25 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26 formatter.write_str("a variable number of bytes represented as a hex string, an array of u8, or raw bytes")
27 }
28
29 fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Self::Value, E> {
30 Ok(Bytes::from(v.to_vec()))
31 }
32
33 fn visit_byte_buf<E: de::Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
34 Ok(Bytes::from(v))
35 }
36
37 fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
38 let mut bytes = Vec::with_capacity(seq.size_hint().unwrap_or(0));
39
40 while let Some(byte) = seq.next_element()? {
41 bytes.push(byte);
42 }
43
44 Ok(Bytes::from(bytes))
45 }
46
47 fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
48 hex::decode(v)
49 .map_err(|_| {
50 de::Error::invalid_value(de::Unexpected::Str(v), &"a valid hex string")
51 })
52 .map(From::from)
53 }
54 }
55
56 if deserializer.is_human_readable() {
57 deserializer.deserialize_any(BytesVisitor)
58 } else {
59 deserializer.deserialize_byte_buf(BytesVisitor)
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67 use serde::Deserialize;
68
69 #[derive(Debug, Deserialize)]
70 struct TestCase {
71 variable: Bytes,
72 }
73
74 #[test]
75 fn serde() {
76 let bytes = Bytes::from_static(&[1, 35, 69, 103, 137, 171, 205, 239]);
77 let ser = serde_json::to_string(&bytes).unwrap();
78 assert_eq!(ser, "\"0x0123456789abcdef\"");
79 assert_eq!(serde_json::from_str::<Bytes>(&ser).unwrap(), bytes);
80
81 let val = serde_json::to_value(&bytes).unwrap();
82 assert_eq!(val, serde_json::json! {"0x0123456789abcdef"});
83 assert_eq!(serde_json::from_value::<Bytes>(val).unwrap(), bytes);
84 }
85
86 #[test]
87 fn serde_num_array() {
88 let json = serde_json::json! {{"variable": [0,1,2,3,4]}};
89
90 assert_eq!(
91 serde_json::from_value::<TestCase>(json).unwrap().variable,
92 Bytes::from_static(&[0, 1, 2, 3, 4])
93 );
94 }
95
96 #[test]
97 fn test_bincode_roundtrip() {
98 let bytes = Bytes::from_static(&[1, 35, 69, 103, 137, 171, 205, 239]);
99
100 let bin = bincode::serialize(&bytes).unwrap();
101 assert_eq!(bincode::deserialize::<Bytes>(&bin).unwrap(), bytes);
102 }
103}