--- archive/json.js Thu May 26 23:48:52 2005 +++ JSON.as Tue May 31 17:08:51 2005 @@ -19,17 +19,28 @@ SOFTWARE. */ -var JSON = { - org: 'http://www.JSON.org', - copyright: '(c)2005 JSON.org', - license: 'http://www.crockford.com/JSON/license.html', - stringify: function stringify(arg) { +/* +ported to Actionscript May 2005 by Trannie Carter , wwww.designvox.com +USAGE: + try { + var o:Object = JSON.parse(jsonStr); + var s:String = JSON.stringify(obj); + } catch(ex) { + trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text); + } + +*/ + +class JSON { + + static function stringify(arg):String { + var c, i, l, s = '', v; switch (typeof arg) { case 'object': if (arg) { - if (arg.constructor == Array) { + if (arg instanceof Array) { for (i = 0; i < arg.length; ++i) { v = stringify(arg[i]); if (s) { @@ -95,8 +106,9 @@ default: return 'null'; } - }, - parse: function (text) { + } + + static function parse(text:String):Object { var at = 0; var ch = ' '; @@ -118,23 +130,23 @@ function white() { while (ch) { if (ch <= ' ') { - next(); + this.next(); } else if (ch == '/') { - switch (next()) { + switch (this.next()) { case '/': - while (next() && ch != '\n' && ch != '\r') {} + while (this.next() && ch != '\n' && ch != '\r') {} break; case '*': - next(); + this.next(); for (;;) { if (ch) { if (ch == '*') { - if (next() == '/') { + if (this.next() == '/') { next(); break; } } else { - next(); + this.next(); } } else { error("Unterminated comment"); @@ -142,7 +154,7 @@ } break; default: - error("Syntax error"); + this.error("Syntax error"); } } else { break; @@ -152,14 +164,15 @@ function string() { var i, s = '', t, u; + var outer:Boolean = false; if (ch == '"') { -outer: while (next()) { + while (this.next()) { if (ch == '"') { - next(); + this.next(); return s; } else if (ch == '\\') { - switch (next()) { + switch (this.next()) { case 'b': s += '\b'; break; @@ -178,12 +191,17 @@ case 'u': u = 0; for (i = 0; i < 4; i += 1) { - t = parseInt(next(), 16); + t = parseInt(this.next(), 16); if (!isFinite(t)) { - break outer; + outer = true; + break; } u = u * 16 + t; } + if(outer) { + outer = false; + break; + } s += String.fromCharCode(u); break; default: @@ -194,65 +212,65 @@ } } } - error("Bad string"); + this.error("Bad string"); } function array() { var a = []; if (ch == '[') { - next(); - white(); + this.next(); + this.white(); if (ch == ']') { - next(); + this.next(); return a; } while (ch) { - a.push(value()); - white(); + a.push(this.value()); + this.white(); if (ch == ']') { - next(); + this.next(); return a; } else if (ch != ',') { break; } - next(); - white(); + this.next(); + this.white(); } } - error("Bad array"); + this.error("Bad array"); } function object() { var k, o = {}; if (ch == '{') { - next(); - white(); + this.next(); + this.white(); if (ch == '}') { - next(); + this.next(); return o; } while (ch) { - k = string(); - white(); + k = this.string(); + this.white(); if (ch != ':') { break; } - next(); - o[k] = value(); - white(); + this.next(); + o[k] = this.value(); + this.white(); if (ch == '}') { - next(); + this.next(); return o; } else if (ch != ',') { break; } - next(); - white(); + this.next(); + this.white(); } } - error("Bad object"); + this.error("Bad object"); } function number() { @@ -260,21 +278,21 @@ if (ch == '-') { n = '-'; - next(); + this.next(); } while (ch >= '0' && ch <= '9') { n += ch; - next(); + this.next(); } if (ch == '.') { n += '.'; - while (next() && ch >= '0' && ch <= '9') { + while (this.next() && ch >= '0' && ch <= '9') { n += ch; } } v = +n; if (!isFinite(v)) { - error("Bad number"); + this.error("Bad number"); } else { return v; } @@ -283,44 +301,44 @@ function word() { switch (ch) { case 't': - if (next() == 'r' && next() == 'u' && next() == 'e') { - next(); + if (this.next() == 'r' && this.next() == 'u' && this.next() == 'e') { + this.next(); return true; } break; case 'f': - if (next() == 'a' && next() == 'l' && next() == 's' && - next() == 'e') { - next(); + if (this.next() == 'a' && this.next() == 'l' && this.next() == 's' && + this.next() == 'e') { + this.next(); return false; } break; case 'n': - if (next() == 'u' && next() == 'l' && next() == 'l') { - next(); + if (this.next() == 'u' && this.next() == 'l' && this.next() == 'l') { + this.next(); return null; } break; } - error("Syntax error"); + this.error("Syntax error"); } function value() { - white(); + this.white(); switch (ch) { case '{': - return object(); + return this.object(); case '[': - return array(); + return this.array(); case '"': - return string(); + return this.string(); case '-': - return number(); + return this.number(); default: - return ch >= '0' && ch <= '9' ? number() : word(); + return ch >= '0' && ch <= '9' ? this.number() : this.word(); } } return value(); } -}; +}