serde_utils: Only use special serialization for human-readable

Non-human-readable serialization (binary) doesn't need hexification.
This commit is contained in:
Steven Roose 2020-12-27 21:15:41 +00:00 committed by Sebastian
parent 5fd92d1c09
commit a8f2faf96d
1 changed files with 60 additions and 20 deletions

View File

@ -18,12 +18,17 @@ pub mod btreemap_byte_values {
{ {
use serde::ser::SerializeMap; use serde::ser::SerializeMap;
// Don't do anything special when not human readable.
if !s.is_human_readable() {
serde::Serialize::serialize(v, s)
} else {
let mut map = s.serialize_map(Some(v.len()))?; let mut map = s.serialize_map(Some(v.len()))?;
for (key, value) in v.iter() { for (key, value) in v.iter() {
map.serialize_entry(key, &value.to_hex())?; map.serialize_entry(key, &value.to_hex())?;
} }
map.end() map.end()
} }
}
pub fn deserialize<'de, D, T>(d: D) pub fn deserialize<'de, D, T>(d: D)
-> Result<BTreeMap<T, Vec<u8>>, D::Error> where -> Result<BTreeMap<T, Vec<u8>>, D::Error> where
@ -53,8 +58,13 @@ pub mod btreemap_byte_values {
} }
} }
// Don't do anything special when not human readable.
if !d.is_human_readable() {
serde::Deserialize::deserialize(d)
} else {
d.deserialize_map(Visitor(PhantomData)) d.deserialize_map(Visitor(PhantomData))
} }
}
} }
pub mod btreemap_as_seq { pub mod btreemap_as_seq {
@ -75,12 +85,17 @@ pub mod btreemap_as_seq {
{ {
use serde::ser::SerializeSeq; use serde::ser::SerializeSeq;
// Don't do anything special when not human readable.
if !s.is_human_readable() {
serde::Serialize::serialize(v, s)
} else {
let mut seq = s.serialize_seq(Some(v.len()))?; let mut seq = s.serialize_seq(Some(v.len()))?;
for pair in v.iter() { for pair in v.iter() {
seq.serialize_element(&pair)?; seq.serialize_element(&pair)?;
} }
seq.end() seq.end()
} }
}
pub fn deserialize<'de, D, T, U>(d: D) pub fn deserialize<'de, D, T, U>(d: D)
-> Result<BTreeMap<T, U>, D::Error> where -> Result<BTreeMap<T, U>, D::Error> where
@ -112,8 +127,13 @@ pub mod btreemap_as_seq {
} }
} }
// Don't do anything special when not human readable.
if !d.is_human_readable() {
serde::Deserialize::deserialize(d)
} else {
d.deserialize_seq(Visitor(PhantomData)) d.deserialize_seq(Visitor(PhantomData))
} }
}
} }
pub mod btreemap_as_seq_byte_values { pub mod btreemap_as_seq_byte_values {
@ -149,12 +169,17 @@ pub mod btreemap_as_seq_byte_values {
{ {
use serde::ser::SerializeSeq; use serde::ser::SerializeSeq;
// Don't do anything special when not human readable.
if !s.is_human_readable() {
serde::Serialize::serialize(v, s)
} else {
let mut seq = s.serialize_seq(Some(v.len()))?; let mut seq = s.serialize_seq(Some(v.len()))?;
for (key, value) in v.iter() { for (key, value) in v.iter() {
seq.serialize_element(&BorrowedPair(key, value))?; seq.serialize_element(&BorrowedPair(key, value))?;
} }
seq.end() seq.end()
} }
}
pub fn deserialize<'de, D, T>(d: D) pub fn deserialize<'de, D, T>(d: D)
-> Result<BTreeMap<T, Vec<u8>>, D::Error> where -> Result<BTreeMap<T, Vec<u8>>, D::Error> where
@ -184,8 +209,13 @@ pub mod btreemap_as_seq_byte_values {
} }
} }
// Don't do anything special when not human readable.
if !d.is_human_readable() {
serde::Deserialize::deserialize(d)
} else {
d.deserialize_seq(Visitor(PhantomData)) d.deserialize_seq(Visitor(PhantomData))
} }
}
} }
pub mod hex_bytes { pub mod hex_bytes {
@ -195,14 +225,19 @@ pub mod hex_bytes {
use hashes::hex::{FromHex, ToHex}; use hashes::hex::{FromHex, ToHex};
use serde; use serde;
pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error> pub fn serialize<T, S>(bytes: &T, s: S) -> Result<S::Ok, S::Error>
where T: AsRef<[u8]>, S: serde::Serializer where T: serde::Serialize + AsRef<[u8]>, S: serde::Serializer
{ {
serializer.serialize_str(&bytes.as_ref().to_hex()) // Don't do anything special when not human readable.
if !s.is_human_readable() {
serde::Serialize::serialize(bytes, s)
} else {
s.serialize_str(&bytes.as_ref().to_hex())
}
} }
pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error> pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error>
where D: serde::Deserializer<'de>, B: FromHex, where D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex,
{ {
struct Visitor<B>(::std::marker::PhantomData<B>); struct Visitor<B>(::std::marker::PhantomData<B>);
@ -230,6 +265,11 @@ pub mod hex_bytes {
} }
} }
// Don't do anything special when not human readable.
if !d.is_human_readable() {
serde::Deserialize::deserialize(d)
} else {
d.deserialize_str(Visitor(::std::marker::PhantomData)) d.deserialize_str(Visitor(::std::marker::PhantomData))
} }
}
} }