ron-rs/ron

Using struct names as enum variants when [de]serializing

Closed this issue · 2 comments

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.

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

  • externally tagged: the canonical representation, for a Rust enum variant Enum::Variant { a: 42 }, you write Variant(a: 42) in RON
  • internally tagged: a JSON-like representation: { “tag”: “Variant”, “a”: 42 }
  • adjacently tagged: a more explicit tag vs content encoding: (tag: Variant, content: (a: 42))
  • untagged: you can also omit the tag and rely on serde identifying the correct variant based on the data, though this results in worse errors if anything goes wrong: (a: 42)

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 :)

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