Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: serialize XorName to hex string #89

Merged
merged 1 commit into from Aug 4, 2022

Conversation

b-zee
Copy link
Contributor

@b-zee b-zee commented Aug 3, 2022

A problem arises when one wants to convert a structure like HashMap<XorName, usize> into a human-readable format like JSON. These formats do not allow complex types like XorName to be used as a key in a key-value structure, but instead only allow strings, for example.

This PR adds a feature that serializes XorName and Prefix into a hex string only if serializing into human-readable format.

I have added the following to the README.md:

Serialization

XorName and Prefix can be serialized into a human-readable hex string, instead of as a u8 array. To enable this, activate the serialize-hex feature. This also allows for these structures to be serialised when used as a key in a map like HashMap, because most formats only allow keys to be strings, instead of more complex types.

A struct like this:

#[derive(Serialize, Deserialize)]
struct MyStruct {
    prefix: Prefix,
    xor_name: XorName,
}

Will yield this JSON

{
  "prefix": "8a817b6d791f4b00000000000000000000000000000000000000000000000000/56",
  "xor_name": "8a817b6d791f4bae4117ac7ae15a88cd2c62fba0b040972ce885f1a47625dea1"
}

instead of

{
    "prefix": {
        "bit_count": 56,
        "name": [141,199,202,57,183,222,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    },
    "xor_name": [141,199,202,57,183,222,153,14,185,67,253,100,133,71,118,221,133,170,130,195,58,66,105,105,60,87,179,110,7,73,237,143]
}

@b-zee b-zee requested a review from a team as a code owner August 3, 2022 11:41
@b-zee b-zee force-pushed the feat-serialize-hex-string branch 4 times, most recently from 694fe39 to 19ba44b Compare August 4, 2022 14:22
Cargo.toml Outdated Show resolved Hide resolved
src/serialize.rs Outdated Show resolved Hide resolved
Serialize `XorName` to a string with hex representation, only if
serializing to a human readable format like with `serde_json` or `toml`.

This is guarded behind a feature as it adds a dependency to the `hex`
crate.
@b-zee b-zee force-pushed the feat-serialize-hex-string branch from 19ba44b to 9d54992 Compare August 4, 2022 15:02
Copy link
Contributor

@davidrusu davidrusu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@davidrusu
Copy link
Contributor

bors r+

@davidrusu davidrusu merged commit f6ed0b0 into master Aug 4, 2022
@davidrusu davidrusu deleted the feat-serialize-hex-string branch August 4, 2022 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants