};
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';
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;
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;
}
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;
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) {
}
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 = "";
}
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}");
TestInvalid("{-1:42}");
// Test for trailing garbage detection.
-
TestInvalid('42 px');
TestInvalid('42 .2');
TestInvalid('42 2');
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');