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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 '/': '\\/', | 73 '/': '\\/', |
74 '\b': '\\b', | 74 '\b': '\\b', |
75 '\f': '\\f', | 75 '\f': '\\f', |
76 '\n': '\\n', | 76 '\n': '\\n', |
77 '\r': '\\r', | 77 '\r': '\\r', |
78 '\t': '\\t', | 78 '\t': '\\t', |
79 '\x0B': '\\u000b' | 79 '\x0B': '\\u000b' |
80 }; | 80 }; |
81 | 81 |
82 function QuoteSingleJSONCharacter(c) { | 82 function QuoteSingleJSONCharacter(c) { |
83 if (c in characterQuoteCache) | 83 if (c in characterQuoteCache) { |
84 return characterQuoteCache[c]; | 84 return characterQuoteCache[c]; |
| 85 } |
85 var charCode = c.charCodeAt(0); | 86 var charCode = c.charCodeAt(0); |
86 var result; | 87 var result; |
87 if (charCode < 16) result = '\\u000'; | 88 if (charCode < 16) result = '\\u000'; |
88 else if (charCode < 256) result = '\\u00'; | 89 else if (charCode < 256) result = '\\u00'; |
89 else if (charCode < 4096) result = '\\u0'; | 90 else if (charCode < 4096) result = '\\u0'; |
90 else result = '\\u'; | 91 else result = '\\u'; |
91 result += charCode.toString(16); | 92 result += charCode.toString(16); |
92 characterQuoteCache[c] = result; | 93 characterQuoteCache[c] = result; |
93 return result; | 94 return result; |
94 } | 95 } |
95 | 96 |
96 function QuoteJSONString(str) { | 97 function QuoteJSONString(str) { |
97 var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; | 98 var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; |
98 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; | 99 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; |
99 } | 100 } |
100 | 101 |
101 function StackContains(stack, val) { | 102 function StackContains(stack, val) { |
102 var length = stack.length; | 103 var length = stack.length; |
103 for (var i = 0; i < length; i++) { | 104 for (var i = 0; i < length; i++) { |
104 if (stack[i] === val) | 105 if (stack[i] === val) |
105 return true; | 106 return true; |
106 } | 107 } |
107 return false; | 108 return false; |
108 } | 109 } |
109 | 110 |
110 function SerializeArray(value, replacer, stack, indent, gap) { | 111 function SerializeArray(value, replacer, stack, indent, gap) { |
111 if (StackContains(stack, value)) | 112 if (StackContains(stack, value)) { |
112 throw MakeTypeError('circular_structure', []); | 113 throw MakeTypeError('circular_structure', []); |
| 114 } |
113 stack.push(value); | 115 stack.push(value); |
114 var stepback = indent; | 116 var stepback = indent; |
115 indent += gap; | 117 indent += gap; |
116 var partial = []; | 118 var partial = []; |
117 var len = value.length; | 119 var len = value.length; |
118 for (var i = 0; i < len; i++) { | 120 for (var i = 0; i < len; i++) { |
119 var strP = JSONSerialize($String(i), value, replacer, stack, | 121 var strP = JSONSerialize($String(i), value, replacer, stack, |
120 indent, gap); | 122 indent, gap); |
121 if (IS_UNDEFINED(strP)) | 123 if (IS_UNDEFINED(strP)) { |
122 strP = "null"; | 124 strP = "null"; |
| 125 } |
123 partial.push(strP); | 126 partial.push(strP); |
124 } | 127 } |
125 var final; | 128 var final; |
126 if (gap == "") { | 129 if (gap == "") { |
127 final = "[" + partial.join(",") + "]"; | 130 final = "[" + partial.join(",") + "]"; |
128 } else if (partial.length > 0) { | 131 } else if (partial.length > 0) { |
129 var separator = ",\n" + indent; | 132 var separator = ",\n" + indent; |
130 final = "[\n" + indent + partial.join(separator) + "\n" + | 133 final = "[\n" + indent + partial.join(separator) + "\n" + |
131 stepback + "]"; | 134 stepback + "]"; |
132 } else { | 135 } else { |
133 final = "[]"; | 136 final = "[]"; |
134 } | 137 } |
135 stack.pop(); | 138 stack.pop(); |
136 return final; | 139 return final; |
137 } | 140 } |
138 | 141 |
139 function SerializeObject(value, replacer, stack, indent, gap) { | 142 function SerializeObject(value, replacer, stack, indent, gap) { |
140 if (StackContains(stack, value)) | 143 if (StackContains(stack, value)) { |
141 throw MakeTypeError('circular_structure', []); | 144 throw MakeTypeError('circular_structure', []); |
| 145 } |
142 stack.push(value); | 146 stack.push(value); |
143 var stepback = indent; | 147 var stepback = indent; |
144 indent += gap; | 148 indent += gap; |
145 var partial = []; | 149 var partial = []; |
146 if (IS_ARRAY(replacer)) { | 150 if (IS_ARRAY(replacer)) { |
147 var length = replacer.length; | 151 var length = replacer.length; |
148 for (var i = 0; i < length; i++) { | 152 for (var i = 0; i < length; i++) { |
149 if (ObjectHasOwnProperty.call(replacer, i)) { | 153 if (ObjectHasOwnProperty.call(replacer, i)) { |
150 var p = replacer[i]; | 154 var p = replacer[i]; |
151 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 155 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
(...skipping 29 matching lines...) Expand all Loading... |
181 final = "{}"; | 185 final = "{}"; |
182 } | 186 } |
183 stack.pop(); | 187 stack.pop(); |
184 return final; | 188 return final; |
185 } | 189 } |
186 | 190 |
187 function JSONSerialize(key, holder, replacer, stack, indent, gap) { | 191 function JSONSerialize(key, holder, replacer, stack, indent, gap) { |
188 var value = holder[key]; | 192 var value = holder[key]; |
189 if (IS_OBJECT(value) && value) { | 193 if (IS_OBJECT(value) && value) { |
190 var toJSON = value.toJSON; | 194 var toJSON = value.toJSON; |
191 if (IS_FUNCTION(toJSON)) | 195 if (IS_FUNCTION(toJSON)) { |
192 value = toJSON.call(value, key); | 196 value = toJSON.call(value, key); |
| 197 } |
193 } | 198 } |
194 if (IS_FUNCTION(replacer)) | 199 if (IS_FUNCTION(replacer)) { |
195 value = replacer.call(holder, key, value); | 200 value = replacer.call(holder, key, value); |
| 201 } |
196 // Unwrap value if necessary | 202 // Unwrap value if necessary |
197 if (IS_OBJECT(value)) { | 203 if (IS_OBJECT(value)) { |
198 if (IS_NUMBER_WRAPPER(value)) { | 204 if (IS_NUMBER_WRAPPER(value)) { |
199 value = $Number(value); | 205 value = $Number(value); |
200 } else if (IS_STRING_WRAPPER(value)) { | 206 } else if (IS_STRING_WRAPPER(value)) { |
201 value = $String(value); | 207 value = $String(value); |
| 208 } else if (IS_BOOLEAN_WRAPPER(value)) { |
| 209 value = $Boolean(value); |
202 } | 210 } |
203 } | 211 } |
204 switch (typeof value) { | 212 switch (typeof value) { |
205 case "string": | 213 case "string": |
206 return QuoteJSONString(value); | 214 return QuoteJSONString(value); |
207 case "object": | 215 case "object": |
208 if (!value) { | 216 if (!value) { |
209 return "null"; | 217 return "null"; |
210 } else if (IS_ARRAY(value)) { | 218 } else if (IS_ARRAY(value)) { |
211 return SerializeArray(value, replacer, stack, indent, gap); | 219 return SerializeArray(value, replacer, stack, indent, gap); |
(...skipping 13 matching lines...) Expand all Loading... |
225 if (IS_OBJECT(space)) { | 233 if (IS_OBJECT(space)) { |
226 // Unwrap 'space' if it is wrapped | 234 // Unwrap 'space' if it is wrapped |
227 if (IS_NUMBER_WRAPPER(space)) { | 235 if (IS_NUMBER_WRAPPER(space)) { |
228 space = $Number(space); | 236 space = $Number(space); |
229 } else if (IS_STRING_WRAPPER(space)) { | 237 } else if (IS_STRING_WRAPPER(space)) { |
230 space = $String(space); | 238 space = $String(space); |
231 } | 239 } |
232 } | 240 } |
233 var gap; | 241 var gap; |
234 if (IS_NUMBER(space)) { | 242 if (IS_NUMBER(space)) { |
235 space = $Math.min(space, 100); | 243 space = $Math.min(space, 10); |
236 gap = ""; | 244 gap = ""; |
237 for (var i = 0; i < space; i++) | 245 for (var i = 0; i < space; i++) { |
238 gap += " "; | 246 gap += " "; |
| 247 } |
239 } else if (IS_STRING(space)) { | 248 } else if (IS_STRING(space)) { |
240 gap = space; | 249 if (space.length > 10) { |
| 250 gap = space.substring(0, 10); |
| 251 } else { |
| 252 gap = space; |
| 253 } |
241 } else { | 254 } else { |
242 gap = ""; | 255 gap = ""; |
243 } | 256 } |
244 return JSONSerialize('', {'': value}, replacer, stack, indent, gap); | 257 return JSONSerialize('', {'': value}, replacer, stack, indent, gap); |
245 } | 258 } |
246 | 259 |
247 function SetupJSON() { | 260 function SetupJSON() { |
248 InstallFunctions($JSON, DONT_ENUM, $Array( | 261 InstallFunctions($JSON, DONT_ENUM, $Array( |
249 "parse", JSONParse, | 262 "parse", JSONParse, |
250 "stringify", JSONStringify | 263 "stringify", JSONStringify |
251 )); | 264 )); |
252 } | 265 } |
253 | 266 |
254 SetupJSON(); | 267 SetupJSON(); |
OLD | NEW |