OLD | NEW |
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 function JSONParse(text, reviver) { | 60 function JSONParse(text, reviver) { |
61 var unfiltered = ParseJSONUnfiltered(text); | 61 var unfiltered = ParseJSONUnfiltered(text); |
62 if (IS_FUNCTION(reviver)) { | 62 if (IS_FUNCTION(reviver)) { |
63 return Revive({'': unfiltered}, '', reviver); | 63 return Revive({'': unfiltered}, '', reviver); |
64 } else { | 64 } else { |
65 return unfiltered; | 65 return unfiltered; |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 var characterQuoteCache = { | |
70 '\b': '\\b', // ASCII 8, Backspace | |
71 '\t': '\\t', // ASCII 9, Tab | |
72 '\n': '\\n', // ASCII 10, Newline | |
73 '\f': '\\f', // ASCII 12, Formfeed | |
74 '\r': '\\r', // ASCII 13, Carriage Return | |
75 '\"': '\\"', | |
76 '\\': '\\\\' | |
77 }; | |
78 | |
79 function QuoteSingleJSONCharacter(c) { | |
80 if (c in characterQuoteCache) { | |
81 return characterQuoteCache[c]; | |
82 } | |
83 var charCode = c.charCodeAt(0); | |
84 var result; | |
85 if (charCode < 16) result = '\\u000'; | |
86 else if (charCode < 256) result = '\\u00'; | |
87 else if (charCode < 4096) result = '\\u0'; | |
88 else result = '\\u'; | |
89 result += charCode.toString(16); | |
90 characterQuoteCache[c] = result; | |
91 return result; | |
92 } | |
93 | |
94 function QuoteJSONString(str) { | 69 function QuoteJSONString(str) { |
95 var quotable = /[\\\"\x00-\x1f]/g; | 70 str = %QuoteJSONString(str); |
96 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; | 71 return '"' + str + '"'; |
97 } | 72 } |
98 | 73 |
99 function StackContains(stack, val) { | 74 function StackContains(stack, val) { |
100 var length = stack.length; | 75 var length = stack.length; |
101 for (var i = 0; i < length; i++) { | 76 for (var i = 0; i < length; i++) { |
102 if (stack[i] === val) { | 77 if (stack[i] === val) { |
103 return true; | 78 return true; |
104 } | 79 } |
105 } | 80 } |
106 return false; | 81 return false; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 231 } |
257 | 232 |
258 function SetupJSON() { | 233 function SetupJSON() { |
259 InstallFunctions($JSON, DONT_ENUM, $Array( | 234 InstallFunctions($JSON, DONT_ENUM, $Array( |
260 "parse", JSONParse, | 235 "parse", JSONParse, |
261 "stringify", JSONStringify | 236 "stringify", JSONStringify |
262 )); | 237 )); |
263 } | 238 } |
264 | 239 |
265 SetupJSON(); | 240 SetupJSON(); |
OLD | NEW |