ron-rs / ron

Rusty Object Notation
https://docs.rs/ron
Apache License 2.0
3.31k stars 122 forks source link

Using struct names as enum variants when [de]serializing #521

Closed JohnTheCoolingFan closed 9 months ago

JohnTheCoolingFan commented 9 months ago

I have an idea to use struct names as tags of enums when deserializing and serializing. I've noticed there is enum grammar in the docs: docs/grammar.md but I failed to understand how it can be used.

I've found #519, which probably has the same blockers as this feature would have. I don't know how possible this is to implement and whether it has a lot of usability for others. I plan to use this to load a list of objects from a file and using enum variants would probably be the best way to detect which concrete type to deserialize into.

juntyr commented 9 months ago

Thank you for reaching out! RON is based on serde and thus supports different representations for enums:

You can learn more about how to annotate your Rust data types to use these representations here: https://serde.rs/enum-representations.html

I hope this has somewhat helped you - please feel free to ask again if anything is still unclear :)

JohnTheCoolingFan commented 9 months ago

Ah, so the default behavior with externally tagged enums is exactly what I'm asking about? Sweet!