[Pharo-users] JSON keys not quoted?

Sven Van Caekenberghe sven at stfx.eu
Fri Feb 22 16:23:25 EST 2019


Good catch.

Indeed, JSON keys have to be strings.

The question is, should the writer convert them for you, or should you simply not use them ?

I mean, there are many Pharo constructs that cannot be converted to JSON.

Some could be output to JSON, but you won't be able to read them back in.

So, if conversion would happen, the dictionary with integer keys will be read back with strings keys.

In STON, keys can be anything, even complex objects, like in Pharo itself.

What will you do with { (1 at 2)->100 } asDictionary in JSON ?

I have to think about this.

> On 22 Feb 2019, at 21:21, Esteban Maringolo <emaringolo at gmail.com> wrote:
> 
> If you convert a Dictionary with numbers as keys it will produce not
> valid JSON [1], since all object keys must be quoted.
> 
> E.g.
> NeoJSONWriter toString: (Dictionary with: 1->'foo').
> or
> (Dictionary with: 1->'foo') asJson
> 
> Should the library generate quoted keys?
> 
> Seaside's WAJsonCanvas is manual, so it is up to the developer to
> render the object properly, but NeoJSON does it automatically.
> 
> In Javascript this will throw a syntax error:
>> JSON.parse('{1: "foo"}')
> 
> And this would generate a quoted key when serialized:
>> JSON.stringify({1: "foo"})
>> {"1": "foo"}
> 
> So my question is: should the library quote whatever you put as key in
> order to produce valid JSON syntax? [2]
> 
> Regards.
> 
> [1] https://jsonlint.com/
> [2] https://www.ecma-international.org/publications/standards/Ecma-404.htm
> 
> Esteban A. Maringolo
> 




More information about the Pharo-users mailing list