Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
JSON: The JavaScript subset that isn't (timelessrepo.com)
4 points by experiment0 on Jan 6, 2013 | hide | past | favorite | 2 comments


Yawn. JSON isn't valid Javascript, but it sure is a valid Javascript data structure.

I often use code like

     echo "var d = ", json_encode($data), ";\n";
and be 100% sure I'm generating perfectly valid Javascript, no matter what is in $data. (Well, apart from circular references, and "resources".)


I think the article's point was that Unicode characters U+2028 and U+2029 are illegal in JavaScript string literals.

Yet, the JSON specification (RFC 4627) officially allows them to exist in JSON string literals:

"All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)."

So with U+2028 or U+2029 you can construct a valid JSON object that JavaScript can't parse.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: