Writing a simple JSON parser

If you write it entirely from scratch, you need more than a couple dozen lines:

  • Validating the number notation. Valid: -0, -1, -0.1, 0e+1, 0e-1, 0.000000000000001. Invalid: +1, 1ee, 1-, NaN, -Infinity, +Infinity,
  • Translation of \u0000-Notation to UTF-8. Including UTF-16 surrogate pairs

And some egde cases along the way you have to keep an eye out for.

Sure, JSON is much easier to parse and validate than XML, but if you want a fully compliant parser thats somewhat fast, you need more than a couple dozen lines.

In my parser, the number validation caused a lot of headache. First I used a simple approach of converting the string to double and back to string and compared if output === "0" && input === "0". But that was really fucking slow and broke way to easily (input = "0e1"). So, my number validation grew from 3 lines to ~80.

/r/programming Thread Parent Link - notes.eatonphil.com