Skip to content

Conversation

@phiotr
Copy link

@phiotr phiotr commented Aug 22, 2023

The undefined atom in Erlang is used as the default value for unset record fields, missing process dictionary entries, or lack of data in most of all the built-in data structures. It is exactly the same concept as null for other modern programming languages (C#, Java, JavaScript), nill (Go, Ruby, Swift), or None (Python, Scala, Rust), etc.

Whenever encoding such Erlang term containing undefines to the JSON with Jiffy (for instance as a response for some API query), the coder has to write a custom converter for it. It can be something such simple as:

undefined_to_null(undefined) -> null;
undefined_to_null(V) -> V.

or more complex whenever more nested data structures are passed into it.

To solve this inconvenience and let us all keep Erlang's world and its conventions separate from formatting data to the JSON, I propose introducing a custom use_undefined encoding option. Very similar to use_nil, but converts undefined atoms to JSON's nulls in place instead of nils.

@phiotr
Copy link
Author

phiotr commented Aug 22, 2023

$ rebar3 shell
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling jiffy
Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]

Eshell V13.2  (abort with ^G)
1> jiffy:encode(#{a => undefined}).                                                      
<<"{\"a\":\"undefined\"}">>
2> jiffy:encode(#{a => undefined}, [use_undefined]).
<<"{\"a\":null}">>
3> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant