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
This commit is contained in:
lrn@chromium.org 2010-02-03 13:17:39 +00:00
parent f74a08d8ee
commit 18c6134e1a
2 changed files with 56 additions and 13 deletions

View File

@ -80,8 +80,9 @@ var characterQuoteCache = {
};
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 QuoteJSONString(str) {
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 @@ function SerializeArray(value, replacer, stack, indent, gap) {
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 SerializeArray(value, replacer, stack, indent, gap) {
}
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 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
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 @@ function JSONStringify(value, replacer, space) {
}
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 = "";
}

View File

@ -200,8 +200,10 @@ 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("{false:42}");
TestInvalid("{null:42}");
@ -211,7 +213,6 @@ TestInvalid("{0:42}");
TestInvalid("{-1:42}");
// Test for trailing garbage detection.
TestInvalid('42 px');
TestInvalid('42 .2');
TestInvalid('42 2');
@ -277,8 +278,36 @@ assertEquals('{\n "a": "b",\n "c": "d"\n}',
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');