From: lrn@chromium.org Date: Thu, 2 Sep 2010 07:09:28 +0000 (+0000) Subject: Make JSON.stringify not quote non-ASCII characters. Fix bug 855. X-Git-Tag: upstream/4.7.83~21253 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6670c0ad38adf935c4d1d4a2ae93125a187e4a12;p=platform%2Fupstream%2Fv8.git Make JSON.stringify not quote non-ASCII characters. Fix bug 855. Review URL: http://codereview.chromium.org/3336001 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5398 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/json.js b/src/json.js index e7ec610..5dd7768 100644 --- a/src/json.js +++ b/src/json.js @@ -68,15 +68,13 @@ function JSONParse(text, reviver) { } var characterQuoteCache = { + '\b': '\\b', // ASCII 8, Backspace + '\t': '\\t', // ASCII 9, Tab + '\n': '\\n', // ASCII 10, Newline + '\f': '\\f', // ASCII 12, Formfeed + '\r': '\\r', // ASCII 13, Carriage Return '\"': '\\"', - '\\': '\\\\', - '/': '\\/', - '\b': '\\b', - '\f': '\\f', - '\n': '\\n', - '\r': '\\r', - '\t': '\\t', - '\x0B': '\\u000b' + '/': '\\/' }; function QuoteSingleJSONCharacter(c) { @@ -95,7 +93,7 @@ function QuoteSingleJSONCharacter(c) { } function QuoteJSONString(str) { - var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; + var quotable = /[\\\"\x00-\x1f]/g; return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; } diff --git a/test/mjsunit/json.js b/test/mjsunit/json.js index 945b662..5353d6c 100644 --- a/test/mjsunit/json.js +++ b/test/mjsunit/json.js @@ -317,3 +317,32 @@ TestInvalid('1); x++; (1'); // Test string conversion of argument. var o = { toString: function() { return "42"; } }; assertEquals(42, JSON.parse(o)); + + +for (var i = 0; i < 65536; i++) { + var string = String.fromCharCode(i); + var encoded = JSON.stringify(string); + var expected = "uninitialized"; + // Following the ES5 specification of the abstraction function Quote. + if (string == '"' || string == '\\') { + // Step 2.a + expected = '\\' + string; + } else if ("\b\t\n\r\f".indexOf(string) >= 0) { + // Step 2.b + if (string == '\b') expected = '\\b'; + else if (string == '\t') expected = '\\t'; + else if (string == '\n') expected = '\\n'; + else if (string == '\f') expected = '\\f'; + else if (string == '\r') expected = '\\r'; + } else if (i < 32) { + // Step 2.c + if (i < 16) { + expected = "\\u000" + i.toString(16); + } else { + expected = "\\u00" + i.toString(16); + } + } else { + expected = string; + } + assertEquals('"' + expected + '"', encoded, "Codepoint " + i); +}