From Wikipedia’s article on JSON
SyntaxError: Unexpected token ILLEGAL? That doesn’t make any sense! It’s just a regular object literal, how can that be a SyntaxError?
Try the same code in a proper JSON parser. No problems at all.
Of course it’s not just a regular object literal. There’s a sneaky little Unicode character in there too: Right between “ro” and “cks!” there’s a tiny U+2028. Your browser probably doesn’t display it because it’s whitespace: LINE SEPARATOR, but it’s still there. If you replace the character with a U+2029 (PARAGRAPH SEPARATOR) you would have the exact same issue.
JSON + U+2028 = ☺
According to the JSON specification, you can safely use this character in any string. It’s not a quote, not a backslash, and not a control character. It’s just a weird Unicode whitespace character:
DoubleStringCharacter :: SourceCharacter but not double-quote " or backslash \ or LineTerminator \ EscapeSequence SingleStringCharacter :: SourceCharacter but not single-quote ' or backslash \ or LineTerminator \ EscapeSequence
And what is a line terminator? Let’s have a look at 7.3 Line Terminators:
The following characters are consider to be line terminators:
\u000A- Line Feed
\u000D- Carriage Return
\u2028- Line separator
\u2029- Paragraph separator
In most applications, you won’t notice this issue. First of all, the line separator and the paragraph separator isn’t exactly widely used. Secondly, any proper JSON parser will have no problems with parsing it.
Luckily, the solution is simple: If we look at the JSON specification we see that the only place where a U+2028 or U+2029 can occur is in a string. Therefore we can simply replace every U+2028 with
\u2028 (the escape sequence) and U+2029 with
\u2029 whenever we need to send out some JSONP.
It’s already been fixed in Rack::JSONP and I encourage all frameworks or libraries that send out JSONP to do the same. It’s a one-line patch in most languages and the result is still 100% valid JSON.