Add $primitive for serializing unit variants as string#304
Add $primitive for serializing unit variants as string#304tafia merged 3 commits intotafia:masterfrom
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::Awill now serialize toAinstead of<A/>andY { x: X::A }will now serialize to<Y x="A"/>instead of<Y><A/></Y>.