angular.js/src/JSON.js

99 lines
2.4 KiB
JavaScript
Raw Normal View History

2010-08-11 19:04:02 +00:00
var array = [].constructor;
2010-01-06 00:36:58 +00:00
2010-01-12 01:32:33 +00:00
function toJson(obj, pretty){
2010-01-06 00:36:58 +00:00
var buf = [];
toJsonArray(buf, obj, pretty ? "\n " : _null, []);
2010-01-06 00:36:58 +00:00
return buf.join('');
2010-04-04 00:04:36 +00:00
}
2010-01-06 00:36:58 +00:00
2010-01-12 01:32:33 +00:00
function fromJson(json) {
2010-03-31 20:57:25 +00:00
if (!json) return json;
2010-01-06 00:36:58 +00:00
try {
2010-01-09 23:02:43 +00:00
var parser = new Parser(json, true);
2010-01-06 00:36:58 +00:00
var expression = parser.primary();
parser.assertAllConsumed();
return expression();
} catch (e) {
2010-01-12 00:15:12 +00:00
error("fromJson error: ", json, e);
2010-01-06 00:36:58 +00:00
throw e;
}
2010-04-04 00:04:36 +00:00
}
2010-01-06 00:36:58 +00:00
2010-01-12 01:32:33 +00:00
angular['toJson'] = toJson;
angular['fromJson'] = fromJson;
2010-01-06 00:36:58 +00:00
2010-01-23 23:54:58 +00:00
function toJsonArray(buf, obj, pretty, stack){
if (typeof obj == "object") {
if (includes(stack, obj)) {
2010-01-23 23:54:58 +00:00
buf.push("RECURSION");
return;
}
stack.push(obj);
}
2010-01-06 00:36:58 +00:00
var type = typeof obj;
if (obj === _null) {
buf.push($null);
} else if (type === $function) {
2010-01-06 00:36:58 +00:00
return;
} else if (type === $boolean) {
2010-01-06 00:36:58 +00:00
buf.push('' + obj);
} else if (type === $number) {
2010-01-06 00:36:58 +00:00
if (isNaN(obj)) {
buf.push($null);
2010-01-06 00:36:58 +00:00
} else {
buf.push('' + obj);
}
} else if (type === $string) {
return buf.push(angular['String']['quoteUnicode'](obj));
} else if (type === $object) {
2010-01-06 00:36:58 +00:00
if (obj instanceof Array) {
buf.push("[");
var len = obj.length;
var sep = false;
for(var i=0; i<len; i++) {
var item = obj[i];
if (sep) buf.push(",");
if (typeof item == $function || typeof item == $undefined) {
buf.push($null);
2010-01-06 00:36:58 +00:00
} else {
2010-01-23 23:54:58 +00:00
toJsonArray(buf, item, pretty, stack);
2010-01-06 00:36:58 +00:00
}
sep = true;
}
buf.push("]");
} else if (obj instanceof Date) {
buf.push(angular['String']['quoteUnicode'](angular['Date']['toString'](obj)));
2010-01-06 00:36:58 +00:00
} else {
buf.push("{");
if (pretty) buf.push(pretty);
var comma = false;
var childPretty = pretty ? pretty + " " : false;
var keys = [];
for(var k in obj) {
if (k.indexOf('$') === 0 || obj[k] === _undefined)
2010-01-06 00:36:58 +00:00
continue;
keys.push(k);
}
keys.sort();
for ( var keyIndex = 0; keyIndex < keys.length; keyIndex++) {
var key = keys[keyIndex];
var value = obj[key];
2010-09-14 21:42:43 +00:00
if (typeof value != $function) {
if (comma) {
buf.push(",");
if (pretty) buf.push(pretty);
2010-01-06 00:36:58 +00:00
}
buf.push(angular['String']['quote'](key));
buf.push(":");
toJsonArray(buf, value, childPretty, stack);
comma = true;
2010-01-06 00:36:58 +00:00
}
}
buf.push("}");
}
}
if (typeof obj == $object) {
2010-01-23 23:54:58 +00:00
stack.pop();
}
2010-04-04 00:04:36 +00:00
}