[Pharo-users] JSON keys not quoted?

Sven Van Caekenberghe sven at stfx.eu
Mon Feb 25 11:05:21 EST 2019



> On 23 Feb 2019, at 03:53, Ben Coman <btc at openinworld.com> wrote:
> 
> Just curiousity... Is an idempotent round trip required/expected ?

Not necessarily, but symmetry is important and a lack of symmetry is often a possible indication of trouble.

Anyway, since the standard is crystal clear that JSON keys/names in objects must be strings, I am enforcing that now. It is not OK to silently generate illegal JSON.

https://github.com/svenvc/NeoJSON/commit/c368bbf475f60d564cd3fb708e99b92efea8ffd3

(And I made the same change to STON in JSON mode)

> cheers -ben
> 
> On Sat, 23 Feb 2019 at 05:24, Sven Van Caekenberghe <sven at stfx.eu> wrote:
> 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