Re:JavaScript is Good, Actually
by Scarblac on Wednesday May 16, 2018 @03:28AM
(#17080617)
It seems you're not talking about JSON (the serialization format) but rather about Javascript's object literal syntax that inspired JSON. They're really different things.
[ Parent
| on HN ]
An advantage, however, is that you can directly encode Javascript data objects to JSON without having to annotate its fields or anything. I don't recall you being able to do that with Java.
Re:JavaScript is Good, Actually
by imtringued on Wednesday May 16, 2018 @10:55AM
(#17082882)
You can't do that with javascript either unless your data objects are acyclic and if they are then using a JSON parser for Java is equally trivial.
You mean that if they are acyclic, you don't have to annotate the fields in Java either? Because the data objects I want to serialize usually are acyclic.
Perhaps, but probably not. ES6 recognises the distinction, and includes native functions for serialisation. That's first-class treatment if anything is.
JSON.stringify and JSON.parse? They're standard library functions that many languages have, I don't see what's ES6 about them, they're much older.
First class treatment would be if the language had JSON-typed objects with functionality, e.g. something like
const myJson = j'{"key":"value"}';
const value = myJson['key'];
const newJson = myJson.set("otherkey", "newValue");
(using the j'' notation for a hypothetical json-typed value)But AFAIK it doesn't, not even in ES6.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Implementing j`{"key":"value"}`; as a tagged template literal is left as an exercise for the reader.