[Pharo-users] JSON keys not quoted?

Richard O'Keefe raoknz at gmail.com
Wed Feb 27 19:53:17 EST 2019


If numeric keys are silently written as strings when generating
JSON, you can get JSON that is technically legal but practically
ambiguous.  Start with the obvious:
   (Dictionary new: 2) at: 1 put true; at: '1' put false; asJson
=> {"1": true, "1": false}.
More subtly,
   d := Dictionary new: 2.
   d at: 1.0 put: true.
   d at: 1.0 successor put: false.
   FloatPrintPolicy
     value: InexactFloatPrintPolicy new
     during: [d asJson]
=> {"1.0": true, "1.0": false}.

All things considered, *not* accepting numeric keys seems like
the best thing to do.


On Sat, 23 Feb 2019 at 09:23, 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20190228/cbce9c3b/attachment.html>


More information about the Pharo-users mailing list