How to serialize an integer as hexadecimal or binary? #781
Answered
by
epage
pastelmind
asked this question in
Q&A
-
I want to serialize certain integer fields of a struct as hex or binary format. The following struct... #[derive(Serialize, Deserialize)]
struct Foo {
dec: u32,
hex: u32,
bin: u32,
}
let foo = Foo { dec: 12, hex: 34, bin: 56 }; ...would be serialized to: dec = 12
hex = 0x22
bin = 0b111000 Is this possible? |
Beta Was this translation helpful? Give feedback.
Answered by
epage
Sep 4, 2024
Replies: 1 comment 3 replies
-
FYI this is more of serde question than toml. Check out the field attributes documented at serde.rs |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, I missed that you were referring to how TOML renders the number.
serde does not natively pass through custom formatting. Our options
serde_spanned
. These are a pain to work withtoml_edit::Document
and apply your own visitor.I currently suggest the last one when people want to do custom formatting.
That leaves us with where you left off, a way to control what repr gets generated for numbers. The only way to do it today is for you to generate a valid TOML representation, parse it, and put it in the document. This is not…