Whitespace handling in strict mode

Issue #7 resolved
Anonymous created an issue

In strict mode JSMN fails to parse the value of "Year" in the following example:

{ "Day": 26, "Month": 9, "Year": 12 }

This data comes straight from JSON.stringify(data, null, 2) (Firefox 15) and according to json.org ("Whitespace can be inserted between any pair of tokens.") this should be valid JSON.

Thank you for your great work!

Comments (3)

  1. Niko Wayne

    Sorry, the JSON string was supposed to look like this:

      "Day": 26,
      "Month": 9,
      "Year": 12

    The problem is, that the parser stops parsing at the first whitespace (newline) after the value. I believe a simple "if(isspace(c)) continue;" in jsmn_parse_primitive should do the trick.

  2. Serge Zaitsev repo owner

    Thanks for reporting the issue. Check the recent version, should be fixed. Strict mode is not so strict, it just adds SOME extra checks that are valid for json. I don't think adding space characters to the primitive token is a good idea, because the last token in your example should be "12", not "12\n ". So, commas between fields and colons between key and value in object should be checked in the main loop, but I haven't done this yet. Your json is now parsed correctly, I've added it to the tests. And, don't forget jsmn is a primitive minimalist parser, if you need a tool to validate your JSON - consider using grownup alternatives

  3. Log in to comment