Skip to content
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

Struct variant don't deserialize from sequence with #[serde(untagged)] #2473

Open
Baptistemontan opened this issue Jun 10, 2023 · 2 comments

Comments

@Baptistemontan
Copy link
Contributor

Baptistemontan commented Jun 10, 2023

I'm currently messing around with serde creating my own binary format, and I encode the type of the data, but I don't encode fields names for struct like type, so a struct is treated as a sequence. encoding the type allows me to implement Deserializer::deserialize_any as I can peek the tag and call the appropriate function.

It worked well until I tried with an enum with the #[serde(untagged)] attribute. I kind of lose my sanity trying to understand what was wrong, until I checked the expanded #[derive(Deserialize)] macro. Struct variant Visitor only implement Visitor::visit_map.

Is there a reason why struct variant don't implement Visitor::visit_seq like normal struct does when the #[serde(untagged)] is present ?

@Mingun
Copy link
Contributor

Mingun commented Jun 10, 2023

My PR #2465 fixes exactly that problem

@Baptistemontan
Copy link
Contributor Author

Thanks! I tried searching any related Issue but did not search through the PR. I kind of fixed the problem by looking throught the auto implemntation and found that the Field enum deserialized from the key of the map can be deserialized with a u64 representing the field index, so I now deserialize struct like types with a custom MapAccess that gives a u64 as a key, and just increment it. It feels a bit like a redneck solutions but everything seams to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants