Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38646

Re: JSON, definitions, standards - things I do not understand

Path csiph.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From retired developer <retired.developer@protonmail.com>
Newsgroups comp.lang.javascript
Subject Re: JSON, definitions, standards - things I do not understand
Date Tue, 4 Aug 2020 12:54:33 +0200
Lines 101
Message-ID <host39Fg8fpU1@mid.individual.net> (permalink)
References <hors7qF9m1cU1@mid.individual.net> <06494226-02c5-49dc-b33d-4064ffa1e30ao@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net tEgRL3ZZsJp0+QZJf2e+EgVIVLttlKfY3Xg2iqYCswqaQELNXU
Cancel-Lock sha1:gVGPINvSTECDj0MymmJHz2033is=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0
In-Reply-To <06494226-02c5-49dc-b33d-4064ffa1e30ao@googlegroups.com>
Content-Language en-US
Xref csiph.com comp.lang.javascript:38646

Show key headers only | View raw


On 04.08.20 11:07, Julio Di Egidio wrote:

Hello Julio,

thank you for the explanations.


> JSON is not JavaScript, strictly speaking it is not even a subset of
> JS: in particular, arrays in JS are sparse, but there is no such thing
> in JSON.  Which overall means you have to be careful when serialising/
> deserialising (i.e. in JS with JSON.stringify/JSON.parse).

OK, JSON is not JavaScript. Clear. One is a "data structure" and the 
other is a (programming) language. But they are related to each other, 
we use JSON structures in JavaScript to solve problems. So at the end of 
the day we work with JSON objects in our JavaScripty code.

Can you explain, why is

var myarray = [11,22,,44,55];

a correct statement but

var mydata = "[11,22,,44,55]";
var myarray = JSON.parse(mydata);

gives an error - Can't parse [11,22,,44,55]?

The former is the same as the latter!
The difference is the parse time only. The first is parsed at compile 
time the second is parsed at runtime.


> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Extra_commas_in_array_literals>

thanks for the link.


> In JS, (in general) arrays are sparse and are a specialisation of
> objects, e.g. an array such as [1,,3] is akin to an object
> { "0": 1, "2": 3 } (I say "akin", not equal, since of course arrays
> have their own extra methods).

I have no problem with sparse arrays. I know and work with them since 1978.

> Now, while the length of a sparse array is still 1 more than the
> index of the last element, when you do a for-in loop, undefined
> elements get skipped... and so on.  It's just how it is.  JSON is
> different and simpler: indeed, it is a known issue with JSON that
> it does not match 1-1 with JS in particular for arrays (there is

JavaScript handles sparse arrays in a way I can't follow.

I don't understand, why the length property
array.length = <indexOfTheLastElement + 1>
does counts missing elements too. In this case JavaScript thinks "in the 
old fashioned way", an array has always all elements, from the first 
until the last.

I would have done it that way:
array.count  = <countOfExistingElemens>
array.first  = <indexOfFirstExistingElement>
array.last   = <indexOfLastExistingElement>
array.length = <indexOfLastElement + 1> // to be compatible with old SW

array.next(i)  a function, to give the next existing element after <i>
                start value for i: null
                end  value  for i: null

Example:
var arr1 = [11,,33];
var arr2 = [, 22,,44];

arr1.count --> 2
arr1.first --> 0
arr1.last  --> 2
arr1.length -> 3
arr1.next(null) --> 0
arr1.next(0)    --> 2
arr1.next(2)    --> null

arr2.count --> 2
arr2.first --> 1
arr2.last  --> 3
arr2.length -> 4
arr2.next(null) --> 1
arr2.next(0)    --> 1
arr2.next(1)    --> 3
arr2.next(3)    --> null

> I hope that gives you an idea.
Oh yes, I got it...

Regards,
Julius

-- 
An old Windows has old security holes.
A new Windows has new security holes.
Another OS has other security holes.
For safety you must care yourself.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

JSON, definitions, standards - things I do not understand retired developer <retired.developer@protonmail.com> - 2020-08-04 03:33 +0200
  Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 02:07 -0700
    Re: JSON, definitions, standards - things I do not understand retired developer <retired.developer@protonmail.com> - 2020-08-04 12:54 +0200
      Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 06:00 -0700
      Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-04 06:25 -0700
      Re: JSON, definitions, standards - things I do not understand Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-05 10:15 +0200
        Re: JSON, definitions, standards - things I do not understand Julio Di Egidio <julio@diegidio.name> - 2020-08-05 02:30 -0700

csiph-web