Commit 18c6134e authored by lrn@chromium.org's avatar lrn@chromium.org

Updated JSON.stringify to newest version of ES5.

Review URL: http://codereview.chromium.org/562034

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3787 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent f74a08d8
...@@ -80,8 +80,9 @@ var characterQuoteCache = { ...@@ -80,8 +80,9 @@ var characterQuoteCache = {
}; };
function QuoteSingleJSONCharacter(c) { function QuoteSingleJSONCharacter(c) {
if (c in characterQuoteCache) if (c in characterQuoteCache) {
return characterQuoteCache[c]; return characterQuoteCache[c];
}
var charCode = c.charCodeAt(0); var charCode = c.charCodeAt(0);
var result; var result;
if (charCode < 16) result = '\\u000'; if (charCode < 16) result = '\\u000';
...@@ -101,15 +102,17 @@ function QuoteJSONString(str) { ...@@ -101,15 +102,17 @@ function QuoteJSONString(str) {
function StackContains(stack, val) { function StackContains(stack, val) {
var length = stack.length; var length = stack.length;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (stack[i] === val) if (stack[i] === val) {
return true; return true;
}
} }
return false; return false;
} }
function SerializeArray(value, replacer, stack, indent, gap) { function SerializeArray(value, replacer, stack, indent, gap) {
if (StackContains(stack, value)) if (StackContains(stack, value)) {
throw MakeTypeError('circular_structure', []); throw MakeTypeError('circular_structure', []);
}
stack.push(value); stack.push(value);
var stepback = indent; var stepback = indent;
indent += gap; indent += gap;
...@@ -117,9 +120,10 @@ function SerializeArray(value, replacer, stack, indent, gap) { ...@@ -117,9 +120,10 @@ function SerializeArray(value, replacer, stack, indent, gap) {
var len = value.length; var len = value.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var strP = JSONSerialize($String(i), value, replacer, stack, var strP = JSONSerialize($String(i), value, replacer, stack,
indent, gap); indent, gap);
if (IS_UNDEFINED(strP)) if (IS_UNDEFINED(strP)) {
strP = "null"; strP = "null";
}
partial.push(strP); partial.push(strP);
} }
var final; var final;
...@@ -137,8 +141,9 @@ function SerializeArray(value, replacer, stack, indent, gap) { ...@@ -137,8 +141,9 @@ function SerializeArray(value, replacer, stack, indent, gap) {
} }
function SerializeObject(value, replacer, stack, indent, gap) { function SerializeObject(value, replacer, stack, indent, gap) {
if (StackContains(stack, value)) if (StackContains(stack, value)) {
throw MakeTypeError('circular_structure', []); throw MakeTypeError('circular_structure', []);
}
stack.push(value); stack.push(value);
var stepback = indent; var stepback = indent;
indent += gap; indent += gap;
...@@ -188,17 +193,21 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) { ...@@ -188,17 +193,21 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
var value = holder[key]; var value = holder[key];
if (IS_OBJECT(value) && value) { if (IS_OBJECT(value) && value) {
var toJSON = value.toJSON; var toJSON = value.toJSON;
if (IS_FUNCTION(toJSON)) if (IS_FUNCTION(toJSON)) {
value = toJSON.call(value, key); value = toJSON.call(value, key);
}
} }
if (IS_FUNCTION(replacer)) if (IS_FUNCTION(replacer)) {
value = replacer.call(holder, key, value); value = replacer.call(holder, key, value);
}
// Unwrap value if necessary // Unwrap value if necessary
if (IS_OBJECT(value)) { if (IS_OBJECT(value)) {
if (IS_NUMBER_WRAPPER(value)) { if (IS_NUMBER_WRAPPER(value)) {
value = $Number(value); value = $Number(value);
} else if (IS_STRING_WRAPPER(value)) { } else if (IS_STRING_WRAPPER(value)) {
value = $String(value); value = $String(value);
} else if (IS_BOOLEAN_WRAPPER(value)) {
value = $Boolean(value);
} }
} }
switch (typeof value) { switch (typeof value) {
...@@ -232,12 +241,17 @@ function JSONStringify(value, replacer, space) { ...@@ -232,12 +241,17 @@ function JSONStringify(value, replacer, space) {
} }
var gap; var gap;
if (IS_NUMBER(space)) { if (IS_NUMBER(space)) {
space = $Math.min(space, 100); space = $Math.min(space, 10);
gap = ""; gap = "";
for (var i = 0; i < space; i++) for (var i = 0; i < space; i++) {
gap += " "; gap += " ";
}
} else if (IS_STRING(space)) { } else if (IS_STRING(space)) {
gap = space; if (space.length > 10) {
gap = space.substring(0, 10);
} else {
gap = space;
}
} else { } else {
gap = ""; gap = "";
} }
......
...@@ -200,8 +200,10 @@ TestInvalid('"Unterminated string'); ...@@ -200,8 +200,10 @@ TestInvalid('"Unterminated string');
TestInvalid('"Unterminated string\\"'); TestInvalid('"Unterminated string\\"');
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("{true:42}");
TestInvalid("{false:42}"); TestInvalid("{false:42}");
TestInvalid("{null:42}"); TestInvalid("{null:42}");
...@@ -211,7 +213,6 @@ TestInvalid("{0:42}"); ...@@ -211,7 +213,6 @@ TestInvalid("{0:42}");
TestInvalid("{-1:42}"); TestInvalid("{-1:42}");
// Test for trailing garbage detection. // Test for trailing garbage detection.
TestInvalid('42 px'); TestInvalid('42 px');
TestInvalid('42 .2'); TestInvalid('42 .2');
TestInvalid('42 2'); TestInvalid('42 2');
...@@ -277,8 +278,36 @@ assertEquals('{\n "a": "b",\n "c": "d"\n}', ...@@ -277,8 +278,36 @@ assertEquals('{\n "a": "b",\n "c": "d"\n}',
JSON.stringify({a:"b",c:"d"}, null, 1)); JSON.stringify({a:"b",c:"d"}, null, 1));
assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x'])); 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(undefined));
assertEquals(undefined, JSON.stringify(function () { })); 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'); TestInvalid('1); throw "foo"; (1');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment