-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonlite.ts
223 lines (194 loc) · 6.52 KB
/
jsonlite.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module jsonlite {
// modified from https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
// by deerchao. Use it however you want to.
//
// Jsonlite is a variant of json, aims for human readability and writability.
//
// Object key, string value, number vaue, true, false, null in json can all be without quotes,
// as long as they don't contain special characters:
// spaces( , \t, \n...), double quote("), comma(,), square brackets([,]),
// object start and end ('{', '}' or '(', ')', depending on options),
// pair seperators (':' or '=' depending on options).
// You can set jsonObjectFormat to false to use "(=)" instead of "{:}" for objects.
//
// Example 1:
// {max-length : 50}
//
// Example 2 (with jsonObjectFormat = false):
// (name=jsonlite, birthday=(year=2013,month=7,date=7), isGreat=true)
export function parse(source: string, jsonObjectFormat?: bool = true) {
var object_start = jsonObjectFormat ? '{' : '(';
var object_end = jsonObjectFormat ? '}' : ')';
var pair_seperator = jsonObjectFormat ? ':' : '=';
var at = 0;
var ch = ' ';
var escapee = {
'"': '"',
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t'
};
var text = source;
var result = readValue();
skipWhitespace();
if (ch) {
raiseError("Syntax error");
}
return result;
function raiseError(m: string) {
throw {
name: 'SyntaxError',
message: m,
at: at,
text: text
};
}
function next(c?: string) {
if (c && c !== ch) {
raiseError("Expected '" + c + "' instead of '" + ch + "'");
}
ch = text.charAt(at);
at += 1;
return ch;
}
function readString() {
var s = '';
if (ch === '"') {
while (next()) {
if (ch === '"') {
next();
return s;
}
if (ch === '\\') {
next();
if (ch === 'u') {
var uffff = 0;
for (var i = 0; i < 4; i += 1) {
var hex = parseInt(next(), 16);
if (!isFinite(hex)) {
break;
}
uffff = uffff * 16 + hex;
}
s += String.fromCharCode(uffff);
} else if (typeof escapee[ch] === 'string') {
s += escapee[ch];
} else {
break;
}
} else {
s += ch;
}
}
}
raiseError("Bad string");
}
function skipWhitespace() {
while (ch && ch <= ' ') {
next();
}
}
function readWord(): any {
var s = '';
while (allowedInWord()) {
s += ch;
next();
}
if (s === "true")
return true;
if (s === "false")
return false;
if (s === "null")
return null;
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s))
return parseFloat(s);
return s;
}
function readArray() {
var array = [];
if (ch === '[') {
next('[');
skipWhitespace();
if (ch === ']') {
next(']');
return array;
}
while (ch) {
array.push(readValue());
skipWhitespace();
if (ch === ']') {
next(']');
return array;
}
next(',');
skipWhitespace();
}
}
raiseError("Bad array");
}
function readObject() {
var o = {};
if (ch === object_start) {
next(object_start);
skipWhitespace();
if (ch === object_end) {
next(object_end);
return o;
}
while (ch) {
var key = ch === '"' ? readString() : readWord();
if (typeof key !== 'string')
raiseError('Bad object key: ' + key);
skipWhitespace();
next(pair_seperator);
if (Object.hasOwnProperty.call(o, key)) {
raiseError('Duplicate key: "' + key + '"');
}
o[key] = readValue();
skipWhitespace();
if (ch === object_end) {
next(object_end);
return o;
}
next(',');
skipWhitespace();
}
}
raiseError("Bad object");
}
function readValue() {
skipWhitespace();
switch (ch) {
case object_start:
return readObject();
case '[':
return readArray();
case '"':
return readString();
default:
return readWord();
}
}
function allowedInWord() {
switch (ch) {
case '"':
case '\\':
case '\t':
case '\n':
case '\r':
case ',':
case '[':
case ']':
case object_start:
case object_end:
case pair_seperator:
return false;
}
return ch > ' ';
}
}
}