-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonparse.icn
401 lines (382 loc) · 11.9 KB
/
jsonparse.icn
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
$ifndef JSONPARSE
$define JSONPARSE
############################################################################
#
# File: jsonparse.icn
#
# Subject: Convert JSON text to and from corresponding Icon values.
#
# Authors: Carl Sturtivant, Gregg Townsend
#
# Date: August 26, 2016
#
############################################################################
#
# This file is in the public domain.
#
############################################################################
#
# json(L|T|i|n|r|s) : s
#
# Takes data (list|table|integer|string|real|&null) and produces a JSON
# string defining that data. See http://json.org/. It is an error
# to use another type, even in substructures. To serialize other types,
# see codeobj.icn from the Icon Programming Library.
#
# Works with Icon data structures constructed from tables and lists
# containing nulls, strings, integers and reals as well as values of
# those last four types. Note:
#
# - Icon table default values are ignored on conversion to JSON.
# - Circular structures are not supported.
# - Lists or tables used more than once in a structure will be duplicated
# in the JSON generated.
# - When the `VNOM` preprocessor symbol is defined, the order of the keys
# is preserved (otherwise keys are ordered alphabetically).
#
# jsonparse(s) : x
#
# Takes a JSON string and produces the corresponding Icon value or
# structure. Tables in such a structure will have default values of null.
# JSON text containing true and false (booleans) will have those converted
# to the strings "true" and "false" respectively.
#
# - When the `VNOM` preprocessor symbol is defined, the order of the keys
# of JSON hashes/objects is preserved because a `VNom` is used rather
# than a simple Icon table (otherwise keys are ordered alphabetically).
#
# character sets
#
# Although these routines should work with UTF-8 strings, nothing is
# included here to ensure that UTF-8 is correctly supported.
#
# The characters of an Icon string are an extension of ASCII codes
# to 256 characters obtained by including the additional characters
# defined by ISO/IEC 8859-1 (also called a Latin-1 string), which are
# the first 256 unicode code points. However here, those
# characters are encoded each as a single Icon character.
#
# Quoted strings inside JSON text are always UTF-8 encoded, with all
# control characters escaped using the \uxxxx convention, where xxxx is a
# string of four hexadecimal digits indicating a unicode code point.
#
# the VNOM preprocessor symbol
#
# Note that (by design) conversion may not be completely symmetric when
# the VNOM preprocessor symbol is defined before this file is included.
# If VNOM is defined, then a VNOM (call it "x") will have a "Kind" of
# "VNom", i.e., x[x, "Kind"] == vmsg(x, "kind") == "VNom". The advantage
# of a VNom over an ordinary Icon table is that it preserves the order of
# the member keys in a JSON object (rather than reordering them
# alphabetically).
#
############################################################################
record _json_pair(key, value)
record _json_token(type, val)
global _json_tok, _json_tokens, _json_runerrC
procedure json(x, errC)
local s, k, ks, v
static lsep, tsep
initial {
lsep := ", "
tsep := " : "
}
if \errC
then _json_runerrC := errC
else @(
_json_runerrC :=
create
while v := @&source
do runerr(v[1], v[2])
)
case type(x) of {
"list" : {
s := "["
every v := !x
do
if v ~=== x
then s ||:= json(v) || lsep
s := s[1: -*lsep]
return s||"]"
}
"table" : {
$ifdef VNOM
# test non-exhaustively whether this looks like a VNom
# (i.e., test whether it has most of the VNom attributes)
if (
\(v := x[x]),
key(v) == "Dspsbl",
key(v) == "ID",
key(v) == "Kind",
key(v) == "Mttbl",
key(v) == "State",
key(v) == "Type"
)
then {
# build JSON hash using !v["State"] as order of keys;
# discard all other attributes of the VNom
s := "{"
every k := !v["State"]
do {
ks := if numeric(k) then string(k) else k
s ||:= json(ks) || tsep || json(x[k]) || lsep
}
s := s[1: -*lsep]
return s||"}"
}
else {
# build JSON hash using key(x) as order of keys
s := "{"
every k := key(x)
do
if k ~=== x # discard circular references
then s ||:= json(k) || tsep || json(x[k]) || lsep
s := s[1: -*lsep]
return s||"}"
}
$else
# build JSON hash using key(x) as order of keys
s := "{"
every k := key(x)
do
if k ~=== x # discard circular references
then s ||:= json(k) || tsep || json(x[k]) || lsep
s := s[1: -*lsep]
return s||"}"
$endif # VNOM
}
"integer" : return string(x)
"string" : return _json_escape(x)
"real" : return string(x)
"null" : return "null"
# TODO map set to table with &null values
# TODO map record to table using field(R, i) from records.icn in
# IPL as keys; a VNom could be helpful here, e.g., with
# x[x, "Kind"] == "Dynamic"
default: _json_runerr(123, type(x)) | fail
}
end
procedure jsonparse(s, errC)
local v
if \errC
then _json_runerrC := errC
else @(
_json_runerrC :=
create
while v := @&source
do runerr(v[1], v[2])
)
s := string(s) | _json_runerr(103, s) | fail
_json_tokens := create _json_tokenize(s)
_json_tok := @_json_tokens
return _json_Value()
end
procedure _json_runerr(i, x)
[i, x] @_json_runerrC
end
procedure _json_Value() #JSON value as Icon value
local value
case _json_tok.type of {
"string"|"number"|"boolean"|"null": {
value := _json_tok.val #booleans are "true"/"false" in Icon
_json_tok := @_json_tokens
return value
}
"[": return _json_Array()
"{": return _json_Object()
default: _json_runerr(123, _json_tok.val) | fail
}
end
procedure _json_Array() #JSON array, as Icon list
local ls
_json_tok := @_json_tokens
if _json_tok.type == "]"
then {
_json_tok := @_json_tokens
return []
}
ls := _json_ValSeq()
if _json_tok.type ~== "]"
then
_json_runerr(123, _json_tok.val) | fail
_json_tok := @_json_tokens
return ls
end
procedure _json_ValSeq() #non-empty JSON array body, as Icon list
local ls
ls := [_json_Value()]
while _json_tok.type == ","
do {
_json_tok := @_json_tokens
put(ls, _json_Value())
}
return ls
end
procedure _json_Object() #JSON object, as Icon table
local t
_json_tok := @_json_tokens
if _json_tok.type == "}"
then {
_json_tok := @_json_tokens
$ifdef VNOM
return vnew()
$else
return table()
$endif # VNOM
}
t := _json_PairSeq()
if _json_tok.type ~== "}"
then
_json_runerr(123, _json_tok.val) | fail
_json_tok := @_json_tokens
return t
end
procedure _json_PairSeq() #body of non-empty JSON object, as Icon table
local t, p
$ifdef VNOM
t := vnew()
$else
t := table()
$endif # VNOM
repeat {
p := _json_Pair()
$ifdef VNOM
vmsg(t, "put", p.key, p.value)
$else
t[p.key] := p.value
$endif # VNOM
if _json_tok.type ~== ","
then return t
_json_tok := @_json_tokens
}
end
procedure _json_Pair() #JSON key value pair separated by a colon, as Icon record
local k, val
if _json_tok.type == "number"
then {
_json_tok.val := "\"" || _json_tok.val || "\""
_json_tok.type := "string"
}
if _json_tok.type ~== "string"
then
_json_runerr(123, _json_tok.val) | fail
k := _json_tok.val
_json_tok := @_json_tokens
if _json_tok.type ~== ":"
then
_json_runerr(123, _json_tok.val) | fail
_json_tok := @_json_tokens
val := _json_Value()
return _json_pair(k, val)
end
procedure _json_tokenize(s)
local white, punctuation, token, x
white := ' \t\r\n'
punctuation := '[]{}:,'
s ? repeat {
tab(many(white))
if pos(0) then break
token :=
_json_token( x := tab( any( punctuation)), x) |
_json_token("string", _json_matchstring()) |
_json_token("number", _json_matchnumber()) |
_json_token("boolean", ="true" | ="false") |
_json_token("null", if ="null" then &null ) |
write(&errout,
"\n" || s ||
"\n" || s[1:&pos] || "^"
) |
_json_runerr(123, s[1:&pos]) |
fail
suspend token
}
end
procedure _json_matchnumber()
local p, s, n
p := &pos
s := ="-" | ""
s ||:= tab(many(&digits))
s ||:= ="."
s ||:= tab(many(&digits))
s ||:= tab(any('eE'))
s ||:= tab(any('+-'))
s ||:= tab(many(&digits))
n := numeric(s) | { &pos := p; fail }
return n
end
procedure _json_matchstring()
local p, s, x
p := &pos
="\"" | fail
s := ""
repeat {
s ||:= x := tab( upto('\\"')) | { &pos := p; fail }
if ="\""
then {
# error only if _json_controlchars(x) succeeds
_json_runerr(123, _json_controlchars(x))
return s
}
s ||:= _json_escapeseq() | { &pos := p; fail }
}
end
procedure _json_controlchars(s)
static controlchars
initial controlchars := cset(&ascii[1:33])
s **:= controlchars
if *s = 0 then fail else return s
end
procedure _json_escapeseq()
local ch, hex
="\\" | fail
return case ch := move(1) of {
default :
_json_runerr(123, ch) | fail
"\"": "\""
"\\": "\\"
"/" : "/"
"b" : "\b"
"f" : "\f"
"n" : "\n"
"r" : "\r"
"t" : "\t"
"u" : {
hex := move(4)|tab(0)
_json_unichar(hex) |
_json_runerr(123, hex) | fail
}
}
end
procedure _json_unichar(hex)
static hexdigits, cpt
initial hexdigits := &digits++'abcdef'
hex := map(hex)
( *hex=4 & *(hex--hexdigits)=0 ) | fail
cpt := integer("16r"||hex)
#UTF-8 encoding
if cpt < 128 then return char(cpt)
if cpt < 2048 then {
return char( ior( 2r11000000, ishift(cpt, -6))) ||
char( ior( 2r10000000, iand( 2r111111, cpt)))
}
return char( ior( 2r11100000, ishift(cpt, -12)) ) ||
char( ior( 2r10000000, iand( 2r111111, ishift(cpt, -6))) ) ||
char( ior( 2r10000000, iand( 2r111111, cpt)))
end
procedure _json_escape(s)
local ctlchars, escapechars, ans, ch, esc
if ctlchars := _json_controlchars(s) then {
escapechars := ctlchars ++ '\\"\b\f\n\r\t'
ans := ""
s ? repeat {
ans ||:= tab( upto(escapechars)|0)
if pos(0) then return ans
if any(ctlchars) then
s ||:= "\\u00"||image(move(1))[4:-1]
else
s ||:= image(move(1))
}
}
else return image(s)
end
$endif # JSONPARSE