Revision: 3787
Author: [email protected]
Date: Wed Feb  3 05:17:39 2010
Log: Updated JSON.stringify to newest version of ES5.

Review URL: http://codereview.chromium.org/562034
http://code.google.com/p/v8/source/detail?r=3787

Modified:
 /branches/bleeding_edge/src/json-delay.js
 /branches/bleeding_edge/test/mjsunit/json.js

=======================================
--- /branches/bleeding_edge/src/json-delay.js   Mon Feb  1 02:31:55 2010
+++ /branches/bleeding_edge/src/json-delay.js   Wed Feb  3 05:17:39 2010
@@ -80,8 +80,9 @@
 };

 function QuoteSingleJSONCharacter(c) {
-  if (c in characterQuoteCache)
+  if (c in characterQuoteCache) {
     return characterQuoteCache[c];
+  }
   var charCode = c.charCodeAt(0);
   var result;
   if (charCode < 16) result = '\\u000';
@@ -101,15 +102,17 @@
 function StackContains(stack, val) {
   var length = stack.length;
   for (var i = 0; i < length; i++) {
-    if (stack[i] === val)
+    if (stack[i] === val) {
       return true;
+    }
   }
   return false;
 }

 function SerializeArray(value, replacer, stack, indent, gap) {
-  if (StackContains(stack, value))
+  if (StackContains(stack, value)) {
     throw MakeTypeError('circular_structure', []);
+  }
   stack.push(value);
   var stepback = indent;
   indent += gap;
@@ -117,9 +120,10 @@
   var len = value.length;
   for (var i = 0; i < len; i++) {
     var strP = JSONSerialize($String(i), value, replacer, stack,
-        indent, gap);
-    if (IS_UNDEFINED(strP))
+                             indent, gap);
+    if (IS_UNDEFINED(strP)) {
       strP = "null";
+    }
     partial.push(strP);
   }
   var final;
@@ -137,8 +141,9 @@
 }

 function SerializeObject(value, replacer, stack, indent, gap) {
-  if (StackContains(stack, value))
+  if (StackContains(stack, value)) {
     throw MakeTypeError('circular_structure', []);
+  }
   stack.push(value);
   var stepback = indent;
   indent += gap;
@@ -188,17 +193,21 @@
   var value = holder[key];
   if (IS_OBJECT(value) && value) {
     var toJSON = value.toJSON;
-    if (IS_FUNCTION(toJSON))
+    if (IS_FUNCTION(toJSON)) {
       value = toJSON.call(value, key);
-  }
-  if (IS_FUNCTION(replacer))
+    }
+  }
+  if (IS_FUNCTION(replacer)) {
     value = replacer.call(holder, key, value);
+  }
   // Unwrap value if necessary
   if (IS_OBJECT(value)) {
     if (IS_NUMBER_WRAPPER(value)) {
       value = $Number(value);
     } else if (IS_STRING_WRAPPER(value)) {
       value = $String(value);
+    } else if (IS_BOOLEAN_WRAPPER(value)) {
+      value = $Boolean(value);
     }
   }
   switch (typeof value) {
@@ -232,12 +241,17 @@
   }
   var gap;
   if (IS_NUMBER(space)) {
-    space = $Math.min(space, 100);
+    space = $Math.min(space, 10);
     gap = "";
-    for (var i = 0; i < space; i++)
+    for (var i = 0; i < space; i++) {
       gap += " ";
+    }
   } else if (IS_STRING(space)) {
-    gap = space;
+    if (space.length > 10) {
+      gap = space.substring(0, 10);
+    } else {
+      gap = space;
+    }
   } else {
     gap = "";
   }
=======================================
--- /branches/bleeding_edge/test/mjsunit/json.js        Mon Feb  1 02:31:55 2010
+++ /branches/bleeding_edge/test/mjsunit/json.js        Wed Feb  3 05:17:39 2010
@@ -200,8 +200,10 @@
 TestInvalid('"Unterminated string\\"');
 TestInvalid('"Unterminated string\\\\\\"');

-// Test bad JSON that would be good JavaScript (ES5).
-
+// JavaScript RegExp literals not valid in JSON.
+TestInvalid('/true/');
+
+// Test bad JSON that would be good JavaScript (ES5).
 TestInvalid("{true:42}");
 TestInvalid("{false:42}");
 TestInvalid("{null:42}");
@@ -211,7 +213,6 @@
 TestInvalid("{-1:42}");

 // Test for trailing garbage detection.
-
 TestInvalid('42 px');
 TestInvalid('42 .2');
 TestInvalid('42 2');
@@ -277,8 +278,36 @@
              JSON.stringify({a:"b",c:"d"}, null, 1));
 assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));

+// The gap is capped at ten characters if specified as string.
+assertEquals('{\n          "a": "b",\n          "c": "d"\n}',
+              JSON.stringify({a:"b",c:"d"}, null,
+                             "          /*characters after 10th*/"));
+
+//The gap is capped at ten characters if specified as number.
+assertEquals('{\n          "a": "b",\n          "c": "d"\n}',
+              JSON.stringify({a:"b",c:"d"}, null, 15));
+
+// Replaced wrapped primitives are unwrapped.
+function newx(k, v)  { return (k == "x") ? new v(42) : v; }
+assertEquals('{"x":"42"}', JSON.stringify({x: String}, newx));
+assertEquals('{"x":42}', JSON.stringify({x: Number}, newx));
+assertEquals('{"x":true}', JSON.stringify({x: Boolean}, newx));
+
 assertEquals(undefined, JSON.stringify(undefined));
 assertEquals(undefined, JSON.stringify(function () { }));
+//
+Arrays with missing, undefined or function elements have those elements
+// replaced by null.
+assertEquals("[null,null,null]",
+             JSON.stringify([undefined,,function(){}]));
+
+// Objects with undefined or function properties (including replaced properties)
+// have those properties ignored.
+assertEquals('{}',
+             JSON.stringify({a: undefined, b: function(){}, c: 42, d: 42},
+ function(k, v) { if (k == "c") return undefined; + if (k == "d") return function(){};
+                                             return v; }));

 TestInvalid('1); throw "foo"; (1');

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to