-
Notifications
You must be signed in to change notification settings - Fork 72
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: force option to serialize as variants (with = option_as_enum) #365
Comments
This seems like a useful addition. I think I would prefer this to exists as a Would there be a benefit to also offer different enum representations, i.e., internally and adjacently tagged? Externally tagged is the more efficient one and does also work with non self-describing formats. |
I don't think internal/adjacent tagging would see any use; it's not really useful to have such structure for a unit variant, imho. But maybe I'm wrong; if the implementation effort is minimal enough, it might be worth providing just for completeness. That said, I do think providing the different ways to map
|
I think the listed variants work ok and could be added. /// unwrap_or_skip
#[serde(with = "unwrap_or_skip", skip_serializing_if = "Option::is_none", default)]
foobar: Option<i32>,
/// unwrap_or_unit
#[serde(with = "unwrap_or_unit")]
foobar: Option<i32>, If there is no immediate desire to add internal and adjacent variants, I would not implement them until they are requested. Do you want to start implementing some of these variants and send a PR? I can assist too. As mentioned, I would prefer them to be structs implementing |
Add new conversions UntaggedOption and ExplicitOption. UntaggedOption serializes the enum but untagged, sidestepping the default Option rules. ExplicitOption serializes the Option as any other enum. Fixes #365
This is one way to e.g. successfully roundtrip things like
Option<Option<u32>>
into formats that haveunwrap_or_null
behavior by default (as opposed to e.g.double_option
orunwrap_or_skip
).Example implementation:
Output, showing behavior (manually reformatted):
The text was updated successfully, but these errors were encountered: