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) {
|
2011-01-28 10:33:10 +00:00
|
|
|
if (%_CallFunction(val, p, ObjectHasOwnProperty)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
var newElement = Revive(val, p, reviver);
|
|
|
|
if (IS_UNDEFINED(newElement)) {
|
|
|
|
delete val[p];
|
|
|
|
} else {
|
|
|
|
val[p] = newElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-03 11:49:03 +00:00
|
|
|
return %_CallFunction(holder, name, val, reviver);
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function JSONParse(text, reviver) {
|
2010-12-22 13:19:25 +00:00
|
|
|
var unfiltered = %ParseJson(TO_STRING_INLINE(text));
|
2011-09-13 11:42:57 +00:00
|
|
|
if (IS_SPEC_FUNCTION(reviver)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
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)) {
|
2011-03-03 11:49:03 +00:00
|
|
|
throw MakeTypeError('circular_structure', $Array());
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
var stepback = indent;
|
|
|
|
indent += gap;
|
2011-03-03 11:49:03 +00:00
|
|
|
var partial = new InternalArray();
|
2009-04-24 08:13:09 +00:00
|
|
|
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)) {
|
2011-03-03 11:49:03 +00:00
|
|
|
throw MakeTypeError('circular_structure', $Array());
|
2010-02-03 13:17:39 +00:00
|
|
|
}
|
2009-04-24 08:13:09 +00:00
|
|
|
var stepback = indent;
|
|
|
|
indent += gap;
|
2011-03-03 11:49:03 +00:00
|
|
|
var partial = new InternalArray();
|
2009-04-24 08:13:09 +00:00
|
|
|
if (IS_ARRAY(replacer)) {
|
|
|
|
var length = replacer.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
2011-01-28 10:33:10 +00:00
|
|
|
if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
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) {
|
2011-01-28 10:33:10 +00:00
|
|
|
if (%_CallFunction(value, p, ObjectHasOwnProperty)) {
|
2009-04-24 08:13:09 +00:00
|
|
|
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;
|
2011-09-13 11:42:57 +00:00
|
|
|
if (IS_SPEC_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
|
|
|
}
|
2011-09-13 11:42:57 +00:00
|
|
|
if (IS_SPEC_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)) {
|
2011-05-30 09:23:17 +00:00
|
|
|
return JSON_NUMBER_TO_STRING(value);
|
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);
|
2011-05-30 09:23:17 +00:00
|
|
|
return JSON_NUMBER_TO_STRING(value);
|
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
|
|
|
|
2009-04-24 08:13:09 +00:00
|
|
|
function JSONStringify(value, replacer, space) {
|
2010-12-15 09:31:05 +00:00
|
|
|
if (%_ArgumentsLength() == 1) {
|
2012-11-02 14:46:57 +00:00
|
|
|
return %BasicJSONStringify(value);
|
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 = "";
|
|
|
|
}
|
2011-03-03 11:49:03 +00:00
|
|
|
return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap);
|
2009-04-24 08:13:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
function SetUpJSON() {
|
|
|
|
%CheckIsBootstrapping();
|
2009-04-24 08:13:09 +00:00
|
|
|
InstallFunctions($JSON, DONT_ENUM, $Array(
|
|
|
|
"parse", JSONParse,
|
|
|
|
"stringify", JSONStringify
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:11:00 +00:00
|
|
|
SetUpJSON();
|