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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } else { | 172 } else { |
173 return SerializeObject(value, replacer, stack, indent, gap); | 173 return SerializeObject(value, replacer, stack, indent, gap); |
174 } | 174 } |
175 } | 175 } |
176 // Undefined or a callable object. | 176 // Undefined or a callable object. |
177 return void 0; | 177 return void 0; |
178 } | 178 } |
179 | 179 |
180 | 180 |
181 function BasicSerializeArray(value, stack, builder) { | 181 function BasicSerializeArray(value, stack, builder) { |
| 182 var len = value.length; |
| 183 if (len == 0) { |
| 184 builder.push("[]"); |
| 185 return; |
| 186 } |
182 if (!%PushIfAbsent(stack, value)) { | 187 if (!%PushIfAbsent(stack, value)) { |
183 throw MakeTypeError('circular_structure', []); | 188 throw MakeTypeError('circular_structure', []); |
184 } | 189 } |
185 builder.push("["); | 190 builder.push("["); |
186 var len = value.length; | 191 var val = value[0]; |
187 for (var i = 0; i < len; i++) { | 192 if (IS_STRING(val)) { |
| 193 // First entry is a string. Remaining entries are likely to be strings too. |
| 194 builder.push(%QuoteJSONString(val)); |
| 195 for (var i = 1; i < len; i++) { |
| 196 builder.push(","); |
| 197 val = value[i]; |
| 198 if (IS_STRING(val)) { |
| 199 builder.push(%QuoteJSONString(val)); |
| 200 } else { |
| 201 var before = builder.length; |
| 202 BasicJSONSerialize(i, value[i], stack, builder); |
| 203 if (before == builder.length) builder.push("null"); |
| 204 } |
| 205 } |
| 206 } else if (IS_NUMBER(val)) { |
| 207 // First entry is a number. Remaining entries are likely to be numbers too. |
| 208 builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null"); |
| 209 for (var i = 1; i < len; i++) { |
| 210 builder.push(","); |
| 211 val = value[i]; |
| 212 if (IS_NUMBER(val)) { |
| 213 builder.push(NUMBER_IS_FINITE(val) |
| 214 ? %_NumberToString(val) |
| 215 : "null"); |
| 216 } else { |
| 217 var before = builder.length; |
| 218 BasicJSONSerialize(i, value[i], stack, builder); |
| 219 if (before == builder.length) builder.push("null"); |
| 220 } |
| 221 } |
| 222 } else { |
188 var before = builder.length; | 223 var before = builder.length; |
189 BasicJSONSerialize(i, value, stack, builder); | 224 BasicJSONSerialize(0, val, stack, builder); |
190 if (before == builder.length) builder.push("null"); | 225 if (before == builder.length) builder.push("null"); |
191 builder.push(","); | 226 for (var i = 1; i < len; i++) { |
| 227 builder.push(","); |
| 228 before = builder.length; |
| 229 val = value[i]; |
| 230 BasicJSONSerialize(i, val, stack, builder); |
| 231 if (before == builder.length) builder.push("null"); |
| 232 } |
192 } | 233 } |
193 stack.pop(); | 234 stack.pop(); |
194 if (builder.pop() != ",") { | 235 builder.push("]"); |
195 builder.push("[]"); // Zero length array. Push "[" back on. | |
196 } else { | |
197 builder.push("]"); | |
198 } | |
199 | |
200 } | 236 } |
201 | 237 |
202 | 238 |
203 function BasicSerializeObject(value, stack, builder) { | 239 function BasicSerializeObject(value, stack, builder) { |
204 if (!%PushIfAbsent(stack, value)) { | 240 if (!%PushIfAbsent(stack, value)) { |
205 throw MakeTypeError('circular_structure', []); | 241 throw MakeTypeError('circular_structure', []); |
206 } | 242 } |
207 builder.push("{"); | 243 builder.push("{"); |
208 for (var p in value) { | 244 for (var p in value) { |
209 if (%HasLocalProperty(value, p)) { | 245 if (%HasLocalProperty(value, p)) { |
210 builder.push(%QuoteJSONString(p)); | 246 builder.push(%QuoteJSONString(p)); |
211 builder.push(":"); | 247 builder.push(":"); |
212 var before = builder.length; | 248 var before = builder.length; |
213 BasicJSONSerialize(p, value, stack, builder); | 249 BasicJSONSerialize(p, value[p], stack, builder); |
214 if (before == builder.length) { | 250 if (before == builder.length) { |
215 builder.pop(); | 251 builder.pop(); |
216 builder.pop(); | 252 builder.pop(); |
217 } else { | 253 } else { |
218 builder.push(","); | 254 builder.push(","); |
219 } | 255 } |
220 } | 256 } |
221 } | 257 } |
222 stack.pop(); | 258 stack.pop(); |
223 if (builder.pop() != ",") { | 259 if (builder.pop() != ",") { |
224 builder.push("{}"); // Object has no own properties. Push "{" back on. | 260 builder.push("{}"); // Object has no own properties. Push "{" back on. |
225 } else { | 261 } else { |
226 builder.push("}"); | 262 builder.push("}"); |
227 } | 263 } |
228 } | 264 } |
229 | 265 |
230 | 266 |
231 function BasicJSONSerialize(key, holder, stack, builder) { | 267 function BasicJSONSerialize(key, value, stack, builder) { |
232 var value = holder[key]; | |
233 if (IS_SPEC_OBJECT(value)) { | 268 if (IS_SPEC_OBJECT(value)) { |
234 var toJSON = value.toJSON; | 269 var toJSON = value.toJSON; |
235 if (IS_FUNCTION(toJSON)) { | 270 if (IS_FUNCTION(toJSON)) { |
236 value = %_CallFunction(value, ToString(key), toJSON); | 271 value = %_CallFunction(value, ToString(key), toJSON); |
237 } | 272 } |
238 } | 273 } |
239 if (IS_STRING(value)) { | 274 if (IS_STRING(value)) { |
240 builder.push(%QuoteJSONString(value)); | 275 builder.push(%QuoteJSONString(value)); |
241 } else if (IS_NUMBER(value)) { | 276 } else if (IS_NUMBER(value)) { |
242 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null"); | 277 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null"); |
(...skipping 16 matching lines...) Expand all Loading... |
259 } else { | 294 } else { |
260 BasicSerializeObject(value, stack, builder); | 295 BasicSerializeObject(value, stack, builder); |
261 } | 296 } |
262 } | 297 } |
263 } | 298 } |
264 | 299 |
265 | 300 |
266 function JSONStringify(value, replacer, space) { | 301 function JSONStringify(value, replacer, space) { |
267 if (%_ArgumentsLength() == 1) { | 302 if (%_ArgumentsLength() == 1) { |
268 var builder = []; | 303 var builder = []; |
269 BasicJSONSerialize('', {'': value}, [], builder); | 304 BasicJSONSerialize('', value, [], builder); |
270 if (builder.length == 0) return; | 305 if (builder.length == 0) return; |
271 var result = %_FastAsciiArrayJoin(builder, ""); | 306 var result = %_FastAsciiArrayJoin(builder, ""); |
272 if (!IS_UNDEFINED(result)) return result; | 307 if (!IS_UNDEFINED(result)) return result; |
273 return %StringBuilderConcat(builder, builder.length, ""); | 308 return %StringBuilderConcat(builder, builder.length, ""); |
274 } | 309 } |
275 if (IS_OBJECT(space)) { | 310 if (IS_OBJECT(space)) { |
276 // Unwrap 'space' if it is wrapped | 311 // Unwrap 'space' if it is wrapped |
277 if (IS_NUMBER_WRAPPER(space)) { | 312 if (IS_NUMBER_WRAPPER(space)) { |
278 space = ToNumber(space); | 313 space = ToNumber(space); |
279 } else if (IS_STRING_WRAPPER(space)) { | 314 } else if (IS_STRING_WRAPPER(space)) { |
(...skipping 17 matching lines...) Expand all Loading... |
297 } | 332 } |
298 | 333 |
299 function SetupJSON() { | 334 function SetupJSON() { |
300 InstallFunctions($JSON, DONT_ENUM, $Array( | 335 InstallFunctions($JSON, DONT_ENUM, $Array( |
301 "parse", JSONParse, | 336 "parse", JSONParse, |
302 "stringify", JSONStringify | 337 "stringify", JSONStringify |
303 )); | 338 )); |
304 } | 339 } |
305 | 340 |
306 SetupJSON(); | 341 SetupJSON(); |
OLD | NEW |