If you write it entirely from scratch, you need more than a couple dozen lines:
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.