-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON.dm
309 lines (282 loc) · 7.28 KB
/
JSON.dm
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Usage:
JSON.stringify(obj) - Converts lists and values into a JSON string.
JSON.parse(json) - Converts a JSON string into lists and values.
*/
/var/datum/jsonHelper/JSON = new // A namespace for procs.
// ************************************ stringify ************************************
/datum/jsonHelper/proc/stringify(value)
return list2text(WriteValue(list(), value))
/datum/jsonHelper/proc/WriteValue(list/json, value)
. = json
if(isnum(value))
json += value // Consider num2text(value, 20) for maximum accuracy.
else if(isnull(value))
json += "null"
else if(istext(value))
WriteString(json, value)
else if(istype(value, /list))
WriteList(json, value)
else
throw EXCEPTION("Datums cannot be converted to JSON.")
/datum/jsonHelper/proc/WriteString(list/json, str)
. = json
var/quotePos = findtextEx(str, "\"")
var/bsPos = findtextEx(str, "\\")
if (quotePos == 0 && bsPos == 0)
json.Add("\"", str, "\"")
else
json += "\""
var/lastStop = 1
while(quotePos != 0 || bsPos != 0)
var/escPos
if(quotePos < bsPos && quotePos != 0 || bsPos == 0)
escPos = quotePos
else
escPos = bsPos
json.Add(copytext(str, lastStop, escPos), "\\")
lastStop = escPos
if(escPos == quotePos)
quotePos = findtextEx(str, "\"", escPos + 1)
else if(escPos == bsPos)
bsPos = findtextEx(str, "\\", escPos + 1)
json.Add(copytext(str, lastStop), "\"")
/datum/jsonHelper/proc/WriteList(list/json, list/listVal)
. = json
#define Either 0
#define CannotBeArray 1
#define CannotBeObject 2
#define BadList (CannotBeArray | CannotBeObject)
var/listType = Either
for(var/key in listVal)
if(istext(key))
if(!isnull(listVal[key]))
listType |= CannotBeArray
else
if(!isnum(key) && !isnull(listVal[key]))
listType = BadList
else
listType |= CannotBeObject
if(listType == BadList)
throw EXCEPTION("The given list cannot be converted to JSON.")
if(listType == CannotBeArray)
json += "{"
var/addComma
for(var/key in listVal)
if(addComma)
json += ","
else
addComma = TRUE
WriteString(json, key)
json += ":"
WriteValue(json, listVal[key])
json += "}"
else
json += "\["
var/addComma
for(var/key in listVal)
if(addComma)
json += ","
else
addComma = TRUE
WriteValue(json, key)
json += "]"
#undef Either
#undef CannotBeFlat
#undef CannotBeAssoc
#undef BadList
// ************************************ parse ************************************
#define aBackspace 0x08
#define aTab 0x09
#define aLineBreak 0x0A
#define aVertTab 0x0B
#define aFormFeed 0x0C
#define aCarriageReturn 0x0D
#define aSpace 0x20
#define aZero 0x30
#define aNonBreakSpace 0xA0
#define Advance if(++readPos > jsonLen) { curAscii = 0; curChar = "" } else { curAscii = text2ascii(json, readPos); curChar = ascii2text(curAscii) } // Deal with it.
#define SkipWhitespace while(curAscii in whitespace) Advance
#define AdvanceWS Advance; SkipWhitespace
/datum/jsonHelper/var
readPos
jsonLen
json
curAscii
curChar
static/list/whitespace = list(aTab, aLineBreak, aVertTab, aFormFeed, aCarriageReturn, aSpace, aNonBreakSpace)
/datum/jsonHelper/proc/parse(json)
readPos = 0
jsonLen = length(json)
src.json = json
curAscii = 0
curChar = ""
AdvanceWS
var/value = ParseValue()
if(readPos < jsonLen)
throw EXCEPTION("Expected: End of JSON")
return value
/datum/jsonHelper/proc/ParseValue()
if(curChar == "\"")
return ParseString()
else if(curChar == "-" || (curAscii >= aZero && curAscii <= aZero + 9) || curChar == "I" || curChar == "N")
return ParseNumber()
else if(curChar == "{")
return ParseObject()
else if(curChar == "\[")
return ParseArray()
else if(curChar == "t")
if(copytext(json, readPos, readPos+4) == "true")
readPos += 3
AdvanceWS
return TRUE
else
throw EXCEPTION("Expected: 'true'")
else if(curChar == "f")
if(copytext(json, readPos, readPos+5) == "false")
readPos += 4
AdvanceWS
return FALSE
else
throw EXCEPTION("Expected: 'false'")
else if(curChar == "n")
if(copytext(json, readPos, readPos+4) == "null")
readPos += 3
AdvanceWS
return null
else
throw EXCEPTION("Expected: 'null'")
else if(curChar == "")
throw EXCEPTION("Unexpected: End of JSON")
else
throw EXCEPTION("Unexpected: '[curChar]'")
/datum/jsonHelper/proc/ParseString()
ASSERT(curChar == "\"")
Advance
var/list/chars = list()
while(readPos <= jsonLen)
if(curChar == "\"")
AdvanceWS
return list2text(chars)
else if(curChar == "\\")
Advance
switch(curChar)
if("\"", "\\", "/")
chars += ascii2text(curAscii)
if("b")
chars += ascii2text(aBackspace)
if("f")
chars += ascii2text(aFormFeed)
if("n")
chars += "\n"
if("r")
chars += ascii2text(aCarriageReturn) // Should we ignore these?
if("t")
chars += "\t"
if("u")
throw EXCEPTION("JSON \\uXXXX escape sequence not supported")
else
throw EXCEPTION("Invalid escape sequence")
Advance
else
chars += ascii2text(curAscii)
Advance
throw EXCEPTION("Unterminated string")
/datum/jsonHelper/proc/ParseNumber()
var/firstPos = readPos
if(curChar == "N") // Special nonstandard value.
if(copytext(json, readPos, readPos+3) == "NaN")
readPos += 2
AdvanceWS
return 1.#IND
else
throw EXCEPTION("Expected: 'NaN'")
else if(curChar == "I") // Special nonstandard value.
if(copytext(json, readPos, readPos+8) == "Infinite")
readPos += 7
AdvanceWS
return 1.#INF
else
throw EXCEPTION("Expected: 'Infinite'")
if(curChar == "-")
Advance
if(curChar == "I") // Special nonstandard value.
if(copytext(json, readPos, readPos+8) == "Infinite")
readPos += 7
AdvanceWS
return -1.#INF
else
throw EXCEPTION("Expected: '-Infinite'")
if(curAscii >= aZero + 1 && curAscii <= aZero + 9)
do
Advance
while(curAscii >= aZero && curAscii <= aZero + 9)
else if(curAscii == aZero)
Advance
else
throw EXCEPTION("Expected: digit")
if(curChar == ".")
Advance
var/found = FALSE
while(curAscii >= aZero && curAscii <= aZero + 9)
found = TRUE
Advance
if(!found)
throw EXCEPTION("Expected: digit")
if(curChar == "E" || curChar == "e")
Advance
var/found = FALSE
if(curChar == "-")
Advance
else if(curChar == "+")
Advance
while(curAscii >= aZero && curAscii <= aZero + 9)
found = TRUE
Advance
if(!found)
throw EXCEPTION("Expected: digit")
SkipWhitespace
return text2num(copytext(json, firstPos, readPos))
/datum/jsonHelper/proc/ParseObject()
ASSERT(curChar == "{")
var/list/object = list()
AdvanceWS
while(curChar == "\"")
var/key = ParseString()
if(curChar != ":")
throw EXCEPTION("Expected: ':'")
AdvanceWS
object[key] = ParseValue()
if(curChar == ",")
AdvanceWS
else
break
if(curChar != "}")
throw EXCEPTION("Expected: string or '}'")
AdvanceWS
return object
/datum/jsonHelper/proc/ParseArray()
ASSERT(curChar == "\[")
var/list/array = list()
AdvanceWS
while(curChar != "]")
array += list(ParseValue()) // Wrapped in a list in case ParseValue() returns a list.
if(curChar == ",")
AdvanceWS
else
break
if(curChar != "]")
throw EXCEPTION("Expected: ']'")
AdvanceWS
return array
#undef aBackspace
#undef aTab
#undef aLineBreak
#undef aVertTab
#undef aFormFeed
#undef aCarriageReturn
#undef aSpace
#undef aZero
#undef aNonBreakSpace
#undef Advance
#undef SkipWhitespace
#undef AdvanceWS