Skip to content

Commit abe2ce8

Browse files
committed
binary types: compact serialization
1 parent 6d4aad3 commit abe2ce8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

packages/std/src/binary.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Serialize for Binary {
211211
if serializer.is_human_readable() {
212212
serializer.serialize_str(&self.to_base64())
213213
} else {
214-
panic!("Binary is only intended to be used with JSON serialization for now. If you are hitting this panic please open an issue at https://github.com/CosmWasm/cosmwasm describing your use case.")
214+
serializer.serialize_bytes(&self.0)
215215
}
216216
}
217217
}
@@ -225,7 +225,7 @@ impl<'de> Deserialize<'de> for Binary {
225225
if deserializer.is_human_readable() {
226226
deserializer.deserialize_str(Base64Visitor)
227227
} else {
228-
panic!("Binary is only intended to be used with JSON serialization for now. If you are hitting this panic please open an issue at https://github.com/CosmWasm/cosmwasm describing your use case.")
228+
deserializer.deserialize_bytes(BytesVisitor)
229229
}
230230
}
231231
}
@@ -250,6 +250,23 @@ impl<'de> de::Visitor<'de> for Base64Visitor {
250250
}
251251
}
252252

253+
struct BytesVisitor;
254+
255+
impl<'de> de::Visitor<'de> for BytesVisitor {
256+
type Value = Binary;
257+
258+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
259+
formatter.write_str("byte array")
260+
}
261+
262+
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
263+
where
264+
E: de::Error,
265+
{
266+
Ok(Binary(v.to_vec()))
267+
}
268+
}
269+
253270
#[cfg(test)]
254271
mod tests {
255272
use super::*;

packages/std/src/hex_binary.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl Serialize for HexBinary {
214214
if serializer.is_human_readable() {
215215
serializer.serialize_str(&self.to_hex())
216216
} else {
217-
panic!("HexBinary is only intended to be used with JSON serialization for now. If you are hitting this panic please open an issue at https://github.com/CosmWasm/cosmwasm describing your use case.")
217+
serializer.serialize_bytes(&self.0)
218218
}
219219
}
220220
}
@@ -228,7 +228,7 @@ impl<'de> Deserialize<'de> for HexBinary {
228228
if deserializer.is_human_readable() {
229229
deserializer.deserialize_str(HexVisitor)
230230
} else {
231-
panic!("HexBinary is only intended to be used with JSON serialization for now. If you are hitting this panic please open an issue at https://github.com/CosmWasm/cosmwasm describing your use case.")
231+
deserializer.deserialize_bytes(BytesVisitor)
232232
}
233233
}
234234
}
@@ -253,6 +253,23 @@ impl<'de> de::Visitor<'de> for HexVisitor {
253253
}
254254
}
255255

256+
struct BytesVisitor;
257+
258+
impl<'de> de::Visitor<'de> for BytesVisitor {
259+
type Value = HexBinary;
260+
261+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
262+
formatter.write_str("byte array")
263+
}
264+
265+
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
266+
where
267+
E: de::Error,
268+
{
269+
Ok(HexBinary(v.to_vec()))
270+
}
271+
}
272+
256273
#[cfg(test)]
257274
mod tests {
258275
use super::*;

0 commit comments

Comments
 (0)