forked from Neural-Systems-at-UIO/QuickNII
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSONp.as
170 lines (160 loc) · 3.82 KB
/
JSONp.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package {
public class JSONp {
public static function parse(json:String):Object {
return new JSONp(json).parse();
}
private var json:String;
private var pos:int=0;
public function JSONp(json:String) {
this.json=json;
}
private function parse():Object {
skipWhiteSpace();
switch(json.charAt(pos)) {
case "n":
expect("null");
return null;
case "t":
expect("true");
return true;
case "f":
expect("false");
return false;
case "\"":
return parseString();
case "[":
return parseArray();
case "{":
return parseObject();
default:
return parseNumber();
}
return null;
}
private function parseObject():Object {
var o:Object={};
pos++;
skipWhiteSpace();
if(json.charAt(pos)=="}") {
pos++;
return o;
}
var c:String;
do {
skipWhiteSpace();
c=json.charAt(pos);
if(c!="\"")throw new Error("Unquoted field name? "+c);
var key:String=parseString();
skipWhiteSpace();
c=read();
if(c!=":")throw new Error("Key-value separator expected after "+key+", got "+c);
o[key]=parse();
skipWhiteSpace();
c=read();
if(c!="}" && c!=",")throw new Error("Illegal field separator after "+key+": "+c);
}
while(c!="}");
return o;
}
private function parseArray():Array {
var a:Array=[];
pos++;
skipWhiteSpace();
if(json.charAt(pos)=="]") {
pos++;
return a;
}
var c:String;
do {
a.push(parse());
skipWhiteSpace();
c=read();
if(c!="]" && c!=",")throw new Error("Illegal array separator: "+c);
}
while(c!="]");
return a;
}
private function parseNumber():Number {
var org:int=pos;
while(pos<json.length && "+-.eE1234567890".indexOf(json.charAt(pos))>=0)pos++;
if(org==pos)throw new Error("Illegal token at "+pos);
return parseFloat(json.substring(org,pos));
}
private function parseString():String {
var s:String="";
var c:String;
pos++;
while((c=read())!="\"") {
if(c!="\\")s+=c;
else {
c=read();
switch(c) {
case "\"":
case "\\":
case "/":
s+=c;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;
case "u":s+=String.fromCharCode(parseInt(read()+read()+read()+read(),16));break;
default: throw new Error("Illegal escape sequence: \\"+c);
}
}
}
return s;
}
private function expect(s:String):void {
for(var i:int=0;i<s.length;i++)
if(read()!=s.charAt(i))
throw new Error("Unexpected literal "+json.substr(pos-i-1,i+1)+" (expected "+s+")");
}
private function read():String {
if(pos>=json.length)throw new Error("Unexpected end of JSON");
return json.charAt(pos++);
}
private function skipWhiteSpace():void {
while(pos<json.length && " \t\r\n".indexOf(json.charAt(pos))>=0)pos++;
if(pos>=json.length)throw new Error("Unexpected end of JSON");
}
public static function stringify(o:Object):String {
var s:String="";
var i:int;
if(o==null)s="null";
else if(o is Boolean || o is Number)s=o.toString();
else if(o is String) {
s+="\"";
var t:String=o as String;
for(i=0;i<t.length;i++) {
var c:String=t.charAt(i);
var pos:int="\"\\\b\f\n\r\t".indexOf(c);
if(pos>=0)s+="\\"+("\"\\bfnrt").charAt(pos);
else if(c>=" " && c<="~")s+=c;
else s+="\\u"+(0x10000+t.charCodeAt(i)).toString(16).substr(1);
}
s+="\"";
} else if(o is Array) {
var a:Array=o as Array;
s+="[";
for(i=0;i<a.length;i++) {
if(i>0)s+=",";
s+=stringify(a[i]);
}
s+="]";
} else {
s+="{";
var comma:Boolean=false;
for(var p:String in o) {
if(comma)s+=",";
comma=true;
s+=stringify(p);
s+=":";
s+=stringify(o[p]);
}
s+="}";
}
return s;
}
}
}