2009-04-24 08:13:09 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
var $JSON = global.JSON;
|
|
|
|
|
|
|
|
function Revive(holder, name, reviver) {
|
|
|
|
var val = holder[name];
|
|
|
|
if (IS_OBJECT(val)) {
|
|
|
|
if (IS_ARRAY(val)) {
|
|
|
|
var length = val.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
var newElement = Revive(val, $String(i), reviver);
|
|
|
|
val[i] = newElement;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var p in val) {
|
|
|
|
if (ObjectHasOwnProperty.call(val, p)) {
|
|
|
|
var newElement = Revive(val, p, reviver);
|
|
|
|
if (IS_UNDEFINED(newElement)) {
|
|
|
|
delete val[p];
|
|
|
|
} else {
|
|
|
|
val[p] = newElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reviver.call(holder, name, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
function JSONParse(text, reviver) {
|
2010-12-22 13:19:25 +00:00
|
|
|
var unfiltered = %ParseJson(TO_STRING_INLINE(text));
|
2009-04-24 08:13:09 +00:00
|
|
|
if (IS_FUNCTION(reviver)) {
|
|
|
|
return Revive({'': unfiltered}, '', reviver);
|
|
|
|
} else {
|
|
|
|
return unfiltered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function SerializeArray(value, replacer, stack, indent, gap) {
|
2010-12-08 09:10:36 +00:00
|
|
|
if (!%PushIfAbsent(stack, value)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
throw MakeTypeError('circular_structure', []);
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
var stepback = indent;
|
|
|
|
indent += gap;
|
|
|
|
var partial = [];
|
|
|
|
var len = value.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
var strP = JSONSerialize($String(i), value, replacer, stack,
|
2010-02-03 13:17:39 +00:00
|
|
|
indent, gap);
|
|
|
|
if (IS_UNDEFINED(strP)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
strP = "null";
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
partial.push(strP);
|
|
|
|
}
|
|
|
|
var final;
|
|
|
|
if (gap == "") {
|
|
|
|
final = "[" + partial.join(",") + "]";
|
|
|
|
} else if (partial.length > 0) {
|
|
|
|
var separator = ",\n" + indent;
|
|
|
|
final = "[\n" + indent + partial.join(separator) + "\n" +
|
|
|
|
stepback + "]";
|
|
|
|
} else {
|
|
|
|
final = "[]";
|
|
|
|
}
|
|
|
|
stack.pop();
|
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SerializeObject(value, replacer, stack, indent, gap) {
|
2010-12-08 09:10:36 +00:00
|
|
|
if (!%PushIfAbsent(stack, value)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
throw MakeTypeError('circular_structure', []);
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
var stepback = indent;
|
|
|
|
indent += gap;
|
|
|
|
var partial = [];
|
|
|
|
if (IS_ARRAY(replacer)) {
|
|
|
|
var length = replacer.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (ObjectHasOwnProperty.call(replacer, i)) {
|
|
|
|
var p = replacer[i];
|
|
|
|
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
|
|
|
if (!IS_UNDEFINED(strP)) {
|
2010-12-03 11:12:02 +00:00
|
|
|
var member = %QuoteJSONString(p) + ":";
|
2009-04-24 08:13:09 +00:00
|
|
|
if (gap != "") member += " ";
|
|
|
|
member += strP;
|
|
|
|
partial.push(member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var p in value) {
|
|
|
|
if (ObjectHasOwnProperty.call(value, p)) {
|
|
|
|
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
|
|
|
if (!IS_UNDEFINED(strP)) {
|
2010-12-03 11:12:02 +00:00
|
|
|
var member = %QuoteJSONString(p) + ":";
|
2009-04-24 08:13:09 +00:00
|
|
|
if (gap != "") member += " ";
|
|
|
|
member += strP;
|
|
|
|
partial.push(member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var final;
|
|
|
|
if (gap == "") {
|
|
|
|
final = "{" + partial.join(",") + "}";
|
|
|
|
} else if (partial.length > 0) {
|
|
|
|
var separator = ",\n" + indent;
|
|
|
|
final = "{\n" + indent + partial.join(separator) + "\n" +
|
|
|
|
stepback + "}";
|
|
|
|
} else {
|
|
|
|
final = "{}";
|
|
|
|
}
|
|
|
|
stack.pop();
|
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
|
|
|
function JSONSerialize(key, holder, replacer, stack, indent, gap) {
|
|
|
|
var value = holder[key];
|
2010-12-15 09:31:05 +00:00
|
|
|
if (IS_SPEC_OBJECT(value)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
var toJSON = value.toJSON;
|
2010-02-03 13:17:39 +00:00
|
|
|
if (IS_FUNCTION(toJSON)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
value = %_CallFunction(value, key, toJSON);
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
2010-02-03 13:17:39 +00:00
|
|
|
if (IS_FUNCTION(replacer)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
value = %_CallFunction(holder, key, value, replacer);
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2010-12-15 09:31:05 +00:00
|
|
|
if (IS_STRING(value)) {
|
|
|
|
return %QuoteJSONString(value);
|
|
|
|
} else if (IS_NUMBER(value)) {
|
2010-12-22 13:19:25 +00:00
|
|
|
return NUMBER_IS_FINITE(value) ? $String(value) : "null";
|
2010-12-15 09:31:05 +00:00
|
|
|
} else if (IS_BOOLEAN(value)) {
|
|
|
|
return value ? "true" : "false";
|
|
|
|
} else if (IS_NULL(value)) {
|
|
|
|
return "null";
|
|
|
|
} else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) {
|
|
|
|
// Non-callable object. If it's a primitive wrapper, it must be unwrapped.
|
|
|
|
if (IS_ARRAY(value)) {
|
|
|
|
return SerializeArray(value, replacer, stack, indent, gap);
|
|
|
|
} else if (IS_NUMBER_WRAPPER(value)) {
|
|
|
|
value = ToNumber(value);
|
2010-12-22 13:19:25 +00:00
|
|
|
return NUMBER_IS_FINITE(value) ? ToString(value) : "null";
|
2009-04-24 08:13:09 +00:00
|
|
|
} else if (IS_STRING_WRAPPER(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
return %QuoteJSONString(ToString(value));
|
2010-02-03 13:17:39 +00:00
|
|
|
} else if (IS_BOOLEAN_WRAPPER(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
return %_ValueOf(value) ? "true" : "false";
|
|
|
|
} else {
|
|
|
|
return SerializeObject(value, replacer, stack, indent, gap);
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-15 09:31:05 +00:00
|
|
|
// Undefined or a callable object.
|
|
|
|
return void 0;
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 11:44:16 +00:00
|
|
|
|
|
|
|
function BasicSerializeArray(value, stack, builder) {
|
2011-01-12 14:16:19 +00:00
|
|
|
var len = value.length;
|
|
|
|
if (len == 0) {
|
|
|
|
builder.push("[]");
|
|
|
|
return;
|
|
|
|
}
|
2010-12-08 09:10:36 +00:00
|
|
|
if (!%PushIfAbsent(stack, value)) {
|
2010-12-03 11:12:02 +00:00
|
|
|
throw MakeTypeError('circular_structure', []);
|
|
|
|
}
|
2010-12-06 11:44:16 +00:00
|
|
|
builder.push("[");
|
2011-01-12 14:16:19 +00:00
|
|
|
var val = value[0];
|
|
|
|
if (IS_STRING(val)) {
|
|
|
|
// First entry is a string. Remaining entries are likely to be strings too.
|
|
|
|
builder.push(%QuoteJSONString(val));
|
|
|
|
for (var i = 1; i < len; i++) {
|
|
|
|
builder.push(",");
|
|
|
|
val = value[i];
|
|
|
|
if (IS_STRING(val)) {
|
|
|
|
builder.push(%QuoteJSONString(val));
|
|
|
|
} else {
|
|
|
|
var before = builder.length;
|
|
|
|
BasicJSONSerialize(i, value[i], stack, builder);
|
|
|
|
if (before == builder.length) builder.push("null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (IS_NUMBER(val)) {
|
|
|
|
// First entry is a number. Remaining entries are likely to be numbers too.
|
|
|
|
builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null");
|
|
|
|
for (var i = 1; i < len; i++) {
|
|
|
|
builder.push(",");
|
|
|
|
val = value[i];
|
|
|
|
if (IS_NUMBER(val)) {
|
|
|
|
builder.push(NUMBER_IS_FINITE(val)
|
|
|
|
? %_NumberToString(val)
|
|
|
|
: "null");
|
|
|
|
} else {
|
|
|
|
var before = builder.length;
|
|
|
|
BasicJSONSerialize(i, value[i], stack, builder);
|
|
|
|
if (before == builder.length) builder.push("null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-12-06 11:44:16 +00:00
|
|
|
var before = builder.length;
|
2011-01-12 14:16:19 +00:00
|
|
|
BasicJSONSerialize(0, val, stack, builder);
|
2010-12-06 11:44:16 +00:00
|
|
|
if (before == builder.length) builder.push("null");
|
2011-01-12 14:16:19 +00:00
|
|
|
for (var i = 1; i < len; i++) {
|
|
|
|
builder.push(",");
|
|
|
|
before = builder.length;
|
|
|
|
val = value[i];
|
|
|
|
BasicJSONSerialize(i, val, stack, builder);
|
|
|
|
if (before == builder.length) builder.push("null");
|
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
|
|
|
stack.pop();
|
2011-01-12 14:16:19 +00:00
|
|
|
builder.push("]");
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 11:44:16 +00:00
|
|
|
|
|
|
|
function BasicSerializeObject(value, stack, builder) {
|
2010-12-08 09:10:36 +00:00
|
|
|
if (!%PushIfAbsent(stack, value)) {
|
2010-12-03 11:12:02 +00:00
|
|
|
throw MakeTypeError('circular_structure', []);
|
|
|
|
}
|
2010-12-06 11:44:16 +00:00
|
|
|
builder.push("{");
|
2010-12-03 11:12:02 +00:00
|
|
|
for (var p in value) {
|
2010-12-06 15:41:07 +00:00
|
|
|
if (%HasLocalProperty(value, p)) {
|
|
|
|
builder.push(%QuoteJSONString(p));
|
|
|
|
builder.push(":");
|
2010-12-06 11:44:16 +00:00
|
|
|
var before = builder.length;
|
2011-01-12 14:16:19 +00:00
|
|
|
BasicJSONSerialize(p, value[p], stack, builder);
|
2010-12-06 11:44:16 +00:00
|
|
|
if (before == builder.length) {
|
|
|
|
builder.pop();
|
|
|
|
builder.pop();
|
|
|
|
} else {
|
|
|
|
builder.push(",");
|
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stack.pop();
|
2010-12-06 11:44:16 +00:00
|
|
|
if (builder.pop() != ",") {
|
|
|
|
builder.push("{}"); // Object has no own properties. Push "{" back on.
|
|
|
|
} else {
|
|
|
|
builder.push("}");
|
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 11:44:16 +00:00
|
|
|
|
2011-01-12 14:16:19 +00:00
|
|
|
function BasicJSONSerialize(key, value, stack, builder) {
|
2010-12-15 09:31:05 +00:00
|
|
|
if (IS_SPEC_OBJECT(value)) {
|
2010-12-03 11:12:02 +00:00
|
|
|
var toJSON = value.toJSON;
|
2010-12-15 09:31:05 +00:00
|
|
|
if (IS_FUNCTION(toJSON)) {
|
|
|
|
value = %_CallFunction(value, ToString(key), toJSON);
|
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
2010-12-06 11:44:16 +00:00
|
|
|
if (IS_STRING(value)) {
|
|
|
|
builder.push(%QuoteJSONString(value));
|
|
|
|
} else if (IS_NUMBER(value)) {
|
2010-12-22 13:19:25 +00:00
|
|
|
builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
|
2010-12-06 11:44:16 +00:00
|
|
|
} else if (IS_BOOLEAN(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
builder.push(value ? "true" : "false");
|
|
|
|
} else if (IS_NULL(value)) {
|
|
|
|
builder.push("null");
|
|
|
|
} else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) {
|
|
|
|
// Value is a non-callable object.
|
2010-12-06 11:44:16 +00:00
|
|
|
// Unwrap value if necessary
|
2010-12-03 11:12:02 +00:00
|
|
|
if (IS_NUMBER_WRAPPER(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
value = ToNumber(value);
|
2010-12-22 13:19:25 +00:00
|
|
|
builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
|
2010-12-03 11:12:02 +00:00
|
|
|
} else if (IS_STRING_WRAPPER(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
builder.push(%QuoteJSONString(ToString(value)));
|
2010-12-03 11:12:02 +00:00
|
|
|
} else if (IS_BOOLEAN_WRAPPER(value)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
builder.push(%_ValueOf(value) ? "true" : "false");
|
|
|
|
} else if (IS_ARRAY(value)) {
|
|
|
|
BasicSerializeArray(value, stack, builder);
|
2010-12-06 11:44:16 +00:00
|
|
|
} else {
|
2010-12-15 09:31:05 +00:00
|
|
|
BasicSerializeObject(value, stack, builder);
|
2010-12-06 11:44:16 +00:00
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 09:31:05 +00:00
|
|
|
|
2009-04-24 08:13:09 +00:00
|
|
|
function JSONStringify(value, replacer, space) {
|
2010-12-15 09:31:05 +00:00
|
|
|
if (%_ArgumentsLength() == 1) {
|
2010-12-06 11:44:16 +00:00
|
|
|
var builder = [];
|
2011-01-12 14:16:19 +00:00
|
|
|
BasicJSONSerialize('', value, [], builder);
|
2010-12-06 11:44:16 +00:00
|
|
|
if (builder.length == 0) return;
|
|
|
|
var result = %_FastAsciiArrayJoin(builder, "");
|
|
|
|
if (!IS_UNDEFINED(result)) return result;
|
|
|
|
return %StringBuilderConcat(builder, builder.length, "");
|
2010-12-03 11:12:02 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
if (IS_OBJECT(space)) {
|
|
|
|
// Unwrap 'space' if it is wrapped
|
|
|
|
if (IS_NUMBER_WRAPPER(space)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
space = ToNumber(space);
|
2009-04-24 08:13:09 +00:00
|
|
|
} else if (IS_STRING_WRAPPER(space)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
space = ToString(space);
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var gap;
|
|
|
|
if (IS_NUMBER(space)) {
|
2010-12-15 09:31:05 +00:00
|
|
|
space = MathMax(0, MathMin(ToInteger(space), 10));
|
|
|
|
gap = SubString(" ", 0, space);
|
2009-04-24 08:13:09 +00:00
|
|
|
} else if (IS_STRING(space)) {
|
2010-02-03 13:17:39 +00:00
|
|
|
if (space.length > 10) {
|
2010-12-15 09:31:05 +00:00
|
|
|
gap = SubString(space, 0, 10);
|
2010-02-03 13:17:39 +00:00
|
|
|
} else {
|
|
|
|
gap = space;
|
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
} else {
|
|
|
|
gap = "";
|
|
|
|
}
|
2010-12-03 11:12:02 +00:00
|
|
|
return JSONSerialize('', {'': value}, replacer, [], "", gap);
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function SetupJSON() {
|
|
|
|
InstallFunctions($JSON, DONT_ENUM, $Array(
|
|
|
|
"parse", JSONParse,
|
|
|
|
"stringify", JSONStringify
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupJSON();
|