Skip to content

Commit

Permalink
Merge pull request #26 from sile/support-json-deserialize
Browse files Browse the repository at this point in the history
Fix deserialization error when using `serde_json`
  • Loading branch information
sile authored Apr 13, 2023
2 parents b38d85a + c4921e4 commit d4e5d4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = { version = "1", optional = true }
clap = { version = "4", features = ["derive"] }
postcard = { version = "1", features = ["use-std"] }
rand = "0.8"
serde_json = { version = "1" }

[package.metadata.docs.rs]
all-features = true
Expand Down
28 changes: 28 additions & 0 deletions src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ impl<'de> Visitor<'de> for BytesVisitor {
{
Ok(Bytes(Cow::Owned(v.to_owned())))
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = Vec::new();
while let Some(byte) = seq.next_element()? {
bytes.push(byte);
}
Ok(Bytes(Cow::Owned(bytes)))
}
}

#[cfg(test)]
Expand All @@ -240,6 +251,23 @@ mod tests {
assert_eq!(map.into_iter().collect::<Vec<_>>(), input);
}

#[test]
fn serde_json_works() {
let mut input = vec![
(Vec::from("foo"), 1u32),
("bar".into(), 2),
("baz".into(), 3),
];
input.sort();

let map: PatriciaMap<u32> = input.iter().cloned().collect();
let json = serde_json::to_string(&map).unwrap();
let map: PatriciaMap<u32> = serde_json::from_str(&json).unwrap();

assert_eq!(map.len(), 3);
assert_eq!(map.into_iter().collect::<Vec<_>>(), input);
}

#[test]
fn large_serde_works() {
let mut input = (0..10000u32)
Expand Down

0 comments on commit d4e5d4a

Please sign in to comment.