<div dir="ltr"><div>I could never understand where this conversion JSON null <-> Erlang undefined was coming from.</div><div><br></div><div>Of course we generally don't use null atom in the code, but I always considered "undefined" value in Erlang meaning "absent" value. For example when a record is created and some field is not populated explicitly, it is set undefined.</div><div></div><div>This logic is very similar to Javascript objects with missing fields. Consider Javascript parsing a JSON object and fetching fields from it. If a key was not present in the JSON, undefined would be returned, which is different from null.</div><div><br></div><div> That example always made me think, that undefined values on Erlang side should be seen on Javascript side as undefined values too. And to make sure this works that way, the attributes holding atom undefined had to be not present in the resulting JSON.</div><div><br></div><div>There is another example from Javascript world:</div><div>> JSON.stringify({a: 1, b: undefined, c: null})<br>< "{"a":1,"c":null}"<br></div><div><br></div><div>So I'd say that to keep semantics synchronised, the conversions should be JSON null <-> Erlang null, and Erlang undefined values in objects must be simply removed from the resulting JSON object.<br></div><div><br></div><div>Anyway, what I'd actually greet as a serious improvement in handling of JSON in our small Erlang world would be an introduction of "SAX JSON" parser (similar to what jsx does) as part of the standard Erlang distribution and implemented in C. That way we would have parsing separated from construction of the representation, and anyone would be able to use whatever representation they like.<br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Kind regards,<div>Dmitry Belyaev</div></div></div></div>
<br><div class="gmail_quote">On Wed, Jun 27, 2018 at 6:56 PM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You are conflating Javascript and JSON. JSON was originally defined as a subset of Javascript, but it has very little to do with it otherwise.<br>
<br>
There is no 'undefined' JSON type. There is only 'null'. The Javascript 'undefined' is irrelevant because it doesn't translate into JSON to begin with.<br>
<br>
That being said, both the Javascript 'undefined' and 'null' translate to the JSON 'null'. They are not treated as distinct. The JSON 'null' has roughly the same semantics as the Erlang 'undefined' so the mapping makes sense and makes using JSON libraries easier as long as the JSON library uses maps and not proplists.<br>
<br>
Cheers,<span class=""><br>
<br>
On 06/27/2018 10:43 AM, Whealy, Chris wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
Allow me to elaborate on your point Michael (also without getting philosophical).<br>
<br>
In JavaScript null and undefined are identifiably distinct datatypes that serve specific purposes:<br>
<br></span>
  * *null* - This variable specifically has no value<br>
  * *undefined* - The value of this variable is indeterminate (I.E.<span class=""><br>
    identifiably different from null)<br>
<br>
<br>
Although the atom null has no special meaning in Erlang, it does when mapped to JavaScript null; therefore to maintain accuracy, Erlang devs who also work in JavaScript should understand and preserve this semantic difference.  Likewise with Erlang undefined mapping to JavaScript undefined.<br>
<br>
Therefore, I submit that this semantic difference should be persevered when mapping from Erlang to JavaScript, otherwise data loss will occur, particularly when mapping from JavaScript back to Erlang.<br>
<br>
Erlang    -> JavaScript<br>
null      -> null<br>
undefined -> undefined<br>
<br>
JavaScript -> Erlang<br>
null       -> null<br>
undefined  -> undefined<br>
<br>
This part of the mapping table at least should be bijective.<br>
<br></span>
*Chris Whealy*<span class=""><br>
<br>
SAP Cloud Platform | Strategy & Product Management | Team<br>
<br></span>
*SAP UK Ltd,* Clockhouse Place, Bedfont Rd, Feltham, Middx, TW14 8HA, England<br>
<br>
M +44 (0)7808 575377<br>
<br>
Find out more on the Strategy & Product Management***Wiki page* <<a href="https://wiki.wdf.sap.corp/wiki/pages/viewpage.action?pageId=1865737441" rel="noreferrer" target="_blank">https://wiki.wdf.sap.corp/wik<wbr>i/pages/viewpage.action?pageId<wbr>=1865737441</a>> (SAP internal)<br>
Follow our latest activities in SAP CP User Community *Jam Group <<a href="https://jam4.sapjam.com/groups/about_page/eopqUq5S182gY7JFrbdwis" rel="noreferrer" target="_blank">https://jam4.sapjam.com/group<wbr>s/about_page/eopqUq5S182gY7JFr<wbr>bdwis</a>>*<span class=""><br>
<br>
Please consider the impact on the environment before printing this e-mail.<br>
<br>
Twitter: @LogaRhythm<br></span>
/"The voice of ignorance speaks loud and long,/<br>
/  but the words of the wise are quiet and few"/<br>
/                                                 Ancient Proverb/<br>
<br>
<br>
<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
On 27 Jun 2018, at 08:42, Michael Nisi <<a href="mailto:michael.nisi@gmail.com" target="_blank">michael.nisi@gmail.com</a> <mailto:<a href="mailto:michael.nisi@gmail.com" target="_blank">michael.nisi@gmail.com</a><wbr>>> wrote:<br>
<br>
Here’s how v8::JSON, the JSON parser in Node’s JavaScript engine, does it:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.stringify({})<br>
</blockquote>
'{}'<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.stringify({ name: null })<br>
</blockquote>
'{"name":null}'<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.stringify({ name: undefined })<br>
</blockquote>
'{}'<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.stringify({ name: 'Lionel' })<br>
</blockquote>
'{"name":"Lionel"}’<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.parse('{}').name<br>
</blockquote>
undefined<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
JSON.parse('{ "name": null }').name<br>
</blockquote>
null<br>
<br>
JavaScript differentiates between null and undefined, without wanting to get philosophical here.<br>
<br>
Michael<br>
<br>
<br>
</span><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 27. Jun 2018, at 09:21, <a href="mailto:zxq9@zxq9.com" target="_blank">zxq9@zxq9.com</a> <mailto:<a href="mailto:zxq9@zxq9.com" target="_blank">zxq9@zxq9.com</a>> wrote:<br>
<br>
Erlang -> JSON<br>
- true      -> true<br>
- false     -> false<br>
- undefined -> null<br>
- Atom      -> String<br>
<br>
JSON -> Erlang<br>
- true  -> true<br>
- false -> false<br>
- null  -> undefined<br>
<br>
</blockquote>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
</span><a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlan<wbr>g.org</a>><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</blockquote><span class="">
<br>
<br>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
<br>
</span></blockquote><span class="HOEnZb"><font color="#888888">
<br>
-- <br>
Loïc Hoguin<br>
<a href="https://ninenines.eu" rel="noreferrer" target="_blank">https://ninenines.eu</a></font></span><div class="HOEnZb"><div class="h5"><br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</div></div></blockquote></div><br></div>