How can I store enums in "adjacently-tagged" style? #4122
Unanswered
inklesspen
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Serde offers the adjacently-tagged representation for enums. As an example:
serde-json serializes these three instances as:
{"t":"Animal","c":{"friendly":true,"name":"Rover"}} {"t":"Vegetable"} {"t":"Mineral","c":{"hardness":10}}What I would like to do is to split off the tag and content: that is, I would like to store an enum like this as a pair of varchar and jsonb columns. (Let's say that the majority of the time, I only care about whether something is an animal, vegetable, or mineral, and not what the thing's friendliness or hardness may be. But sometimes I do need the full enum variant.)
I know that I could query this using an intermediate struct that has separate fields for the enum variant name and the "payload", and then I could use
serde_json::Valueto put those fields together and deserialize them into aThingKindinstance. But that is a lot of stuff to do by hand.(Note that I want to use the query macros, so the FromRow trait is not going to be helpful here at all.)
Beta Was this translation helpful? Give feedback.
All reactions