Will JSON commas remain?

So, I read about the proposed JavaScript functions, and one of them concerns reverse comma support in object literals and arrays.

For parameters, trailing commas are not related here, so do not pay attention to them. I understand the benefits of version control, but I'm worried about how it will respond to JSON.

const arr = [ 'red', 'green', 'blue', ]; 

It will become valid.

But what happens when you return the JSON syntax? JSON is supported by the RFC, so I doubt that JSON will ever support commas. Maybe one day..

But how does JavaScript handle the returning something like:

 const jsonReturn = [{ "derp":1 }, { "foo":"bar" }, { "slide":true, },]; 

Will the trailing comma be removed internally if the header content type is JSON, or do trailing commas interrupt everything?

+5
source share
1 answer

You won't have any problems because the JSON source and JS have nothing to do with each other.

JSON does not (and, for the sake of example, will not) support commas. The current JSON specification clearly shows that commas can only occur between values ​​within an object or array.

If JS introduces support for trailing commas, the original representation of the object and the serializable version are largely unrelated. Most browsers today accept a trailing comma, but all commas are discarded in the representation of the actual object (dict / hash or struct):

 > var foo = {bar: 1, baz: 2,}; < undefined > foo < Object {bar: 1, baz: 2} 

Even today, serializing an object with a trailing comma works fine:

 > JSON.stringify({bar: 1, baz: 2,}) < "{"bar":1,"baz":2}" 

Commas are for parsing only the source and do not exist in the representation of the runtime object.

+7
source

Source: https://habr.com/ru/post/1245917/


All Articles