Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: src/json.js

Issue 3336001: Make JSON.stringify not quote non-ASCII characters. Fix bug 855. (Closed)
Patch Set: Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/mjsunit/json.js » ('j') | test/mjsunit/json.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 function JSONParse(text, reviver) { 61 function JSONParse(text, reviver) {
62 var unfiltered = ParseJSONUnfiltered(text); 62 var unfiltered = ParseJSONUnfiltered(text);
63 if (IS_FUNCTION(reviver)) { 63 if (IS_FUNCTION(reviver)) {
64 return Revive({'': unfiltered}, '', reviver); 64 return Revive({'': unfiltered}, '', reviver);
65 } else { 65 } else {
66 return unfiltered; 66 return unfiltered;
67 } 67 }
68 } 68 }
69 69
70 var characterQuoteCache = { 70 var characterQuoteCache = {
71 '\b': '\\b', // ASCII 8, Backspace
72 '\t': '\\t', // ASCII 9, Tab
73 '\n': '\\n', // ASCII 10, Newline
74 '\f': '\\f', // ASCII 12, Formfeed
75 '\r': '\\r', // ASCII 13, Carriage Return
71 '\"': '\\"', 76 '\"': '\\"',
72 '\\': '\\\\', 77 '/': '\\/'
73 '/': '\\/',
74 '\b': '\\b',
75 '\f': '\\f',
76 '\n': '\\n',
77 '\r': '\\r',
78 '\t': '\\t',
79 '\x0B': '\\u000b'
80 }; 78 };
81 79
82 function QuoteSingleJSONCharacter(c) { 80 function QuoteSingleJSONCharacter(c) {
83 if (c in characterQuoteCache) { 81 if (c in characterQuoteCache) {
84 return characterQuoteCache[c]; 82 return characterQuoteCache[c];
85 } 83 }
86 var charCode = c.charCodeAt(0); 84 var charCode = c.charCodeAt(0);
87 var result; 85 var result;
88 if (charCode < 16) result = '\\u000'; 86 if (charCode < 16) result = '\\u000';
89 else if (charCode < 256) result = '\\u00'; 87 else if (charCode < 256) result = '\\u00';
90 else if (charCode < 4096) result = '\\u0'; 88 else if (charCode < 4096) result = '\\u0';
91 else result = '\\u'; 89 else result = '\\u';
92 result += charCode.toString(16); 90 result += charCode.toString(16);
93 characterQuoteCache[c] = result; 91 characterQuoteCache[c] = result;
94 return result; 92 return result;
95 } 93 }
96 94
97 function QuoteJSONString(str) { 95 function QuoteJSONString(str) {
98 var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; 96 var quotable = /[\\\"\x00-\x1f]/g;
99 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; 97 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"';
100 } 98 }
101 99
102 function StackContains(stack, val) { 100 function StackContains(stack, val) {
103 var length = stack.length; 101 var length = stack.length;
104 for (var i = 0; i < length; i++) { 102 for (var i = 0; i < length; i++) {
105 if (stack[i] === val) { 103 if (stack[i] === val) {
106 return true; 104 return true;
107 } 105 }
108 } 106 }
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 } 257 }
260 258
261 function SetupJSON() { 259 function SetupJSON() {
262 InstallFunctions($JSON, DONT_ENUM, $Array( 260 InstallFunctions($JSON, DONT_ENUM, $Array(
263 "parse", JSONParse, 261 "parse", JSONParse,
264 "stringify", JSONStringify 262 "stringify", JSONStringify
265 )); 263 ));
266 } 264 }
267 265
268 SetupJSON(); 266 SetupJSON();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/json.js » ('j') | test/mjsunit/json.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698