Re:JavaScript is Good, Actually
by sametmax on Wednesday May 16, 2018 @02:57AM
(#17080495)
JSON is seldom JS now, as any language has had encoder/decoder built in for years.
[ Parent
| on HN ]
Sure, but there is boilerplate and abstractions that are needed in a language like Java or Scala. In JavaScript, it's a first-class citizen of the language.
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.
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.
Isn't comparing JS to something like Java or Scala a bit misleading in this regard? They're completely different styles of language with different tradeoffs. I think it would be a lot more fair to compare to something like Python or Ruby, in which case this advantage become a lot less clear IMO.