forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.lcb
More file actions
520 lines (430 loc) · 15.6 KB
/
Copy pathjson.lcb
File metadata and controls
520 lines (430 loc) · 15.6 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
Copyright (C) 2015 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*
This library provides support for generating and parsing JavaScript Object
Notation (JSON) data. See also <http://json.org>.
Tags: JSON, Library
*/
library com.livecode.library.json
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "JSON Library"
--================================================================
-- JSON error handling
--================================================================
-- Workaround string formatting issues
private handler FormatInt(in pNumber as Number) returns String
variable tNumberString
put pNumber formatted as string into tNumberString
if "." is in tNumberString then
variable tDotOffset
put the first offset of "." in tNumberString into tDotOffset
delete char tDotOffset to (the number of chars in tNumberString) of tNumberString
end if
return tNumberString
end handler
private handler JsonSyntaxError(in pLine as Number, in pColumn as Number, \
in pMessage as String) returns nothing
throw "syntax error:" && FormatInt(pLine) & ":" & FormatInt(pColumn) && pMessage
end handler
--================================================================
-- JSON parser
--================================================================
-- Scanner states
constant kScanStartToken is 0
constant kScanEndToken is 1
constant kScanInString is 10
constant kScanInStringEscape is 11
constant kScanInStringUnicode is 12
constant kScanInLiteral is 20
constant kScanInNumber is 30
-- Parser states
constant kParseTop is 0
constant kParseEndValue is 1
constant kParseArrayStart is 10
constant kParseArrayValue is 11
constant kParseArrayNext is 12
constant kParseObjectStart is 20
constant kParseObjectKey is 21
constant kParseObjectColon is 22
constant kParseObjectValue is 23
constant kParseObjectNext is 24
/*
Summary: Parse JSON text into a LiveCode value.
pJson: String containing JSON text
Returns: The top level value. The type depends on the JSON content.
Description:
<JsonImport> is used to convert data encoded in JSON format into a LiveCode
value. It parses <pJson> and returns the first top-level value found. If any
syntax errors are found in <pJson>, an error is thrown.
The return value may be any of the following types, depending on the content
of the file:
* Array (for a JSON object)
* List (for a JSON array)
* String (for a JSON string)
* Number (for any JSON number)
* Boolean (for JSON `true` or `false`)
* nothing (for JSON `null`)
Tags: JSON
*/
public handler JsonImport(in pJson as String) returns any
-- These variables track the cursor position
variable tLine as Number
put 1 into tLine
variable tColumn as Number
put 0 into tColumn
-- Scanner state
variable tScanState as Number
put kScanStartToken into tScanState
variable tToken as optional String
variable tExtraToken as optional String
variable tTokenValue as optional String
variable tStringUniHex as String
put "" into tStringUniHex
-- Parser state
variable tParseState as Number
put kParseTop into tParseState
variable tValue as optional any
variable tValueStack as List
put [] into tValueStack
variable tStateStack as List
put [] into tStateStack
-- Loop over all the charaters in the JSON input in a single pass
variable tChar as String
repeat for each char tChar in pJson
-- Update cursor position
if tChar is in "\r\n" then
add 1 to tLine
put 1 into tColumn
else
add 1 to tColumn
end if
---------- TOKEN SCANNER
if tScanState is kScanStartToken then
-- Consume whitespace
if tChar is in " \t\r\n" then
next repeat
end if
if tChar is in "{}[]:," then
put tChar into tToken
put nothing into tTokenValue
put kScanEndToken into tScanState
else if tChar is "\q" then
put tChar into tToken
put "" into tTokenValue
put kScanInString into tScanState
else if tChar is in "tfn" then
put tChar into tToken
put tChar into tTokenValue
put kScanInLiteral into tScanState
else if tChar is in "-0123456789" then
put "0" into tToken
put tChar into tTokenValue
put kScanInNumber into tScanState
else
return JsonSyntaxError(tLine, tColumn, \
"Unexpected character '" & tChar & "'")
end if
-- String support is complicated
else if tScanState is kScanInString then
if tChar is "\q" then
put kScanEndToken into tScanState
else if tChar is "\\" then
put kScanInStringEscape into tScanState
else
put tChar after tTokenValue
end if
else if tScanState is kScanInStringEscape then
if tChar is in "\q\\/" then
put tChar after tTokenValue
put kScanInString into tScanState
else if tChar is "n" then
put "\n" after tTokenValue
put kScanInString into tScanState
else if tChar is "r" then
put "\r" after tTokenValue
put kScanInString into tScanState
else if tChar is "t" then
put "\t" after tTokenValue
put kScanInString into tScanState
else if tChar is "u" then
put "" into tStringUniHex
put kScanInStringUnicode into tScanState
else if tChar is "b" then
put "\u{08}" after tTokenValue
put kScanInString into tScanState
else if tChar is "f" then
put "\u{0c}" after tTokenValue
put kScanInString into tScanState
else
return JsonSyntaxError(tLine, tColumn, \
"illegal escape sequence '\\" & tChar & "'")
end if
else if tScanState is kScanInStringUnicode then
put tChar after tStringUniHex
if not (tChar is in "0123456789abcdefABCDEF") then
return JsonSyntaxError(tLine, tColumn, \
"illegal escape sequence '\\u" & tStringUniHex & "'")
end if
if the number of chars in tStringUniHex >= 4 then
put the char with code (tStringUniHex converted from base 16) after tTokenValue
put kScanInString into tScanState
end if
else if tScanState is kScanInLiteral then
put tChar after tTokenValue
if tToken is "n" and the number of chars in tTokenValue is 4 then
if tTokenValue is not "null" then
return JsonSyntaxError(tLine, tColumn, \
"invalid token '" & tTokenValue & "'")
end if
put kScanEndToken into tScanState
else if tToken is "t" and the number of chars in tTokenValue is 4 then
if tTokenValue is not "true" then
return JsonSyntaxError(tLine, tColumn, \
"invalid token '" & tTokenValue & "'")
end if
put kScanEndToken into tScanState
else if tToken is "f" and the number of chars in tTokenValue is 5 then
if tTokenValue is not "false" then
return JsonSyntaxError(tLine, tColumn, \
"invalid token '" & tTokenValue & "'")
end if
put kScanEndToken into tScanState
end if
-- Numbers are a massive pain. Because you can't tell from examining a
-- single character whether it ends the number, you have to wait until the
-- character after the *end* of the number -- which might be the start of
-- another token.
else if tScanState is kScanInNumber then
if tChar is in ".+-eE0123456789" then
put tChar after tTokenValue
next repeat
else
put kScanEndToken into tScanState
if tChar is in "]}," then
put tChar into tExtraToken
else if not (tChar is in " \t\r\n") then
return JsonSyntaxError(tLine, tColumn, "bad number terminal '" & tChar & "'")
end if
end if
else
return JsonSyntaxError(tLine, tColumn, "bad scan state")
end if
---------- PARSER
-- Loop until a whole token is found
if tScanState is not kScanEndToken then
next repeat
end if
put kScanStartToken into tScanState
-- Allow for an extra parsing pass when the end of a number is found
variable tNeedPass as Boolean
put true into tNeedPass
repeat while tNeedPass
put false into tNeedPass
-- At this point we know that the next token is in tToken and if it's a
-- token that takes a value the value will be in tTokenValue
put nothing into tValue
if tToken is "\q" then
put tTokenValue into tValue
else if tToken is "n" then
put nothing into tValue
else if tToken is "t" then
put true into tValue
else if tToken is "f" then
put false into tValue
else if tToken is "0" then
put tTokenValue parsed as number into tValue
-- At the end of a number, there are (potentially) two tokens found
-- rather than just one
if tExtraToken is not nothing then
put tExtraToken into tToken
put nothing into tExtraToken
put true into tNeedPass
end if
if tValue is nothing then
return JsonSyntaxError(tLine, tColumn, \
"unsupported number format '" & tTokenValue & "'")
end if
else if tToken is "[" then
if tParseState is in [kParseTop, kParseArrayStart, kParseArrayNext, \
kParseObjectColon, kParseObjectNext] then
push [] onto tValueStack
push tParseState onto tStateStack
put kParseArrayStart into tParseState
next repeat
else
return JsonSyntaxError(tLine, tColumn, "unexpected '['")
end if
else if tToken is "]" then
if tParseState is kParseArrayStart or tParseState is kParseArrayValue then
pop tValueStack into tValue
pop tStateStack into tParseState
else
return JsonSyntaxError(tLine, tColumn, "unexpected ']'")
end if
else if tToken is "{" then
if tParseState is in [kParseTop, kParseArrayStart, kParseArrayNext, \
kParseObjectColon, kParseObjectNext] then
push the empty array onto tValueStack
push tParseState onto tStateStack
put kParseObjectStart into tParseState
next repeat
else
return JsonSyntaxError(tLine, tColumn, "unexpected '{'")
end if
else if tToken is "}" then
if tParseState is kParseObjectStart or tParseState is kParseObjectValue then
pop tValueStack into tValue
pop tStateStack into tParseState
else
return JsonSyntaxError(tLine, tColumn, "unexpected '}'")
end if
else if tToken is "," then
if tParseState is kParseArrayValue then
put kParseArrayNext into tParseState
next repeat
else if tParseState is kParseObjectValue then
put kParseObjectNext into tParseState
next repeat
else
return JsonSyntaxError(tLine, tColumn, "unexpected ','")
end if
else if tToken is ":" then
if tParseState is kParseObjectKey then
put kParseObjectColon into tParseState
next repeat
else
return JsonSyntaxError(tLine, tColumn, "unexpected ':'")
end if
else
return JsonSyntaxError(tLine, tColumn, \
"unhandled token '" & tToken & "'")
return nothing
end if
-- At this point, we know that tValue contains a complete value that's being
-- returned to its enclosing context
-- Array construction
if tParseState is kParseArrayStart or tParseState is kParseArrayNext then
push tValue onto tValueStack[-1]
put kParseArrayValue into tParseState
-- Object construction
else if tParseState is kParseObjectStart or tParseState is kParseObjectNext then
if tValue is a string then
push tValue onto tValueStack
put kParseObjectKey into tParseState
else
return JsonSyntaxError(tLine, tColumn, "expected string")
end if
else if tParseState is kParseObjectColon then
variable tKey as String
pop tValueStack into tKey
put tValue into tValueStack[-1][tKey]
put kParseObjectValue into tParseState
-- Top level
else if tParseState is kParseTop then
return tValue
else
return JsonSyntaxError(tLine, tColumn, "bad parse state" && FormatInt(tParseState) && "at token '" & tToken & "'")
end if
end repeat
end repeat
return JsonSyntaxError(tLine, tColumn, "unexpected end of input")
end handler
--================================================================
-- JSON generator
--================================================================
private handler JsonExportString(in pValue as String) returns String
replace "\\" with "\\\\" in pValue
replace "\n" with "\\n" in pValue
replace "\r" with "\\r" in pValue
replace "\t" with "\\t" in pValue
replace "\q" with "\\\q" in pValue
replace "\u{08}" with "\\b" in pValue
replace "\u{0c}" with "\\f" in pValue
return "\q" & pValue & "\q"
end handler
private handler JsonExportNumber(in pValue as Number) returns String
return pValue formatted as string
end handler
private handler JsonExportList(in pValue as List) returns String
variable tTokens as List
put [] into tTokens
variable tFirst as Boolean
put true into tFirst
push "[" onto tTokens
variable tElement as optional any
repeat for each element tElement in pValue
if tFirst then
put false into tFirst
else
push "," onto tTokens
end if
push JsonExport(tElement) onto tTokens
end repeat
push "]" onto tTokens
variable tResult as String
combine tTokens with "" into tResult
return tResult
end handler
private handler JsonExportArray(in pValue as Array) returns String
variable tTokens as List
put [] into tTokens
variable tFirst as Boolean
put true into tFirst
push "{" onto tTokens
variable tKey as String
repeat for each key tKey in pValue
if tFirst then
put false into tFirst
else
push "," onto tTokens
end if
push JsonExport(tKey) onto tTokens
push ": " onto tTokens
push JsonExport(pValue[tKey]) onto tTokens
end repeat
push "}" onto tTokens
variable tResult as String
combine tTokens with "" into tResult
return tResult
end handler
/*
Summary: Format a LiveCode value as JSON text
pValue: A LiveCode value (Array, List, String, Number, Boolean, or nothing)
Returns: A string containing JSON-formatted text.
Description:
<JsonExport> is used to convert a LiveCode value into data encoded in JSON
format. If <pValue> is of a type that cannot be converted to JSON, an error is
thrown.
Tags: JSON
*/
public handler JsonExport(in pValue as optional any) returns String
if pValue is a string then
return JsonExportString(pValue)
else if pValue is a number then
return JsonExportNumber(pValue)
else if pValue is a list then
return JsonExportList(pValue)
else if pValue is an array then
return JsonExportArray(pValue)
else if pValue is true then
return "true"
else if pValue is false then
return "false"
else if pValue is nothing then
return "null"
else
throw "Unsupported value type for JSON"
end if
end handler
end library