-
Notifications
You must be signed in to change notification settings - Fork 86
/
json.scm
456 lines (390 loc) · 13.5 KB
/
json.scm
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
#|
Copyright (c) 2011-2014, Marc Feeley
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
(##namespace ("json#"))
(##include "~~lib/gambit#.scm")
(##include "json#.scm")
(declare
(standard-bindings)
(extended-bindings)
(block)
(fixnum)
(not safe)
)
;;;============================================================================
(define use-symbols? #f) ;; how to encode JS true, false and null
(define use-tables? #f) ;; how to encode JS objects
(define use-symbols-for-keys? #f) ;; how to encode JS object slot names
(define debug? #f)
(if debug?
(set! ##wr
(lambda (we obj)
(##default-wr
we
(if (table? obj)
(cons 'TABLE (table->list obj))
obj)))))
(define (json-set-options! #!key (symbols #f) (tables #f) (keys #f))
(set! use-symbols? symbols)
(set! use-tables? tables)
(set! use-symbols-for-keys? keys))
(define-macro (->string obj)
`(cond
((symbol? ,obj) (symbol->string ,obj))
(else ,obj)))
(define (json-decode str)
(call-with-input-string str json-read))
(define (json-encode obj)
(call-with-output-string "" (lambda (port) (json-write obj port))))
(define (json-read port)
(define (create-object props)
(if use-tables?
(list->table props)
props))
(define (create-array elements)
(list->vector elements))
(define (rd)
(read-char port))
(define (pk)
(peek-char port))
(define (accum c i str)
(if (not (json-error? str))
(string-set! str i c))
str)
(define (digit? c radix)
(and (char? c)
(let ((n
(cond ((and (char>=? c #\0) (char<=? c #\9))
(- (char->integer c) (char->integer #\0)))
((and (char>=? c #\a) (char<=? c #\z))
(+ 10 (- (char->integer c) (char->integer #\a))))
((and (char>=? c #\A) (char<=? c #\Z))
(+ 10 (- (char->integer c) (char->integer #\A))))
(else
999))))
(and (< n radix)
n))))
(define (space)
(let ((c (pk)))
(if (and (char? c)
(char<=? c #\space))
(begin (rd) (space)))))
(define (parse-value)
(space)
(let ((c (pk)))
(if (not (char? c))
json-error
(cond ((eqv? c #\{)
(parse-object))
((eqv? c #\[)
(parse-array))
((eqv? c #\")
(parse-string))
((or (eqv? c #\-) (digit? c 10))
(parse-number))
((eqv? c #\f)
(rd)
(if (not (and (eqv? (rd) #\a)
(eqv? (rd) #\l)
(eqv? (rd) #\s)
(eqv? (rd) #\e)))
json-error
(if use-symbols?
'false
#f)))
((eqv? c #\t)
(rd)
(if (not (and (eqv? (rd) #\r)
(eqv? (rd) #\u)
(eqv? (rd) #\e)))
json-error
(if use-symbols?
'true
#t)))
((eqv? c #\n)
(rd)
(if (not (and (eqv? (rd) #\u)
(eqv? (rd) #\l)
(eqv? (rd) #\l)))
json-error
(if use-symbols?
'null
'())))
(else
json-error)))))
(define (parse-object)
(rd) ;; skip #\{
(space)
(if (eqv? (pk) #\})
(begin (rd) (create-object '()))
(let loop ((rev-elements '()))
(let ((str (if (not (eqv? (pk) #\")) json-error (parse-string))))
(if (json-error? str)
str
(begin
(space)
(if (not (eqv? (pk) #\:))
json-error
(begin
(rd)
(space)
(let ((val (parse-value)))
(if (json-error? val)
val
(let ((new-rev-elements
(cons (cons (if use-symbols-for-keys?
(string->symbol str)
str) val)
rev-elements)))
(space)
(let ((c (pk)))
(cond ((eqv? c #\})
(rd)
(create-object
(reverse new-rev-elements)))
((eqv? c #\,)
(rd)
(space)
(loop new-rev-elements))
(else
json-error))))))))))))))
(define (parse-array)
(rd) ;; skip #\[
(space)
(if (eqv? (pk) #\])
(begin (rd) (create-array '()))
(let ((x (parse-value)))
(if (json-error? x)
x
(let loop ((rev-elements (list x)))
(space)
(let ((c (pk)))
(cond ((eqv? c #\])
(rd)
(create-array (reverse rev-elements)))
((eqv? c #\,)
(rd)
(let ((y (parse-value)))
(if (json-error? y)
y
(loop (cons y rev-elements)))))
(else
json-error))))))))
(define (parse-string)
(define (parse-str pos)
(let ((c (rd)))
(cond ((eqv? c #\")
(make-string pos))
((eqv? c #\\)
(let ((x (rd)))
(if (eqv? x #\u)
(let loop ((n 0) (i 4))
(if (> i 0)
(let ((h (rd)))
(cond ((not (char? h))
json-error)
((digit? h 16)
=>
(lambda (d)
(loop (+ (* n 16) d) (- i 1))))
(else
json-error)))
(accum (integer->char n) pos (parse-str (+ pos 1)))))
(let ((e (assv x json-string-escapes)))
(if e
(accum (cdr e) pos (parse-str (+ pos 1)))
json-error)))))
((char? c)
(accum c pos (parse-str (+ pos 1))))
(else
json-error))))
(rd) ;; skip #\"
(parse-str 0))
(define (parse-number)
(define (sign-part)
(let ((c (pk)))
(if (eqv? c #\-)
(begin (rd) (accum c 0 (after-sign-part 1)))
(after-sign-part 0))))
(define (after-sign-part pos)
(if (not (digit? (pk) 10))
json-error
(integer-part pos)))
(define (integer-part pos)
(let ((c (pk)))
(if (digit? c 10)
(begin (rd) (accum c pos (integer-part (+ pos 1))))
(if (eqv? c #\.)
(begin (rd) (accum c pos (decimals-part (+ pos 1))))
(exponent-part pos)))))
(define (decimals-part pos)
(let ((c (pk)))
(if (digit? c 10)
(begin (rd) (accum c pos (decimals-part (+ pos 1))))
(exponent-part pos))))
(define (exponent-part pos)
(let ((c (pk)))
(if (or (eqv? c #\e) (eqv? c #\E))
(begin (rd) (accum c pos (exponent-sign-part (+ pos 1))))
(done pos))))
(define (exponent-sign-part pos)
(let ((c (pk)))
(if (or (eqv? c #\-) (eqv? c #\+))
(begin (rd) (accum c pos (exponent-after-sign-part (+ pos 1))))
(exponent-after-sign-part pos))))
(define (exponent-after-sign-part pos)
(if (not (digit? (pk) 10))
json-error
(exponent-integer-part pos)))
(define (exponent-integer-part pos)
(let ((c (pk)))
(if (digit? c 10)
(begin (rd) (accum c pos (exponent-integer-part (+ pos 1))))
(done pos))))
(define (done pos)
(make-string pos))
(let ((str (sign-part)))
(if (json-error? str)
str
(string->number str))))
(parse-value))
(define (json-write obj port)
(define (wr-string s)
(display #\" port)
(let loop ((i 0) (j 0))
(if (< j (string-length s))
(let* ((c
(string-ref s j))
(n
(char->integer c))
(ctrl-char?
(or (<= n 31) (>= n 127)))
(x
(cond ((or (char=? c #\\)
(char=? c #\"))
c)
((and ctrl-char?
(##assq-cdr c json-string-escapes))
=>
car)
(else
#f)))
(j+1
(+ j 1)))
(if (or x ctrl-char?)
(begin
(display (substring s i j) port)
(display #\\ port)
(if x
(begin
(display x port)
(loop j+1 j+1))
(begin
(display #\u port)
(display (substring (number->string (+ n #x10000) 16)
1
5)
port)
(loop j+1 j+1))))
(loop i j+1)))
(begin
(display (substring s i j) port)
(display #\" port)))))
(define (wr-prop prop)
(wr-string (->string (car prop)))
(display ":" port)
(wr (cdr prop)))
(define (wr-object obj)
(wr-props (table->list obj)))
(define (wr-props lst)
(display "{" port)
(if (pair? lst)
(begin
(wr-prop (car lst))
(let loop ((lst (cdr lst)))
(if (pair? lst)
(begin
(display "," port)
(wr-prop (car lst))
(loop (cdr lst)))))))
(display "}" port))
(define (wr-array obj)
(display "[" port)
(let loop ((i 0))
(if (< i (vector-length obj))
(begin
(if (> i 0) (display "," port))
(wr (vector-ref obj i))
(loop (+ i 1)))))
(display "]" port))
(define (wr-time obj)
;; this works when using JavaScript's eval to decode the object
(display "new Date(" port)
(display (* 1000 (time->seconds obj)) port)
(display ")" port))
(define (wr obj)
(cond ((number? obj)
(write (if (integer? obj) obj (exact->inexact obj)) port))
((string? obj)
(wr-string obj))
((symbol? obj)
(display (symbol->string obj) port))
((boolean? obj)
(display (if obj "true" "false") port))
((and (not use-symbols?)
(null? obj))
(display "null" port))
((or (null? obj)
(pair? obj))
(wr-props obj))
((vector? obj)
(wr-array obj))
((table? obj)
(wr-object obj))
((time? obj)
(wr-time obj))
(else
(error "unwritable object" obj))))
(wr obj))
(define json-string-escapes
'((#\" . #\")
(#\\ . #\\)
(#\/ . #\/)
(#\b . #\x08)
(#\t . #\x09)
(#\n . #\x0A)
(#\v . #\x0B)
(#\f . #\x0C)
(#\r . #\x0D)))
(define json-error
'json-error)
(define (json-error? x)
(eq? x json-error))
;;;============================================================================