-
-
Notifications
You must be signed in to change notification settings - Fork 777
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
Allow to define numbers for adjacently tagged enums #2479
Comments
This is already supported -- as you can see (run Tools->Expand macros), |
Yes, all is need is to annotate with:
Sorry, I was making some other mistakes and I thought this was not supported. |
Just a side note: you can use any names for {
"<selected value of 'tag'>": "<data tag>",
"<selected value of 'content'>": ...,
} and [
"<data tag>",
...
] In your case it will recognize {
"0": "<data tag>",
"1": ...
} |
My case is just like the first one, not than the second one. Not sure what you meant, but thanks for your answer |
I mean that both inputs are valid if you specify #[serde(tag = "0", content = "1")] In other words, specific values for |
Sorry, but I think I'm not understanding you correctly.
It works for both objects and arrays. But this las sentence of yours that I quoted seems to suggest that adjacently tagged enums work out of the box without the need for this annotation. As far as I know, there is no other way to opt-in into adjacently tagged enums. |
Of course not, annotation is needed. I just try to pay your attention, that |
Oh, I think I understand what you mean @Mingun , it is because of this #2480 right? |
The same approach is used to deserialize a pseudo-struct with "tag" and "content" fields, yes. You can think about that as if deserialization of that type is performed: #[derive(Deserialize)]
struct AdjacentlyEnumContainer<T> {
#[serde(rename = "<your tag>")]
tag: String,
#[serde(rename = "<your content>")]
content: T,
} except that the visitor is implemented manually and the type |
This could look similar to #745 , but it is not.
Some libraries in some other programming languages are serializing enums like this:
Basically a tuple. Exactly the same as an adjacently tagged enum but the key being 0 and the payload being 1.
I think implementing this in serde will be easier than having to implement it myself for every enum I have to deal with. To give you a little example, here is an implementation I came up with the great help of the rust community:
As you can see, it is a lot of boilerplate to have to repeat it so many times (I have dozens of this)
The text was updated successfully, but these errors were encountered: