initial commit, kind of working.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.newgrounds.encoders.json
|
||||
{
|
||||
public class JSON
|
||||
{
|
||||
|
||||
public function JSON()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public static function encode(param1:Object) : String
|
||||
{
|
||||
return encodeJson(param1);
|
||||
}
|
||||
|
||||
public static function decode(param1:String) : Object
|
||||
{
|
||||
return decodeJson(param1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
package com.newgrounds.encoders.json
|
||||
{
|
||||
public const decodeJson:Function = initDecodeJson();
|
||||
}
|
||||
|
||||
import com.newgrounds.encoders.json.§com.newgrounds.encoders.json.decodeJson§;
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
function initDecodeJson():Function
|
||||
{
|
||||
var i:int;
|
||||
var parseStringEscaped:Function;
|
||||
var position:uint = 0;
|
||||
var byteInput:ByteArray = null;
|
||||
var char:uint = 0;
|
||||
var charConvert:ByteArray = null;
|
||||
var isNumberChar:ByteArray = null;
|
||||
var stringHelper:ByteArray = null;
|
||||
var isWhitespace:ByteArray = null;
|
||||
var parseNumber:Function = null;
|
||||
var parseWhitespace:Function = null;
|
||||
var parse:Object = null;
|
||||
charConvert = new ByteArray();
|
||||
charConvert.length = 256;
|
||||
charConvert[34] = 34;
|
||||
charConvert[92] = 92;
|
||||
charConvert[47] = 47;
|
||||
charConvert[98] = 8;
|
||||
charConvert[102] = 12;
|
||||
charConvert[110] = 10;
|
||||
charConvert[114] = 13;
|
||||
charConvert[116] = 9;
|
||||
isNumberChar = new ByteArray();
|
||||
isNumberChar.length = 256;
|
||||
isNumberChar[43] = 1;
|
||||
isNumberChar[45] = 1;
|
||||
isNumberChar[46] = 1;
|
||||
isNumberChar[48] = 1;
|
||||
isNumberChar[49] = 1;
|
||||
isNumberChar[50] = 1;
|
||||
isNumberChar[51] = 1;
|
||||
isNumberChar[52] = 1;
|
||||
isNumberChar[53] = 1;
|
||||
isNumberChar[54] = 1;
|
||||
isNumberChar[55] = 1;
|
||||
isNumberChar[56] = 1;
|
||||
isNumberChar[57] = 1;
|
||||
isNumberChar[69] = 1;
|
||||
isNumberChar[101] = 1;
|
||||
stringHelper = new ByteArray();
|
||||
stringHelper.length = 256;
|
||||
i = 0;
|
||||
while(i < 256)
|
||||
{
|
||||
stringHelper[i++] = 1;
|
||||
}
|
||||
stringHelper[34] = 0;
|
||||
stringHelper[92] = 0;
|
||||
isWhitespace = new ByteArray();
|
||||
isWhitespace.length = 256;
|
||||
isWhitespace[9] = 1;
|
||||
isWhitespace[10] = 1;
|
||||
isWhitespace[13] = 1;
|
||||
isWhitespace[32] = 1;
|
||||
parseNumber = function():Number
|
||||
{
|
||||
if(position === 1)
|
||||
{
|
||||
byteInput.position = 0;
|
||||
return parseFloat(byteInput.readUTFBytes(byteInput.length));
|
||||
}
|
||||
byteInput.position = position - 1;
|
||||
while(isNumberChar[byteInput[position++]])
|
||||
{
|
||||
}
|
||||
return Number(byteInput.readUTFBytes(position-- - byteInput.position - 1));
|
||||
};
|
||||
parseWhitespace = function():Object
|
||||
{
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
return parse[byteInput[position++]]();
|
||||
};
|
||||
parseStringEscaped = function(param1:String):String
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if((char = byteInput[position++]) === 117)
|
||||
{
|
||||
byteInput.position = position;
|
||||
char = parseInt(byteInput.readUTFBytes(4),16);
|
||||
position = position + 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!(char = charConvert[char]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
byteInput.position = position;
|
||||
}
|
||||
param1 = param1 + String.fromCharCode(char);
|
||||
while(stringHelper[byteInput[position++]])
|
||||
{
|
||||
}
|
||||
if(position - 1 > byteInput.position)
|
||||
{
|
||||
param1 = param1 + byteInput.readUTFBytes(position - 1 - byteInput.position);
|
||||
}
|
||||
if(byteInput[position - 1] !== 92)
|
||||
{
|
||||
return param1;
|
||||
}
|
||||
}
|
||||
throw new Error("Unknown escaped character encountered at position " + (position - 1));
|
||||
};
|
||||
parse = {
|
||||
34:function():String
|
||||
{
|
||||
if(stringHelper[byteInput[position++]])
|
||||
{
|
||||
byteInput.position = position - 1;
|
||||
while(stringHelper[byteInput[position++]])
|
||||
{
|
||||
}
|
||||
if(byteInput[position - 1] === 92)
|
||||
{
|
||||
return parseStringEscaped(byteInput.readUTFBytes(position - 1 - byteInput.position));
|
||||
}
|
||||
return byteInput.readUTFBytes(position - 1 - byteInput.position);
|
||||
}
|
||||
if(byteInput[position - 1] === 92)
|
||||
{
|
||||
return parseStringEscaped("");
|
||||
}
|
||||
return "";
|
||||
},
|
||||
123:function():Object
|
||||
{
|
||||
var _loc2_:* = undefined;
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
if(byteInput[position] === 125)
|
||||
{
|
||||
position++;
|
||||
return {};
|
||||
}
|
||||
var _loc1_:* = {};
|
||||
loop1:
|
||||
while(true)
|
||||
{
|
||||
do
|
||||
{
|
||||
_loc2_ = parse[byteInput[position++]]();
|
||||
if(byteInput[position] !== 58)
|
||||
{
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
if(byteInput[position++] !== 58)
|
||||
{
|
||||
break loop1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
position++;
|
||||
}
|
||||
_loc1_[_loc2_] = parse[byteInput[position++]]();
|
||||
}
|
||||
while(byteInput[position++] === 44);
|
||||
if(byteInput[position - 1] === 125)
|
||||
{
|
||||
return _loc1_;
|
||||
}
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
if(byteInput[position++] !== 44)
|
||||
{
|
||||
if(byteInput[position - 1] !== 125)
|
||||
{
|
||||
throw new Error("Expected , or } at " + (position - 1));
|
||||
}
|
||||
return _loc1_;
|
||||
}
|
||||
}
|
||||
throw new Error("Expected : at " + (position - 1));
|
||||
},
|
||||
91:function():Object
|
||||
{
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
if(byteInput[position] === 93)
|
||||
{
|
||||
position++;
|
||||
return [];
|
||||
}
|
||||
var _loc1_:* = [];
|
||||
while(true)
|
||||
{
|
||||
do
|
||||
{
|
||||
_loc1_[_loc1_.length] = parse[byteInput[position++]]();
|
||||
}
|
||||
while(byteInput[position++] === 44);
|
||||
if(byteInput[position - 1] === 93)
|
||||
{
|
||||
break;
|
||||
}
|
||||
position--;
|
||||
while(isWhitespace[byteInput[position]])
|
||||
{
|
||||
position++;
|
||||
}
|
||||
if(byteInput[position++] !== 44)
|
||||
{
|
||||
if(byteInput[position - 1] !== 93)
|
||||
{
|
||||
throw new Error("Expected , or ] at " + (position - 1));
|
||||
}
|
||||
return _loc1_;
|
||||
}
|
||||
}
|
||||
return _loc1_;
|
||||
},
|
||||
116:function():Boolean
|
||||
{
|
||||
if(byteInput[position] === 114 && byteInput[position + 1] === 117 && byteInput[position + 2] === 101)
|
||||
{
|
||||
position = position + 3;
|
||||
return true;
|
||||
}
|
||||
throw new Error("Expected \"true\" at position " + position);
|
||||
},
|
||||
102:function():Boolean
|
||||
{
|
||||
if(byteInput[position] === 97 && byteInput[position + 1] === 108 && byteInput[position + 2] === 115 && byteInput[position + 3] === 101)
|
||||
{
|
||||
position = position + 4;
|
||||
return false;
|
||||
}
|
||||
throw new Error("Expected \"false\" at position " + (position - 1));
|
||||
},
|
||||
110:function():Object
|
||||
{
|
||||
if(byteInput[position] === 117 && byteInput[position + 1] === 108 && byteInput[position + 2] === 108)
|
||||
{
|
||||
position = position + 3;
|
||||
return null;
|
||||
}
|
||||
throw new Error("Expected \"null\" at position " + position);
|
||||
},
|
||||
110:function():void
|
||||
{
|
||||
throw new Error("Unexpected end of array at " + position);
|
||||
},
|
||||
125:function():void
|
||||
{
|
||||
throw new Error("Unexpected end of object at " + position);
|
||||
},
|
||||
44:function():void
|
||||
{
|
||||
throw new Error("Unexpected comma at " + position);
|
||||
},
|
||||
45:parseNumber,
|
||||
48:parseNumber,
|
||||
49:parseNumber,
|
||||
50:parseNumber,
|
||||
51:parseNumber,
|
||||
52:parseNumber,
|
||||
53:parseNumber,
|
||||
54:parseNumber,
|
||||
55:parseNumber,
|
||||
56:parseNumber,
|
||||
57:parseNumber,
|
||||
13:parseWhitespace,
|
||||
10:parseWhitespace,
|
||||
9:parseWhitespace,
|
||||
32:parseWhitespace
|
||||
};
|
||||
return function(param1:*):Object
|
||||
{
|
||||
var input:* = param1;
|
||||
if(input is String)
|
||||
{
|
||||
byteInput = new ByteArray();
|
||||
byteInput.writeUTFBytes(input as String);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!(input is ByteArray))
|
||||
{
|
||||
throw new Error("Unexpected input <" + input + ">");
|
||||
}
|
||||
byteInput = input as ByteArray;
|
||||
}
|
||||
position = 0;
|
||||
try
|
||||
{
|
||||
return parse[byteInput[position++]]();
|
||||
}
|
||||
catch(e:TypeError)
|
||||
{
|
||||
if(position - 1 < byteInput.length)
|
||||
{
|
||||
e.message = "Unexpected character " + String.fromCharCode(byteInput[position - 1]) + " (0x" + byteInput[position - 1].toString(16) + ")" + " at position " + (position - 1) + " (" + e.message + ")";
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package com.newgrounds.encoders.json
|
||||
{
|
||||
public const encodeJson:Function = initDecodeJson();
|
||||
}
|
||||
|
||||
import com.newgrounds.encoders.json.§com.newgrounds.encoders.json.encodeJson§;
|
||||
import flash.utils.ByteArray;
|
||||
import flash.utils.IDataOutput;
|
||||
|
||||
function initDecodeJson():Function
|
||||
{
|
||||
var parseArray:Function;
|
||||
var result:IDataOutput = null;
|
||||
var i:int = 0;
|
||||
var j:int = 0;
|
||||
var strLen:int = 0;
|
||||
var str:String = null;
|
||||
var char:int = 0;
|
||||
var tempBytes:ByteArray = null;
|
||||
var blockNonFiniteNumbers:Boolean = false;
|
||||
var charConvert:Array = null;
|
||||
var parseString:Function = null;
|
||||
var parse:Object = null;
|
||||
tempBytes = new ByteArray();
|
||||
charConvert = new Array(256);
|
||||
j = 0;
|
||||
while(j < 10)
|
||||
{
|
||||
charConvert[j] = j + 48 | 0x30303000;
|
||||
j++;
|
||||
}
|
||||
while(j < 16)
|
||||
{
|
||||
charConvert[j] = j + 55 | 0x30303000;
|
||||
j++;
|
||||
}
|
||||
while(j < 26)
|
||||
{
|
||||
charConvert[j] = j + 32 | 0x30303100;
|
||||
j++;
|
||||
}
|
||||
while(j < 32)
|
||||
{
|
||||
charConvert[j] = j + 39 | 0x30303100;
|
||||
j++;
|
||||
}
|
||||
while(j < 256)
|
||||
{
|
||||
charConvert[j] = j;
|
||||
j++;
|
||||
}
|
||||
charConvert[10] = 23662;
|
||||
charConvert[13] = 23666;
|
||||
charConvert[9] = 23668;
|
||||
charConvert[8] = 23650;
|
||||
charConvert[12] = 23654;
|
||||
charConvert[8] = 23650;
|
||||
charConvert[34] = 23586;
|
||||
charConvert[92] = 23644;
|
||||
charConvert[127] = 808466246;
|
||||
parseArray = function(param1:Array):void
|
||||
{
|
||||
result.writeByte(91);
|
||||
var _loc2_:* = 0;
|
||||
var _loc3_:int = param1.length - 1;
|
||||
if(_loc3_ >= 0)
|
||||
{
|
||||
while(_loc2_ < _loc3_)
|
||||
{
|
||||
parse[typeof param1[_loc2_]](param1[_loc2_]);
|
||||
result.writeByte(44);
|
||||
_loc2_++;
|
||||
}
|
||||
parse[typeof param1[_loc2_]](param1[_loc2_]);
|
||||
}
|
||||
result.writeByte(93);
|
||||
};
|
||||
parseString = function(param1:String):void
|
||||
{
|
||||
result.writeByte(34);
|
||||
tempBytes.position = 0;
|
||||
tempBytes.length = 0;
|
||||
tempBytes.writeUTFBytes(param1);
|
||||
i = 0;
|
||||
j = 0;
|
||||
strLen = tempBytes.length;
|
||||
while(j < strLen)
|
||||
{
|
||||
char = charConvert[tempBytes[j++]];
|
||||
if(char > 256)
|
||||
{
|
||||
if(j - 1 > i)
|
||||
{
|
||||
result.writeBytes(tempBytes,i,j - 1 - i);
|
||||
}
|
||||
if(char > 65536)
|
||||
{
|
||||
result.writeShort(23669);
|
||||
result.writeUnsignedInt(char);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.writeShort(char);
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
if(strLen > i)
|
||||
{
|
||||
result.writeBytes(tempBytes,i,strLen - i);
|
||||
}
|
||||
result.writeByte(34);
|
||||
};
|
||||
parse = {
|
||||
"object":function(param1:Object):void
|
||||
{
|
||||
var _loc2_:* = undefined;
|
||||
if(param1)
|
||||
{
|
||||
if(param1 is Array)
|
||||
{
|
||||
parseArray(param1);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.writeByte(123);
|
||||
_loc2_ = true;
|
||||
for(str in param1)
|
||||
{
|
||||
if(_loc2_)
|
||||
{
|
||||
_loc2_ = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.writeByte(44);
|
||||
}
|
||||
parseString(str);
|
||||
result.writeByte(58);
|
||||
parse[typeof param1[str]](param1[str]);
|
||||
}
|
||||
result.writeByte(125);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.writeUnsignedInt(1853189228);
|
||||
}
|
||||
},
|
||||
"string":parseString,
|
||||
"number":function(param1:Number):void
|
||||
{
|
||||
if(blockNonFiniteNumbers && !isFinite(param1))
|
||||
{
|
||||
throw new Error("Number " + param1 + " is not encodable");
|
||||
}
|
||||
result.writeUTFBytes(String(param1));
|
||||
},
|
||||
"boolean":function(param1:Boolean):void
|
||||
{
|
||||
if(param1)
|
||||
{
|
||||
result.writeUnsignedInt(1953658213);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.writeByte(102);
|
||||
result.writeUnsignedInt(1634497381);
|
||||
}
|
||||
},
|
||||
"xml":function(param1:Object):void
|
||||
{
|
||||
if(!param1.toXMLString is Function || (param1 = param1.toXMLString() as String) == null)
|
||||
{
|
||||
throw new Error("unserializable XML object encountered");
|
||||
}
|
||||
parseString(param1);
|
||||
},
|
||||
"undefined":function(param1:Boolean):void
|
||||
{
|
||||
result.writeUnsignedInt(1853189228);
|
||||
}
|
||||
};
|
||||
return function(param1:Object, param2:IDataOutput = null, param3:Boolean = false):String
|
||||
{
|
||||
var byteOutput:* = undefined;
|
||||
var input:Object = param1;
|
||||
var writeTo:IDataOutput = param2;
|
||||
var strictNumberSupport:Boolean = param3;
|
||||
blockNonFiniteNumbers = strictNumberSupport;
|
||||
try
|
||||
{
|
||||
if(writeTo)
|
||||
{
|
||||
result = writeTo;
|
||||
result.endian = "bigEndian";
|
||||
parse[typeof input](input);
|
||||
byteOutput.position = 0;
|
||||
return byteOutput.readUTFBytes(byteOutput.length);
|
||||
}
|
||||
switch(typeof input)
|
||||
{
|
||||
case "xml":
|
||||
if(!input.toXMLString is Function || (input = input.toXMLString() as String) == null)
|
||||
{
|
||||
throw new Error("unserializable XML object encountered");
|
||||
}
|
||||
break;
|
||||
case "object":
|
||||
case "string":
|
||||
break;
|
||||
case "number":
|
||||
if(blockNonFiniteNumbers && !isFinite(input as Number))
|
||||
{
|
||||
throw new Error("Number " + input + " is not encodable");
|
||||
}
|
||||
return String(input);
|
||||
break;
|
||||
case "boolean":
|
||||
return input ? "true" : "false";
|
||||
case "undefined":
|
||||
return "null";
|
||||
default:
|
||||
throw new Error("Unexpected type \"" + typeof input + "\" encountered");
|
||||
}
|
||||
result = byteOutput = new ByteArray();
|
||||
result.endian = "bigEndian";
|
||||
parse[typeof input](input);
|
||||
byteOutput.position = 0;
|
||||
return byteOutput.readUTFBytes(byteOutput.length);
|
||||
}
|
||||
catch(e:TypeError)
|
||||
{
|
||||
throw new Error("Unexpected type encountered");
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user