If you go to json.org, you’ll be amazed by simplicity and cleanliness of JSON format. At least I was.
Now… there are some little things you should know before writing JSON parser:
Take a look at these:
char
any-Unicode-character-
except-”-or-\-or-
control-character
\”
\\
\/
\b
\f
\n
\r
\t
\u four-hex-digits
There’s one escaped char that doesn’t conform to the reality and there’s another char that should be escaped. Could you tell me what are these chars?
Hint: there are actually two chars that should be escaped. Answers are below:
One: forward slash shouldn’t be escaped. Moreover, JSON validators like jslint.org and jsonlint.org accept unescaped forward slashes just fine. The reason for this requirement is that escaped forward slashes add complexity to parsing and reading URLs embedded in JSON.
Two: angle brackets < and > should be escaped. Reason: security. For example, IIS (of some versions) will refuse to accept incoming requests with angle brackets inside. Escape or die.
Oh, and you can escape these brackets in any way, like > or \u0xxx , but parser (at least parser trying to be true to the spec) will convert only \u0xxx form.
PS Programming JSON writer API in COM is an exercise in humility. WriteLong(), WriteDouble, WriteString,WriteLongVal, WriteDoubleVal, WriteStringVal…