fadedbee
fadedbee

Reputation: 44797

Make serde only produce hex strings for human-readable serialiser?

I'm currently using serde-hex.

use serde_hex::{SerHex,StrictPfx,CompactPfx};

#[derive(Debug,PartialEq,Eq,Serialize,Deserialize)]
struct Foo {
    #[serde(with = "SerHex::<StrictPfx>")]
    bar: [u8;4],
    #[serde(with = "SerHex::<CompactPfx>")]
    bin: u64
}

fn it_works() {
    let foo = Foo { bar: [0,1,2,3], bin: 16 };
    let ser = serde_json::to_string(&foo).unwrap();
    let exp = r#"{"bar":"0x00010203","bin":"0x10"}"#;
    assert_eq!(ser,exp);

    // this fails
    let binser = bincode::serialize(&foo).unwrap();
    let binexp: [u8; 12] = [0, 1, 2, 3, 16, 0, 0, 0, 0, 0, 0, 0];
    assert_eq!(binser,binexp);
}

fails with:

thread 'wire::era::tests::it_works' panicked at 'assertion failed: `(left == right)`
  left: `[10, 0, 0, 0, 0, 0, 0, 0, 48, 120, 48, 48, 48, 49, 48, 50, 48, 51, 4, 0, 0, 0, 0, 0, 0, 0, 48, 120, 49, 48]`,
 right: `[0, 1, 2, 3, 16, 0, 0, 0, 0, 0, 0, 0]`', src/test.rs:20:9

because it has expanded values to hex strings for bincode.

I have many structs which I need to serialise with both serde_json and bincode. serde_hex does exactly what I need for JSON serialisation. When using bincode serde-hex still transforms arrays into hex strings, which is not wanted.

I notice that secp256k1 uses d.is_human_readable().

How can I make serde_hex apply only to serde_json and be ignored for bincode?

Upvotes: 2

Views: 1602

Answers (1)

jonasbb
jonasbb

Reputation: 2603

The implementation of a function usable with serde's with-attribute is mostly boilerplate and looks like this. This only differentiates between human-readable and other formats. If you need more fine-grained control, you could branch on a thread-local variable instead.

fn serialize_hex<S>(v: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    if serializer.is_human_readable() {
        serde_hex::SerHex::<serde_hex::StrictPfx>::serialize(v, serializer)
    } else {
        v.serialize(serializer)
    }
}

// use like
// #[serde(serialize_with = "serialize_hex")]
// bin: u64

The snippet could be improved by turning the u64 into a generic.

Upvotes: 1

Related Questions