-
Notifications
You must be signed in to change notification settings - Fork 28
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
Encode None to undefined?! #26
Comments
Undefined property will be removed in JSON so I don't think this is the desired behavior |
How about not including the key in JSON if it's |
This is a tricky one. Just omitting the key might make sense. For now, you could do something like this: let encodeUndefined = (encoder, value) =>
switch value {
| None => [%bs.raw "undefined"]
| Some(v) => encoder(v)
};
let decodeUndefined = (decoder, json) =>
(json !== [%bs.raw "undefined"])
? Decco.optionFromJson(decoder, json)
: Ok(None);
[@decco] type mytype = {
u: [@decco.codec (encodeUndefined, decodeUndefined)] option(int),
}; |
Thank you, will try #26 (comment) in the near future.
and I will try to make a pull request?! |
Hey @strdr4605- I don't plan on working on this in the foreseeable future, and considering there's a pretty reasonable workaround, I'm wary about adding new functionality to handle this case unless this issue gets more traction. However, if you'd like to give it a go anyway, my comment in #6 might be helpful. |
BTW, you maybe figured this out already, but if you don't want to repeat |
You could also do something like [@decco]
type undefined('a) = [@decco.codec (encodeUndefined, decodeUndefined)] option('a); and use the |
I have 2 types:
and use them like:
Type
attachment
must have almost all fields optional. To not write everywherefield: None
... I want to use @bs.deriving abstract but as I understand I can't have dublicate@bs.deriving
.What should I do if need
slack_command_responseToJs
for Axios but also want optional fields?!Update:
To fix first problem I used:
Also
attachments: None,
created a json field"attachments": null
but I need to not be at all or"attachments": undefined
Is it possible to do it with
ppx_decco
?Maybe some new attribute like
[@decco.undefined]
only for optionsThe text was updated successfully, but these errors were encountered: