JSONNumber - optional decimal

Garrett Smith dhtmlkitchen at gmail.com
Tue Jun 8 20:46:18 PDT 2010


Today I looked for a good json regexp tester and finding nothing,
decided to write one.

The strategy that occurred to me was to first define a regex for the
literal components (ES5 lumps literal value into the JSONValue
alongside JSONObject and JSONArray). That way, I could reuse the
literal components to define JSONObject and JSONArray.

In the process, I noticed that the grammar defined for JSONNumber is
different than that defiend by json2.js, which I had just looked at,
and which has similar numeric parsing as seen in The GOod parts. That
is, it allows DecimalLiteral, which includes this production:

  DecimalIntegerLiteral .

In contrast, JSONNumber does not allow that.

JSONNumber ::
-opt  DecimalIntegerLiteral JSONFractionopt  ExponentPartopt

Am I misreading the spec, or is "1." not valid JSON?

Next step was to see what the browsers do.

Mozilla and IE accept it, while Webkit and Opera throws an error.

 JSON.parse("1.")

Firefox 3.6, IE8, Besen r27:
1

Safari, Opera
SyntaxError

IF anyone has a correct JSON parser, I would appreciate it. Also, are
there any good test suites for JSON?

Garrett


More information about the es-discuss mailing list