Skip to content

Control integer representations #144

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

Closed
kvark opened this issue Feb 11, 2019 · 9 comments
Closed

Control integer representations #144

kvark opened this issue Feb 11, 2019 · 9 comments
Labels
feature A new feature for the syntax or library. help wanted stale

Comments

@kvark
Copy link
Collaborator

kvark commented Feb 11, 2019

It would be good to be able to say that a particular integer needs to be serialized in hex, for example. On first approximation, this doesn't appear to be possible through serde attributes, since serde doesn't care about how integers are parsed.

@kvark kvark added help wanted feature A new feature for the syntax or library. labels Feb 11, 2019
@torkleyy
Copy link
Contributor

Can you control the definition of the struct? I'd just use a custom path:

pub struct MyStruct {
    #[serde(with = "hex")]
    flags: usize,
}

mod hex {
    // pseudo code
    fn serialize<S>(flags: &usize, ser: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let flags = flags.to_hex_string();
        ser.serialize_str(&flags)
    }

    // fn deserialize
}

@torkleyy
Copy link
Contributor

@kvark Does this work for your use case?

@kvark
Copy link
Collaborator Author

kvark commented Feb 14, 2019

Trouble is that in WebRender the same structure may need to be serialized with both bincode and RON. And I only want RON to be showing the hex representation, of course.

@torkleyy
Copy link
Contributor

torkleyy commented Apr 5, 2019

Okay, I understand. So you want to set the representation of all integers globally?

@kvark
Copy link
Collaborator Author

kvark commented Apr 22, 2019

@torkleyy nope, not all integers, just a particular field of a particular struct.

@torkleyy
Copy link
Contributor

Okay. So given that werde doesn’t support that, the only option I see would be to configure overrides for the serializer. Something like

serializer.for_field(“flags”, Repr::Hex);

which... seems very special case-y.

@kvark
Copy link
Collaborator Author

kvark commented Apr 23, 2019

That's an interesting approach, and while inconvenient, it's certainly at least an option for us to proceed. I'd say let's expose this in some way.

@CAD97
Copy link
Contributor

CAD97 commented May 2, 2020

The serialized structure can also look at serializer.is_human_readable() and serialize differently depending on that.

@github-actions
Copy link

Issue has had no activity in the last 180 days and is going to be closed in 7 days if no further activity occurs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature for the syntax or library. help wanted stale
Projects
None yet
Development

No branches or pull requests

3 participants