#byte-buffer #hex-string #hex #byte #serde

no-std hex-buffer-serde

Helper for serializing byte buffers as hex strings in serde

7 unstable releases

0.4.0 Nov 1, 2022
0.3.0 May 3, 2021
0.2.2 Dec 5, 2020
0.2.1 Mar 15, 2020
0.1.1 Nov 30, 2018

#1593 in Encoding

Download history 1509/week @ 2024-03-14 1280/week @ 2024-03-21 1142/week @ 2024-03-28 1402/week @ 2024-04-04 1214/week @ 2024-04-11 1263/week @ 2024-04-18 1544/week @ 2024-04-25 1449/week @ 2024-05-02 1758/week @ 2024-05-09 1302/week @ 2024-05-16 1374/week @ 2024-05-23 1209/week @ 2024-05-30 1492/week @ 2024-06-06 1302/week @ 2024-06-13 1367/week @ 2024-06-20 1442/week @ 2024-06-27

5,843 downloads per month
Used in 35 crates (9 directly)

Apache-2.0

32KB
425 lines

Hex encoding helper for serde

Build Status License: Apache-2.0 rust 1.57+ required

Documentation: Docs.rs crate docs (master)

hex-buffer-serde is a helper crate allowing to serialize types, which logically correspond to a byte buffer, in hex encoding within serde.

Usage

Add this to your Crate.toml:

[dependencies]
hex-buffer-serde = "0.4.0"

Basic usage:

use hex_buffer_serde::{Hex as _, HexForm};
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct Example {
    // will be serialized as hex string in human-readable formats
    // (e.g., JSON or TOML), and as a byte array in binary formats
    // (e.g., bincode).
    #[serde(with = "HexForm")]
    buffer: [u8; 32],
    // other fields...
}

See crate docs for more examples of usage.

Alternatives

hex-serde provides similar functionality and is a viable alternative if you have the control over the type that needs hex-encoding. This crate differs from hex-serde in the following ways:

  • You don't need control over the (de)serialized type; it does not need to implement any specific "useful" traits (such as AsRef<[u8]>).
  • Hex encoding is used only with human-readable (de)serializers (e.g., JSON or TOML). If the (de)serializer is not human-readable (e.g., bincode), the type is serialized as a byte array.

serde-with also provides hex encoding / decoding functionality with similar constraints to hex-serde.

License

hex-buffer-serde is licensed under the Apache License (Version 2.0). See LICENSE for details.

Dependencies

~130–365KB