mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-20 09:37:50 -05:00
Brought up to date with the private repository (feea4c7)
This commit is contained in:
1 parent
1aa738d692
commit
1b1df1ae48
7925 files changed
+2617237
-20
No files matched your search
@@ -0,0 +1,24 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSON
|
||||
{
|
||||
|
||||
public function JSON()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public static function encode(o:Object) : String
|
||||
{
|
||||
var encoder:JSONEncoder = new JSONEncoder(o);
|
||||
return encoder.getString();
|
||||
}
|
||||
|
||||
public static function decode(s:String) : *
|
||||
{
|
||||
var decoder:JSONDecoder = new JSONDecoder(s);
|
||||
return decoder.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSONDecoder
|
||||
{
|
||||
|
||||
private var value:*;
|
||||
|
||||
private var tokenizer:JSONTokenizer;
|
||||
|
||||
private var token:JSONToken;
|
||||
|
||||
public function JSONDecoder(s:String)
|
||||
{
|
||||
super();
|
||||
this.tokenizer = new JSONTokenizer(s);
|
||||
this.nextToken();
|
||||
this.value = this.parseValue();
|
||||
}
|
||||
|
||||
public function getValue() : *
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private function nextToken() : JSONToken
|
||||
{
|
||||
return this.token = this.tokenizer.getNextToken();
|
||||
}
|
||||
|
||||
private function parseArray() : Array
|
||||
{
|
||||
var a:Array = new Array();
|
||||
this.nextToken();
|
||||
if(this.token.type == JSONTokenType.RIGHT_BRACKET)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
while(true)
|
||||
{
|
||||
a.push(this.parseValue());
|
||||
this.nextToken();
|
||||
if(this.token.type == JSONTokenType.RIGHT_BRACKET)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
if(this.token.type == JSONTokenType.COMMA)
|
||||
{
|
||||
this.nextToken();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.tokenizer.parseError("Expecting ] or , but found " + this.token.value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function parseObject() : Object
|
||||
{
|
||||
var key:String = null;
|
||||
var o:Object = new Object();
|
||||
this.nextToken();
|
||||
if(this.token.type == JSONTokenType.RIGHT_BRACE)
|
||||
{
|
||||
return o;
|
||||
}
|
||||
while(true)
|
||||
{
|
||||
if(this.token.type == JSONTokenType.STRING)
|
||||
{
|
||||
key = String(this.token.value);
|
||||
this.nextToken();
|
||||
if(this.token.type == JSONTokenType.COLON)
|
||||
{
|
||||
this.nextToken();
|
||||
o[key] = this.parseValue();
|
||||
this.nextToken();
|
||||
if(this.token.type == JSONTokenType.RIGHT_BRACE)
|
||||
{
|
||||
return o;
|
||||
}
|
||||
if(this.token.type == JSONTokenType.COMMA)
|
||||
{
|
||||
this.nextToken();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.tokenizer.parseError("Expecting } or , but found " + this.token.value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.tokenizer.parseError("Expecting : but found " + this.token.value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.tokenizer.parseError("Expecting string but found " + this.token.value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function parseValue() : Object
|
||||
{
|
||||
switch(this.token.type)
|
||||
{
|
||||
case JSONTokenType.LEFT_BRACE:
|
||||
return this.parseObject();
|
||||
case JSONTokenType.LEFT_BRACKET:
|
||||
return this.parseArray();
|
||||
case JSONTokenType.STRING:
|
||||
case JSONTokenType.NUMBER:
|
||||
case JSONTokenType.TRUE:
|
||||
case JSONTokenType.FALSE:
|
||||
case JSONTokenType.NULL:
|
||||
return this.token.value;
|
||||
default:
|
||||
this.tokenizer.parseError("Unexpected " + this.token.value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
import flash.utils.describeType;
|
||||
|
||||
public class JSONEncoder
|
||||
{
|
||||
|
||||
private var jsonString:String;
|
||||
|
||||
public function JSONEncoder(value:*)
|
||||
{
|
||||
super();
|
||||
this.jsonString = this.convertToString(value);
|
||||
}
|
||||
|
||||
public function getString() : String
|
||||
{
|
||||
return this.jsonString;
|
||||
}
|
||||
|
||||
private function convertToString(value:*) : String
|
||||
{
|
||||
if(value is String)
|
||||
{
|
||||
return this.escapeString(value as String);
|
||||
}
|
||||
if(value is Number)
|
||||
{
|
||||
return isFinite(value as Number) ? value.toString() : "null";
|
||||
}
|
||||
if(value is Boolean)
|
||||
{
|
||||
return Boolean(value) ? "true" : "false";
|
||||
}
|
||||
if(value is Array)
|
||||
{
|
||||
return this.arrayToString(value as Array);
|
||||
}
|
||||
if(value is Object && value != null)
|
||||
{
|
||||
return this.objectToString(value);
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
|
||||
private function escapeString(str:String) : String
|
||||
{
|
||||
var ch:String = null;
|
||||
var hexCode:String = null;
|
||||
var zeroPad:String = null;
|
||||
var s:String = "";
|
||||
var len:Number = str.length;
|
||||
for(var i:int = 0; i < len; i++)
|
||||
{
|
||||
ch = str.charAt(i);
|
||||
switch(ch)
|
||||
{
|
||||
case "\"":
|
||||
s += "\\\"";
|
||||
break;
|
||||
case "\\":
|
||||
s += "\\\\";
|
||||
break;
|
||||
case "\b":
|
||||
s += "\\b";
|
||||
break;
|
||||
case "\f":
|
||||
s += "\\f";
|
||||
break;
|
||||
case "\n":
|
||||
s += "\\n";
|
||||
break;
|
||||
case "\r":
|
||||
s += "\\r";
|
||||
break;
|
||||
case "\t":
|
||||
s += "\\t";
|
||||
break;
|
||||
default:
|
||||
if(ch < " ")
|
||||
{
|
||||
hexCode = ch.charCodeAt(0).toString(16);
|
||||
zeroPad = hexCode.length == 2 ? "00" : "000";
|
||||
s += "\\u" + zeroPad + hexCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
s += ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "\"" + s + "\"";
|
||||
}
|
||||
|
||||
private function arrayToString(a:Array) : String
|
||||
{
|
||||
var s:String = "";
|
||||
for(var i:int = 0; i < a.length; i++)
|
||||
{
|
||||
if(s.length > 0)
|
||||
{
|
||||
s += ",";
|
||||
}
|
||||
s += this.convertToString(a[i]);
|
||||
}
|
||||
return "[" + s + "]";
|
||||
}
|
||||
|
||||
private function objectToString(o:Object) : String
|
||||
{
|
||||
var value:Object = null;
|
||||
var key:String = null;
|
||||
var v:XML = null;
|
||||
var s:String = "";
|
||||
var classInfo:XML = describeType(o);
|
||||
if(classInfo.@name.toString() == "Object")
|
||||
{
|
||||
for(key in o)
|
||||
{
|
||||
value = o[key];
|
||||
if(!(value is Function))
|
||||
{
|
||||
if(s.length > 0)
|
||||
{
|
||||
s += ",";
|
||||
}
|
||||
s += this.escapeString(key) + ":" + this.convertToString(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for each(v in classInfo..*.(name() == "variable" || name() == "accessor"))
|
||||
{
|
||||
if(s.length > 0)
|
||||
{
|
||||
s += ",";
|
||||
}
|
||||
s += this.escapeString(v.@name.toString()) + ":" + this.convertToString(o[v.@name]);
|
||||
}
|
||||
}
|
||||
return "{" + s + "}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSONParseError extends Error
|
||||
{
|
||||
|
||||
private var _location:int;
|
||||
|
||||
private var _text:String;
|
||||
|
||||
public function JSONParseError(message:String = "", location:int = 0, text:String = "")
|
||||
{
|
||||
super(message);
|
||||
this._location = location;
|
||||
this._text = text;
|
||||
}
|
||||
|
||||
public function get location() : int
|
||||
{
|
||||
return this._location;
|
||||
}
|
||||
|
||||
public function get text() : String
|
||||
{
|
||||
return this._text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSONToken
|
||||
{
|
||||
|
||||
private var _type:int;
|
||||
|
||||
private var _value:Object;
|
||||
|
||||
public function JSONToken(type:int = -1, value:Object = null)
|
||||
{
|
||||
super();
|
||||
this._type = type;
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
public function get type() : int
|
||||
{
|
||||
return this._type;
|
||||
}
|
||||
|
||||
public function set type(value:int) : void
|
||||
{
|
||||
this._type = value;
|
||||
}
|
||||
|
||||
public function get value() : Object
|
||||
{
|
||||
return this._value;
|
||||
}
|
||||
|
||||
public function set value(v:Object) : void
|
||||
{
|
||||
this._value = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSONTokenType
|
||||
{
|
||||
|
||||
public static const UNKNOWN:int = -1;
|
||||
|
||||
public static const COMMA:int = 0;
|
||||
|
||||
public static const LEFT_BRACE:int = 1;
|
||||
|
||||
public static const RIGHT_BRACE:int = 2;
|
||||
|
||||
public static const LEFT_BRACKET:int = 3;
|
||||
|
||||
public static const RIGHT_BRACKET:int = 4;
|
||||
|
||||
public static const COLON:int = 6;
|
||||
|
||||
public static const TRUE:int = 7;
|
||||
|
||||
public static const FALSE:int = 8;
|
||||
|
||||
public static const NULL:int = 9;
|
||||
|
||||
public static const STRING:int = 10;
|
||||
|
||||
public static const NUMBER:int = 11;
|
||||
|
||||
public function JSONTokenType()
|
||||
{
|
||||
super();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,350 @@
|
||||
package com.adobe.serialization.json
|
||||
{
|
||||
public class JSONTokenizer
|
||||
{
|
||||
|
||||
private var obj:Object;
|
||||
|
||||
private var jsonString:String;
|
||||
|
||||
private var loc:int;
|
||||
|
||||
private var ch:String;
|
||||
|
||||
public function JSONTokenizer(s:String)
|
||||
{
|
||||
super();
|
||||
this.jsonString = s;
|
||||
this.loc = 0;
|
||||
this.nextChar();
|
||||
}
|
||||
|
||||
public function getNextToken() : JSONToken
|
||||
{
|
||||
var possibleTrue:String = null;
|
||||
var possibleFalse:String = null;
|
||||
var possibleNull:String = null;
|
||||
var token:JSONToken = new JSONToken();
|
||||
this.skipIgnored();
|
||||
switch(this.ch)
|
||||
{
|
||||
case "{":
|
||||
token.type = JSONTokenType.LEFT_BRACE;
|
||||
token.value = "{";
|
||||
this.nextChar();
|
||||
break;
|
||||
case "}":
|
||||
token.type = JSONTokenType.RIGHT_BRACE;
|
||||
token.value = "}";
|
||||
this.nextChar();
|
||||
break;
|
||||
case "[":
|
||||
token.type = JSONTokenType.LEFT_BRACKET;
|
||||
token.value = "[";
|
||||
this.nextChar();
|
||||
break;
|
||||
case "]":
|
||||
token.type = JSONTokenType.RIGHT_BRACKET;
|
||||
token.value = "]";
|
||||
this.nextChar();
|
||||
break;
|
||||
case ",":
|
||||
token.type = JSONTokenType.COMMA;
|
||||
token.value = ",";
|
||||
this.nextChar();
|
||||
break;
|
||||
case ":":
|
||||
token.type = JSONTokenType.COLON;
|
||||
token.value = ":";
|
||||
this.nextChar();
|
||||
break;
|
||||
case "t":
|
||||
possibleTrue = "t" + this.nextChar() + this.nextChar() + this.nextChar();
|
||||
if(possibleTrue == "true")
|
||||
{
|
||||
token.type = JSONTokenType.TRUE;
|
||||
token.value = true;
|
||||
this.nextChar();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.parseError("Expecting \'true\' but found " + possibleTrue);
|
||||
}
|
||||
break;
|
||||
case "f":
|
||||
possibleFalse = "f" + this.nextChar() + this.nextChar() + this.nextChar() + this.nextChar();
|
||||
if(possibleFalse == "false")
|
||||
{
|
||||
token.type = JSONTokenType.FALSE;
|
||||
token.value = false;
|
||||
this.nextChar();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.parseError("Expecting \'false\' but found " + possibleFalse);
|
||||
}
|
||||
break;
|
||||
case "n":
|
||||
possibleNull = "n" + this.nextChar() + this.nextChar() + this.nextChar();
|
||||
if(possibleNull == "null")
|
||||
{
|
||||
token.type = JSONTokenType.NULL;
|
||||
token.value = null;
|
||||
this.nextChar();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.parseError("Expecting \'null\' but found " + possibleNull);
|
||||
}
|
||||
break;
|
||||
case "\"":
|
||||
token = this.readString();
|
||||
break;
|
||||
default:
|
||||
if(this.isDigit(this.ch) || this.ch == "-")
|
||||
{
|
||||
token = this.readNumber();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this.ch == "")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
this.parseError("Unexpected " + this.ch + " encountered");
|
||||
}
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
private function readString() : JSONToken
|
||||
{
|
||||
var hexValue:String = null;
|
||||
var i:int = 0;
|
||||
var token:JSONToken = new JSONToken();
|
||||
token.type = JSONTokenType.STRING;
|
||||
var string:String = "";
|
||||
this.nextChar();
|
||||
while(this.ch != "\"" && this.ch != "")
|
||||
{
|
||||
if(this.ch == "\\")
|
||||
{
|
||||
this.nextChar();
|
||||
switch(this.ch)
|
||||
{
|
||||
case "\"":
|
||||
string += "\"";
|
||||
break;
|
||||
case "/":
|
||||
string += "/";
|
||||
break;
|
||||
case "\\":
|
||||
string += "\\";
|
||||
break;
|
||||
case "b":
|
||||
string += "\b";
|
||||
break;
|
||||
case "f":
|
||||
string += "\f";
|
||||
break;
|
||||
case "n":
|
||||
string += "\n";
|
||||
break;
|
||||
case "r":
|
||||
string += "\r";
|
||||
break;
|
||||
case "t":
|
||||
string += "\t";
|
||||
break;
|
||||
case "u":
|
||||
hexValue = "";
|
||||
for(i = 0; i < 4; i++)
|
||||
{
|
||||
if(!this.isHexDigit(this.nextChar()))
|
||||
{
|
||||
this.parseError(" Excepted a hex digit, but found: " + this.ch);
|
||||
}
|
||||
hexValue += this.ch;
|
||||
}
|
||||
string += String.fromCharCode(parseInt(hexValue,16));
|
||||
break;
|
||||
default:
|
||||
string += "\\" + this.ch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string += this.ch;
|
||||
}
|
||||
this.nextChar();
|
||||
}
|
||||
if(this.ch == "")
|
||||
{
|
||||
this.parseError("Unterminated string literal");
|
||||
}
|
||||
this.nextChar();
|
||||
token.value = string;
|
||||
return token;
|
||||
}
|
||||
|
||||
private function readNumber() : JSONToken
|
||||
{
|
||||
var token:JSONToken = new JSONToken();
|
||||
token.type = JSONTokenType.NUMBER;
|
||||
var input:String = "";
|
||||
if(this.ch == "-")
|
||||
{
|
||||
input += "-";
|
||||
this.nextChar();
|
||||
}
|
||||
if(!this.isDigit(this.ch))
|
||||
{
|
||||
this.parseError("Expecting a digit");
|
||||
}
|
||||
if(this.ch == "0")
|
||||
{
|
||||
input += this.ch;
|
||||
this.nextChar();
|
||||
if(this.isDigit(this.ch))
|
||||
{
|
||||
this.parseError("A digit cannot immediately follow 0");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(this.isDigit(this.ch))
|
||||
{
|
||||
input += this.ch;
|
||||
this.nextChar();
|
||||
}
|
||||
}
|
||||
if(this.ch == ".")
|
||||
{
|
||||
input += ".";
|
||||
this.nextChar();
|
||||
if(!this.isDigit(this.ch))
|
||||
{
|
||||
this.parseError("Expecting a digit");
|
||||
}
|
||||
while(this.isDigit(this.ch))
|
||||
{
|
||||
input += this.ch;
|
||||
this.nextChar();
|
||||
}
|
||||
}
|
||||
if(this.ch == "e" || this.ch == "E")
|
||||
{
|
||||
input += "e";
|
||||
this.nextChar();
|
||||
if(this.ch == "+" || this.ch == "-")
|
||||
{
|
||||
input += this.ch;
|
||||
this.nextChar();
|
||||
}
|
||||
if(!this.isDigit(this.ch))
|
||||
{
|
||||
this.parseError("Scientific notation number needs exponent value");
|
||||
}
|
||||
while(this.isDigit(this.ch))
|
||||
{
|
||||
input += this.ch;
|
||||
this.nextChar();
|
||||
}
|
||||
}
|
||||
var num:Number = Number(input);
|
||||
if(isFinite(num) && !isNaN(num))
|
||||
{
|
||||
token.value = num;
|
||||
return token;
|
||||
}
|
||||
this.parseError("Number " + num + " is not valid!");
|
||||
return null;
|
||||
}
|
||||
|
||||
private function nextChar() : String
|
||||
{
|
||||
return this.ch = this.jsonString.charAt(this.loc++);
|
||||
}
|
||||
|
||||
private function skipIgnored() : void
|
||||
{
|
||||
this.skipWhite();
|
||||
this.skipComments();
|
||||
this.skipWhite();
|
||||
}
|
||||
|
||||
private function skipComments() : void
|
||||
{
|
||||
if(this.ch == "/")
|
||||
{
|
||||
this.nextChar();
|
||||
switch(this.ch)
|
||||
{
|
||||
case "/":
|
||||
do
|
||||
{
|
||||
this.nextChar();
|
||||
}
|
||||
while(this.ch != "\n" && this.ch != "");
|
||||
this.nextChar();
|
||||
break;
|
||||
case "*":
|
||||
this.nextChar();
|
||||
while(true)
|
||||
{
|
||||
if(this.ch == "*")
|
||||
{
|
||||
this.nextChar();
|
||||
if(this.ch == "/")
|
||||
{
|
||||
this.nextChar();
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.nextChar();
|
||||
}
|
||||
if(this.ch == "")
|
||||
{
|
||||
this.parseError("Multi-line comment not closed");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
this.parseError("Unexpected " + this.ch + " encountered (expecting \'/\' or \'*\' )");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function skipWhite() : void
|
||||
{
|
||||
while(this.isWhiteSpace(this.ch))
|
||||
{
|
||||
this.nextChar();
|
||||
}
|
||||
}
|
||||
|
||||
private function isWhiteSpace(ch:String) : Boolean
|
||||
{
|
||||
return ch == " " || ch == "\t" || ch == "\n";
|
||||
}
|
||||
|
||||
private function isDigit(ch:String) : Boolean
|
||||
{
|
||||
return ch >= "0" && ch <= "9";
|
||||
}
|
||||
|
||||
private function isHexDigit(ch:String) : Boolean
|
||||
{
|
||||
var uc:String = ch.toUpperCase();
|
||||
return this.isDigit(ch) || uc >= "A" && uc <= "F";
|
||||
}
|
||||
|
||||
public function parseError(message:String) : void
|
||||
{
|
||||
throw new JSONParseError(message,this.loc,this.jsonString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user