0

I want to get the pretty print in Rust for [u8; 32], with serde_json, but can't make it work. I know if the following struct is a (Vec<u8>), that works, as this playground code. But I have to keep the ([u8; 32]) struct because it's everywhere in an existing project. Could somebody help to make this work for array instead of Vec ?

#[derive(Serialize, Deserialize)]
struct OneFactor(
    #[serde(
        serialize_with = "buffer_to_hex",
        deserialize_with = "hex_to_buffer"
    )]
    [u8; 32],
);

/// Serializes `buffer` to a lowercase hex string.
pub fn buffer_to_hex<T, S>(buffer: &T, serializer: S) -> Result<S::Ok, S::Error>
where
    T: AsRef<[u8]>,
    S: Serializer,
{
    serializer.serialize_str(&buffer.as_ref().to_hex())
}

/// Deserializes a lowercase hex string to a `Vec<u8>`.
pub fn hex_to_buffer<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
    D: Deserializer<'de>,
{
    use serde::de::Error;
    String::deserialize(deserializer)
        .and_then(|string| Vec::from_hex(&string).map_err(|err| Error::custom(err.to_string())))
}

fn print_an_factor() -> Result<(), Error> {
    let factor = OneFactor([
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
        26, 27, 28, 29, 30, 31, 32,
    ]);

    // Serialize it to a JSON string.
    let j = serde_json::to_string_pretty(&factor)?;

    // Print, write to a file, or send to an HTTP server.
    println!("{}", j);

    Ok(())
}

fn main() {
    print_an_factor().unwrap();
}

And I put the full code into the Rust Playground for your easier check.

[Update]

I work out a version which works for to_hex() direction, the code is here. But the reverse direction from_hex() still need help ~

gary
  • 1,569
  • 3
  • 20
  • 30
  • Do you use Serde only for pretty print? – Boiethios Jul 24 '18 at 11:30
  • No, it's widely used. Here I just want to demo the problem I faced:) – gary Jul 24 '18 at 11:43
  • 2
    You're trying to deserialize to an array, but you're actually returning a `Vec`. – Boiethios Jul 24 '18 at 11:50
  • Could you please help to fix? @Boiethios – gary Jul 24 '18 at 12:45
  • If this is actuelly the problem you are facing, this is a duplicate of https://stackoverflow.com/questions/25428920/how-to-get-a-slice-as-an-array-in-rust – Boiethios Jul 24 '18 at 12:52
  • No, not this problem. I want pretty print for ([u8; 32]). – gary Jul 24 '18 at 12:57
  • If you want pretty print for `[u8; 32]`, use `println!("{:?}", my_array)` – Boiethios Jul 24 '18 at 13:05
  • :) that's not a solution. this ([u8; 32]) could be a field of many other structures, I need use `serde_jason` for all of them. And [u8; 32] should be printed as hex. – gary Jul 24 '18 at 13:10
  • I work out a version which works for `to_hex()` direction, the code is [here](http://play.integer32.com/?gist=05fef4638189f1bf860920bd623149da&version=stable&mode=debug&edition=2015). But the reverse direction `from_hex()` still need help ~ – gary Jul 24 '18 at 14:11

0 Answers0