Path: csiph.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: retired developer 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: References: <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 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. > 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 = 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 = array.first = array.last = array.length = // to be compatible with old SW array.next(i) a function, to give the next existing element after 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.