From c9c176a53b1632ca2b1c6ed27382ab72ac21d45d Mon Sep 17 00:00:00 2001 From: Adam Abrons Date: Tue, 5 Jan 2010 16:36:58 -0800 Subject: angular.js --- src/JSON.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/JSON.js (limited to 'src/JSON.js') diff --git a/src/JSON.js b/src/JSON.js new file mode 100644 index 00000000..2b6393bf --- /dev/null +++ b/src/JSON.js @@ -0,0 +1,92 @@ +nglr.array = [].constructor; + +nglr.toJson = function(obj, pretty){ + var buf = []; + nglr.toJsonArray(buf, obj, pretty ? "\n " : null); + return buf.join(''); +}; + +nglr.toPrettyJson = function(obj) { + return nglr.toJson(obj, true); +}; + +nglr.fromJson = function(json) { + try { + var parser = new nglr.Parser(json, true); + var expression = parser.primary(); + parser.assertAllConsumed(); + return expression(); + } catch (e) { + console.error("fromJson error: ", json, e); + throw e; + } +}; + + +nglr.toJsonArray = function(buf, obj, pretty){ + var type = typeof obj; + if (obj === null) { + buf.push("null"); + } else if (type === 'function') { + return; + } else if (type === 'boolean') { + buf.push('' + obj); + } else if (type === 'number') { + if (isNaN(obj)) { + buf.push('null'); + } else { + buf.push('' + obj); + } + } else if (type === 'string') { + return buf.push(angular.String.quoteUnicode(obj)); + } else if (type === 'object') { + if (obj instanceof Array) { + buf.push("["); + var len = obj.length; + var sep = false; + for(var i=0; i