-
Notifications
You must be signed in to change notification settings - Fork 252
Add $primitive for serializing unit variants as string #304
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
Conversation
For example, if we define `enum X { A, B }`, `X::A` will now be serialized to `A` instead of `<A/>`.
@fwcd Could this be somehow controlled via an |
Perhaps, I am not that familiar with serde's macro system though. We might be able to use a special dollar-named prefix, like already used for unflattening structs (see here). I have a feeling that most use cases where unit variants are involved are concerned with serializing 'simple' enums (i.e. without associated values) that are stored as a field, and making unit variants-as-strings (i.e. this PR) the default behavior would match the intuitive serialization there. Any thoughts? |
I think the default behavior isn't necessarily unintuitive. Sadly this discussion is probably to late because I am afraid to break quite a lot of code. I agree with a new $ attribute to make it work. This is far from perfect but xml really isn't that great with serde and this is the best workaround I can see. Eventually we'll have to properly document all these $ attributes. |
I have restored the previous behavior and added a |
@tafia I would really like to be able to use this. Is there some chance of getting this merged? |
Thanks, I'll publish a new version this weekend |
@tafia awesome, thanks! |
@tafia happy new year! Gentle ping on getting this out to crates.io? |
Fixes #283
This PR updates the serializer for unit variants to serialize them as string primitives instead of self-closing tags if prefixed with
$primitive
. For example, if we defineX::A
will now serialize toA
instead of<A/>
andY { x: X::A }
will now serialize to<Y x="A"/>
instead of<Y><A/></Y>
.