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


Groups > comp.lang.javascript > #38665

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

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: JSON, definitions, standards - things I do not understand
Date 2020-08-05 10:15 +0200
Organization PointedEars Software (PES)
Message-ID <5529528.MhkbZ0Pkbq@PointedEars.de> (permalink)
References <hors7qF9m1cU1@mid.individual.net> <06494226-02c5-49dc-b33d-4064ffa1e30ao@googlegroups.com> <host39Fg8fpU1@mid.individual.net>

Show all headers | View raw


retired developer wrote:
^^^^^^^^^^^^^^^^^
Your real name belongs there.

> On 04.08.20 11:07, Julio Di Egidio wrote:
> 
> Hello Julio,

This is a public Usenet newsgroup.  If you want to communicate with only one 
person, write them an e-mail.
 
>> 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.

No.  JSON is a *small* *subset* of valid ECMAScript code.

  {'foo': /bar/}

is valid ECMAScript code.  It is NOT valid JSON: in JSON, string literals 
have to be delimited with <">, not <'>, and there are no RegExp literals.
 
> 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]?

Because it satisfies the *ECMAScript* grammar, but not the *JSON* grammar.

<https://json.org/>

The only way to have that parsed into a value equivalent to the one assiged 
above is using eval().
 
> The former is the same as the latter!

No, it isn’t.

> The difference is the parse time only.

No, the first is a piece of ECMAScript code.  The second is supposed to be 
parsed as JSON.

> The first is parsed at compile time the second is parsed at runtime.

Yes, but that is not the only difference.

>> In JS, (in general) arrays are sparse

No.  You can have sparse arrays, though.

>> 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.

Because it does not give the number of elements, but the value of the 
greatest used index + 1.

> In this case JavaScript thinks "in the
> old fashioned way", an array has always all elements, from the first
> until the last.

No, *you* are thinking the old-fashioned way.
 
> I would have done it that way:

Nobody cares about your inefficient fantasies.  But you are free to subclass 
Array to implement additional features if you find them necessary or useful.

> array.count  = <countOfExistingElemens>
> array.first  = <indexOfFirstExistingElement>
> array.last   = <indexOfLastExistingElement>

  class RetiredDeveloperArray extends Array
  {
    constructor (...args)
    {
      if (Array.isArray(args[0]))
      {
        super(...args[0]);
      }
      else
      {
        super(...args);
      }
    }

    get count () { return Array.from(this.keys()).length; }
    get first () { return Array.from(this.keys())[0]; }
    get last () {
      let keys = Array.from(this.keys());
      return keys[keys.length - 1];
    }
  }

Then use “new RetiredDeveloperArray([11, 22,, 44, 55])” instead of
”[11, 22,, 44, 55]”.

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

That would be a _method_.  “End value”?

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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