This is a follow-up question from “Staying sane with
Maybe”.The only discussion I’ve found on here that touches on storing types as
jsonsuggests usingelm-codec, but I’m not sure how to integrate that withJson.DecodeorJson.Decode.Pipeline(there’s not many examples given) and it seems like overkill for simple types?What’s the simplest way to make this happen, without plugins?
I think I remember seeing Evan talk about saving custom types as SQL in the database (in the future), but what’s the best way to model these in json? I understand there’s no way to store types as json, and there’s limited json data types, but what about …
- A
Maybetype.- Would
Nothingbe represented simply as[],0,null, orfalse? - Is
Maybegenerally used for application state only?
- Would
- Take a simple example like
Colourbelow- What’s the best way to
encodeanddecodethese?
- What’s the best way to
- Perhaps you can give other examples you’ve discovered
I’ll caveat this by saying I’ve mostly decodeed and haven’t done any encodeing as of yet.
type Colour
= Light
| Dark
helper colour =
case colour of
Light -> "light"
Dark -> "dark"
Perhaps this is a CSS class that’s converted to "light" or "dark" strings with a case statement when a user clicks a button. How would you encode/decode that as json? Just store it in your record as a string and use that object to encode with? A simple helper function?
type Colour
= Cyan percentage
| Magenta percentage
| Yellow percentage
| Black percentage
Perhaps you have a colour picker that’s user generated. Would you just save this as a List percentage? Is there a better way to represent the custom type?
These might be overly simplistic or bad examples, I don’t know … but as far as I’m aware custom types allow you to be more specific within your app and add some type safety too. Does it matter how you store these?
How are you guys handling this when it comes to storing as json? How do you convert these from json->type and type->json? What problems have you found?