Skip to content

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

Merged
merged 3 commits into from
Dec 3, 2021

Conversation

fwcd
Copy link
Contributor

@fwcd fwcd commented Aug 12, 2021

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 define

enum X {
    #[serde(rename = "$primitive=A")]
    A
}

struct Y {
    x: X
}

X::A will now serialize to A instead of <A/> and Y { x: X::A } will now serialize to <Y x="A"/> instead of <Y><A/></Y>.

For example, if we define `enum X { A, B }`, `X::A` will now be
serialized to `A` instead of `<A/>`.
@bachp
Copy link

bachp commented Aug 12, 2021

@fwcd Could this be somehow controlled via an #[] attribute on an enum or enum variant level?

@fwcd
Copy link
Contributor Author

fwcd commented Aug 16, 2021

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?

@tafia
Copy link
Owner

tafia commented Aug 16, 2021

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.

@fwcd fwcd changed the title Serialize unit variants as string (suggestion) Add $primitive for serializing unit variants as string Aug 16, 2021
@fwcd
Copy link
Contributor Author

fwcd commented Aug 16, 2021

I have restored the previous behavior and added a $primitive= prefix that can be placed on unit variants to optionally serialize them as strings. A new paragraph in the README documents this behavior.

@fwcd fwcd marked this pull request as ready for review August 16, 2021 02:05
@djc
Copy link

djc commented Dec 2, 2021

@tafia I would really like to be able to use this. Is there some chance of getting this merged?

@tafia tafia merged commit d872771 into tafia:master Dec 3, 2021
@tafia
Copy link
Owner

tafia commented Dec 3, 2021

Thanks, I'll publish a new version this weekend

@djc
Copy link

djc commented Dec 3, 2021

@tafia awesome, thanks!

@djc
Copy link

djc commented Jan 6, 2022

@tafia happy new year! Gentle ping on getting this out to crates.io?

@fwcd fwcd deleted the unit-variants-as-string branch January 29, 2022 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is this result expected when serializing in the enum type?
4 participants