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

Side by Side Diff: src/json.js

Issue 11186025: Reimplement a simpler version of JSON.stringify. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | src/json-stringifier.h » ('j') | src/json-stringifier.h » ('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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 var separator = ",\n" + indent; 84 var separator = ",\n" + indent;
85 final = "[\n" + indent + partial.join(separator) + "\n" + 85 final = "[\n" + indent + partial.join(separator) + "\n" +
86 stepback + "]"; 86 stepback + "]";
87 } else { 87 } else {
88 final = "[]"; 88 final = "[]";
89 } 89 }
90 stack.pop(); 90 stack.pop();
91 return final; 91 return final;
92 } 92 }
93 93
94 function SerializeObject(value, replacer, stack, indent, gap) { 94 function SerializeObject(value, replacer, stack, indent, gap) {
Toon Verwaest 2012/10/17 17:52:50 We should try the fast path here, rather than in J
Yang 2012/10/18 12:27:33 Will do in a later CL.
95 if (!%PushIfAbsent(stack, value)) { 95 if (!%PushIfAbsent(stack, value)) {
96 throw MakeTypeError('circular_structure', $Array()); 96 throw MakeTypeError('circular_structure', $Array());
97 } 97 }
98 var stepback = indent; 98 var stepback = indent;
99 indent += gap; 99 indent += gap;
100 var partial = new InternalArray(); 100 var partial = new InternalArray();
101 if (IS_ARRAY(replacer)) { 101 if (IS_ARRAY(replacer)) {
102 var length = replacer.length; 102 var length = replacer.length;
103 for (var i = 0; i < length; i++) { 103 for (var i = 0; i < length; i++) {
104 if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) { 104 if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) {
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 BasicSerializeArray(value, stack, builder); 300 BasicSerializeArray(value, stack, builder);
301 } else { 301 } else {
302 BasicSerializeObject(value, stack, builder); 302 BasicSerializeObject(value, stack, builder);
303 } 303 }
304 } 304 }
305 } 305 }
306 306
307 307
308 function JSONStringify(value, replacer, space) { 308 function JSONStringify(value, replacer, space) {
309 if (%_ArgumentsLength() == 1) { 309 if (%_ArgumentsLength() == 1) {
310 var result = %BasicJSONStringify(value);
311 if (result != 0) return result;
Toon Verwaest 2012/10/17 17:52:50 If possible, also use IS_UNDEFINED here; like belo
Yang 2012/10/18 12:27:33 I don't think this is necessary: if nothing is wri
310 var builder = new InternalArray(); 312 var builder = new InternalArray();
311 BasicJSONSerialize('', value, new InternalArray(), builder); 313 BasicJSONSerialize('', value, new InternalArray(), builder);
312 if (builder.length == 0) return; 314 if (builder.length == 0) return;
313 var result = %_FastAsciiArrayJoin(builder, ""); 315 result = %_FastAsciiArrayJoin(builder, "");
314 if (!IS_UNDEFINED(result)) return result; 316 if (!IS_UNDEFINED(result)) return result;
315 return %StringBuilderConcat(builder, builder.length, ""); 317 return %StringBuilderConcat(builder, builder.length, "");
316 } 318 }
317 if (IS_OBJECT(space)) { 319 if (IS_OBJECT(space)) {
318 // Unwrap 'space' if it is wrapped 320 // Unwrap 'space' if it is wrapped
319 if (IS_NUMBER_WRAPPER(space)) { 321 if (IS_NUMBER_WRAPPER(space)) {
320 space = ToNumber(space); 322 space = ToNumber(space);
321 } else if (IS_STRING_WRAPPER(space)) { 323 } else if (IS_STRING_WRAPPER(space)) {
322 space = ToString(space); 324 space = ToString(space);
323 } 325 }
(...skipping 16 matching lines...) Expand all
340 342
341 function SetUpJSON() { 343 function SetUpJSON() {
342 %CheckIsBootstrapping(); 344 %CheckIsBootstrapping();
343 InstallFunctions($JSON, DONT_ENUM, $Array( 345 InstallFunctions($JSON, DONT_ENUM, $Array(
344 "parse", JSONParse, 346 "parse", JSONParse,
345 "stringify", JSONStringify 347 "stringify", JSONStringify
346 )); 348 ));
347 } 349 }
348 350
349 SetUpJSON(); 351 SetUpJSON();
OLDNEW
« no previous file with comments | « no previous file | src/json-stringifier.h » ('j') | src/json-stringifier.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698