spacejson 0.0.14

Derive JSON interfaces for types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use crate::{Json, String, Vec, format};
use anyhow::Result;

impl Json<String> for Vec<u8> {
    fn to_json(self) -> String {
        format!("0x{}", hex::encode(self))
    }

    fn from_json(json: String) -> Result<Self> {
        let bytes = hex::decode(json.trim_start_matches("0x"))
            .map_err(|e| anyhow::anyhow!("failed to decode json string: {e:?}"))?;
        Ok(bytes)
    }
}