Git Product home page Git Product logo

serde-hex's Introduction

serde-hex Build Status

Rust crate for easy and versatile serialization/deserialization of hexadecimal values.

Overview

The serde-hex crate is built around the SerHex trait, which makes it easy to specify custom hexadecimal serialization/deserialization with serde. Once implemented, the SerHex trait is configured via its generic type parameter (e.g.; SerHex<Compact> for hexadecimal values with no leading zeroes, or SerHex<StrictPfx> for leading zeroes and the 0x prefix). Thanks to Rust's very magical compiler, only the components of the serialization/deserialization functionality that your configuration actually uses get compiled in. The most common way to use SerHex is when deriving the Serialize and Deserialize trait for your types. Here is a simple example using serde_derive and serde_json:

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);
}

SerHex is automatically implemented for all unsigned integer types, and all Strict variants are implemented for arrays of [T;1] through [T;64] (where T: SerHex of course). We skip default impls for arrays of length zero, as well as signed integers & floats, since there isn't any particularly intuitive way to represent these values.

This crate provides a number of helpful macros for implementing SerHex on common patterns. If none of the macros suit your needs, a number of utility functions are also provided to make implementing custom variations as painless as possible.

Note

Check out the widely used hex crate if you are just looking for generic hexadecimal conversion traits. This crate is intended fairly specifically for customizeable serde interop, and the generic traits of the hex crate should be preferred if that isn't what you are looking for.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

serde-hex's People

Contributors

gluk64 avatar fspmarshall avatar

Watchers

Sumeet Agarwal avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.