-
Notifications
You must be signed in to change notification settings - Fork 126
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
Add support for serializing hex strings without quotes #554
base: master
Are you sure you want to change the base?
Conversation
src/ser/mod.rs
Outdated
fn hex_as_raw(&self) -> bool { | ||
self.pretty | ||
.as_ref() | ||
.map_or(true, |(ref config, _)| config.hex_as_raw) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implies that the hex_as_raw
option is enabled when no pretty config is given. Same with the default in the PrettyConfig
above. Usually pretty config options are opt-in, so I'd like to hear some justification for being opt-out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good idea, I changed it so now it defaults to false
I didn't know we supported parsing integers from hex strings ... can you add a test for that? |
I'm not sure if I'm in favor of this proposed change so far. If the goal is to be able to serialize all integers in binary, octal, or hex notation, I'd happily accept a proposal for that. If you only want to serialize specific integers as hex, there is a (hacky) workaround that should work already. You could define a serde |
@aljen Thank you for submitting the PR! I've left some thoughts above :) |
You're right, it was late, I was thinking about parsing hex value as an integer, not a string - fixed in description, sorry for confusion =) Sorry for long time to reply, I was on holiday vacations :) |
No worries, happy New Year! Do you want this PR to go in the suggested direction of serialising all integers as hex / octal / binary? It would require a small rewrite in this case. If it's just about serialising a few values in hex, have you tried out the hack using raw values? |
Problem
Currently, RON supports parsing hex numbers in both quoted and unquoted form:
However, when serializing such values, they are always wrapped in quotes:
This creates an inconsistency between parsing and serialization capabilities, and makes the serialized output less elegant, especially in configuration files where hex numbers are commonly used (e.g., for colors, flags, or memory addresses).
Solution
This PR adds a new configuration option
hex_as_raw
(default:true
) toPrettyConfig
that allows hex strings to be serialized without quotes. When enabled:Number is serialised clean and consistent with parsing capabilities.
The implementation includes:
hex_as_raw
configuration optionBenefits
0x[0-9a-fA-F]+
) are serialized without quotesThis enhancement makes RON's handling of hex numbers more elegant and consistent :)
CHANGELOG.md