-
Notifications
You must be signed in to change notification settings - Fork 8
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
A different way to store custom types in JSON #17
Comments
Ooooh, I like this. I would probably still use It can also be made in a backwards-compatible way, because even if a user picks My main concern is... what would users prefer? I'm planning on doing a breaking release anyway, to fix all the nullable/optional/maybe fields mess, so I might have two different functions for this |
Giving both options would answer this question. Looking at the JSON with human eyes, I didn't like all the extra "tag" and "args", as these are strings that are not in my code base. I was even thinking to create an option where these two strings could be customized ("key" and "values", for example), but then I realized that I can even remove them, so I went for this last option. The extra thing I was thinking to clean up was about avoiding using a list if there is only one element in the payload of a custom type. For example, in the case of
instead of
but I didn't try to implement it |
I think a nice option would be to have:
So that people using the old way can migrate to either the compat way (if only Elm is reading the data) or old way (if other software is reading the data too) My only unhappiness with this approach is that if someone updates the library without reading the changelog it will break everything without any compiler errors. |
I decided I'm going to implement this in I think I'm not going to implement the |
{
"Red": [
42,
"Ciao"
]
} The above one definitely reads more cleanly than the current format: {
"args": [
42,
"Ciao"
],
"tag": "Red"
} I think it might be worth considering how it is accessed though. For example, I often do a switch statement based on the tag like so: switch (body.tag) {
case "EmptyBody": {
return null;
}
case "StringBody": {
return body.args[1];
}
case "BytesBody": {
return Buffer.from(body.args[1], "base64");
}
case "JsonBody": {
return JSON.stringify(body.args[0]);
}
} It seems a little more awkward to do a check for different tags with the proposed format. The way I can think to do it would be: switch (Object.keys(body)[0]) {
case "StringBody": {
return Object.values(body)[0];
} Is there a better way to access it? If not, the proposed format seems less clean to access in my opinion. |
Another alternative would be |
Yeah, both seem more concise but also more dependent on understanding the structure than the current format. If I'm looking at With |
Mh... guess This idea needs more baking, I need to think of a nice way to do it. It might end up being a |
More a comment than an issue.
I wanted to remove the "tag" and "args" from inside the JSON output.
So I changed a couple of functions and now instead of generating this JSON
(for
Red 42 Ciao
as per the example in the README)it generates this:
I wonder if this has been considered as an option and if there are any concerns
about generating this type of JSON.
These are the functions that I modified to achieve this (I commented out the removed code for comparison)
The text was updated successfully, but these errors were encountered: