Skip to content
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

Feature request: default handling of undefined values in the standard lib json module #8905

Open
erlend-axelsson opened this issue Oct 5, 2024 · 1 comment
Assignees
Labels
enhancement team:PS Assigned to OTP team PS

Comments

@erlend-axelsson
Copy link

Is your feature request related to a problem? Please describe.
I have a use case where I need to send a json object with an explicit undefined value, and I discovered that the default behavior of json:encode/1 and json:decode is to encode the atom 'undefined' as a string, and throw an exception while decoding.
While it isn't a massive issue I believe it would be a quality of life improvement to treat 'undefined' as a special case like how 'null' is.

Describe the solution you'd like
Add a special case where 'undefined' gets (en/de)coded as undefined like how 'null' gets (en/de)coded as null in the default json:encode and decode functions.

Describe alternatives you've considered
Alternatively providing an encoder and decoder like json:encode_key_value_list_checked/2 which handles 'undefined' as undefined would also work if changing the defaults risks introducing regressions in existing code.

@erlend-axelsson
Copy link
Author

I read up on the RFC 8259 and ECMA 404 standards, so it seems I was wrong in believing undefined to be a part of the JSON standard.

I still think there is some value in at least allowing the option of a "lax" encoding/decoding since it's not terribly uncommon for JSON "in the wild" to contain undefined values. It was relatively easy to get json:encode/2 to encode undefined, however it seems that the decoding part is hard coded to reject undefined as decode/1, decode/3 and decode_start/3 all immediately call json:value/6 which then errors on the function overload at line 797.

ref encoding undefined:

custom_encode(undefined, _Encode) ->
    <<"undefined">>;
custom_encode(Value, Encode) ->
    json:encode_value(Value, Encode).
custom_encode(Value) -> json:encode(Value, fun custom_encode/2).

@IngelaAndin IngelaAndin added the team:PS Assigned to OTP team PS label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:PS Assigned to OTP team PS
Projects
None yet
Development

No branches or pull requests

4 participants