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

Side by Side Diff: src/json.js

Issue 5578004: Improved JSON stringify. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « src/array.js ('k') | test/mjsunit/json.js » ('j') | no next file with comments »
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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 198
199 function BasicSerializeArray(value, stack, builder) { 199 function BasicSerializeArray(value, stack, builder) {
200 if (StackContains(stack, value)) { 200 if (StackContains(stack, value)) {
201 throw MakeTypeError('circular_structure', []); 201 throw MakeTypeError('circular_structure', []);
202 } 202 }
203 stack.push(value); 203 stack.push(value);
204 builder.push("["); 204 builder.push("[");
205 var len = value.length; 205 var len = value.length;
206 for (var i = 0; i < len; i++) { 206 for (var i = 0; i < len; i++) {
207 var before = builder.length; 207 var before = builder.length;
208 BasicJSONSerialize($String(i), value, stack, builder); 208 BasicJSONSerialize(i, value, stack, builder);
209 if (before == builder.length) builder.push("null"); 209 if (before == builder.length) builder.push("null");
210 builder.push(","); 210 builder.push(",");
211 } 211 }
212 stack.pop(); 212 stack.pop();
213 if (builder.pop() != ",") { 213 if (builder.pop() != ",") {
214 builder.push("[]"); // Zero length array. Push "[" back on. 214 builder.push("[]"); // Zero length array. Push "[" back on.
215 } else { 215 } else {
216 builder.push("]"); 216 builder.push("]");
217 } 217 }
218 218
219 } 219 }
220 220
221 221
222 function BasicSerializeObject(value, stack, builder) { 222 function BasicSerializeObject(value, stack, builder) {
223 if (StackContains(stack, value)) { 223 if (StackContains(stack, value)) {
224 throw MakeTypeError('circular_structure', []); 224 throw MakeTypeError('circular_structure', []);
225 } 225 }
226 stack.push(value); 226 stack.push(value);
227 builder.push("{"); 227 builder.push("{");
228 for (var p in value) { 228 for (var p in value) {
229 if (ObjectHasOwnProperty.call(value, p)) { 229 if (%HasLocalProperty(value, p)) {
230 builder.push(%QuoteJSONString(p), ":"); 230 builder.push(%QuoteJSONString(p));
231 builder.push(":");
231 var before = builder.length; 232 var before = builder.length;
232 BasicJSONSerialize(p, value, stack, builder); 233 BasicJSONSerialize(p, value, stack, builder);
233 if (before == builder.length) { 234 if (before == builder.length) {
234 builder.pop(); 235 builder.pop();
235 builder.pop(); 236 builder.pop();
236 } else { 237 } else {
237 builder.push(","); 238 builder.push(",");
238 } 239 }
239 } 240 }
240 } 241 }
241 stack.pop(); 242 stack.pop();
242 if (builder.pop() != ",") { 243 if (builder.pop() != ",") {
243 builder.push("{}"); // Object has no own properties. Push "{" back on. 244 builder.push("{}"); // Object has no own properties. Push "{" back on.
244 } else { 245 } else {
245 builder.push("}"); 246 builder.push("}");
246 } 247 }
247 } 248 }
248 249
249 250
250 function BasicJSONSerialize(key, holder, stack, builder) { 251 function BasicJSONSerialize(key, holder, stack, builder) {
251 var value = holder[key]; 252 var value = holder[key];
252 if (IS_OBJECT(value) && value) { 253 if (IS_OBJECT(value) && value) {
253 var toJSON = value.toJSON; 254 var toJSON = value.toJSON;
254 if (IS_FUNCTION(toJSON)) value = toJSON.call(value, key); 255 if (IS_FUNCTION(toJSON)) value = toJSON.call(value, $String(key));
255 } 256 }
256 if (IS_STRING(value)) { 257 if (IS_STRING(value)) {
257 builder.push(%QuoteJSONString(value)); 258 builder.push(%QuoteJSONString(value));
258 } else if (IS_NUMBER(value)) { 259 } else if (IS_NUMBER(value)) {
259 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); 260 builder.push(($isFinite(value) ? %_NumberToString(value) : "null"));
260 } else if (IS_BOOLEAN(value)) { 261 } else if (IS_BOOLEAN(value)) {
261 builder.push((value ? "true" : "false")); 262 builder.push((value ? "true" : "false"));
262 } else if (IS_OBJECT(value)) { 263 } else if (IS_OBJECT(value)) {
263 // Unwrap value if necessary 264 // Unwrap value if necessary
264 if (IS_NUMBER_WRAPPER(value)) { 265 if (IS_NUMBER_WRAPPER(value)) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 } 319 }
319 320
320 function SetupJSON() { 321 function SetupJSON() {
321 InstallFunctions($JSON, DONT_ENUM, $Array( 322 InstallFunctions($JSON, DONT_ENUM, $Array(
322 "parse", JSONParse, 323 "parse", JSONParse,
323 "stringify", JSONStringify 324 "stringify", JSONStringify
324 )); 325 ));
325 } 326 }
326 327
327 SetupJSON(); 328 SetupJSON();
OLDNEW
« no previous file with comments | « src/array.js ('k') | test/mjsunit/json.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698