spacejson/bytes.rs
1use crate::{Json, String, Vec, format};
2use anyhow::Result;
3
4impl Json<String> for Vec<u8> {
5 fn to_json(self) -> String {
6 format!("0x{}", hex::encode(self))
7 }
8
9 fn from_json(json: String) -> Result<Self> {
10 let bytes = hex::decode(json.trim_start_matches("0x"))
11 .map_err(|e| anyhow::anyhow!("failed to decode json string: {e:?}"))?;
12 Ok(bytes)
13 }
14}