-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson-lib.lisp
More file actions
620 lines (550 loc) · 18.2 KB
/
json-lib.lisp
File metadata and controls
620 lines (550 loc) · 18.2 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
;;;; JSON-Lib
;; A simple, relatively fast parser and encoder.
;;
;; Copyright 2021 Alex Nygren (Kina, LLC) kinaknowledge.com
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to
;; deal in the Software without restriction, including without limitation the
;; rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included
;; in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
;; IN THE SOFTWARE.
(in-package #:json-lib)
;; CONSTANTS
(defconstant +left-bracket+ #\[)
(defconstant +right-bracket+ #\])
(defconstant +left-brace+ #\{)
(defconstant +right-brace+ #\})
(defconstant +space+ #\ )
(defconstant +comma+ #\,)
(defconstant +tab+ #\Tab)
(defconstant +colon+ #\:)
(defconstant +quote+ #\")
(defconstant +new-line+ #\Linefeed)
(defconstant +period+ #\.)
(defconstant +escape-char+ #\\)
(defconstant +forward-slash+ #\/)
(defconstant +f-char+ #\f)
(defconstant +t-char+ #\t)
(defconstant +hyphen+ #\-)
(defconstant +e+ #\e)
(defconstant +CAP-E+ #\E)
(defconstant +u+ #\u)
(defconstant +CAP-U+ #\U)
(defconstant +plus+ #\+)
(defconstant +in-code+ 0)
(defconstant +in-comment+ 1)
(defconstant +in-quotes+ 2)
(defconstant +in-string+ 2)
(defconstant +in-qm+ 3)
(defconstant +in-number+ 4)
(defconstant +in-array+ 5)
(defconstant +in-pairs+ 6)
(defconstant +in-boolean+ 7)
(defconstant +in-key+ 8)
(defun lookup-escape (c)
(cond
((char= c +escape-char+)
+escape-char+)
((char= c #\n)
+new-line+)
((char= c #\t)
+tab+)
((char= c #\r)
#\Newline)
((char= c +quote+)
+quote+)
((char= c +forward-slash+)
+forward-slash+)
(T
nil)))
(defun parse (json-text &key use-keywords-for-keys trace (max-depth 1000) (max-exponent-length 2) object-key-handler)
"Given an encoded UTF-8 JSON string, returns a Common Lisp structure or value.
If use-keywords-for-keys is T, then hash table keys will be constructed as keywords.
If an object-key-handler lambda/function is provided, this will be called for each object key and the result
value used for the specific object key.
By default the limit is 1000 for structural depth, but this can be set with the keyword
max-depth. Exponent representation in serialized form is limited to a length of 2 to prevent huge values
causing slow downs and other issues in the conversion process."
(let
((pos 0)
(c nil)
(escape-c nil)
(escape-mode 0)
(depth 0)
(total-length (length json-text)))
(labels
((next-char ()
(when (< pos total-length)
(setf c (elt json-text pos))
(if (and (< escape-mode 2)
(char= c +escape-char+))
(setf escape-mode 2)
(decf escape-mode))
(setf escape-mode (max escape-mode 0))
(incf pos)
c))
(peek-next-char ()
(if (< pos total-length)
(elt json-text pos)
nil))
(add-escape-char (c collector)
(progn
(setf escape-c (lookup-escape c))
(when escape-c
(vector-push-extend escape-c collector))))
(raise-error (message)
(error (format nil "pos: ~A: ~A: ~A*"
pos
message
(str:substring (max 0 (- pos 25)) (min total-length (+ pos 1)) json-text))))
(delimiter? (ch)
(if (or (null ch)
(char= ch +space+ )
(char= ch +comma+ )
(char= ch +new-line+)
(char= ch +tab+))
T
nil))
;; main recursive decode routine
(read-obj (&optional (read-mode 0) (last-c nil))
(let
((collector (make-array 0
:element-type T
:adjustable T
:fill-pointer 0))
(str-val nil)
(mode read-mode)
(rval nil)
(in-unicode nil)
(exponent-present nil)
(is-float? nil))
(incf depth)
(when last-c
(vector-push-extend last-c collector))
(when (> depth max-depth)
(raise-error "recursive parse depth exceeded max-depth"))
(loop for c = (cond
((or (eq mode +in-array+)
(eq mode +in-pairs+)
(eq mode +in-number+))
(peek-next-char))
(T
(next-char)))
do
(when trace
(format T "~4D ~5D ~2D ~3D ~A~%"
pos
depth
escape-mode
mode
c)
(sleep 0.0))
(cond
((eq mode +in-string+)
(progn
(cond
;(add-escape-char c collector))
((null c)
(raise-error "unclosed string"))
;; unicode character on-way
((and (= escape-mode 1)
(char= c +u+))
(setf in-unicode
(make-array 4 :element-type 'character :fill-pointer 0)))
;; specifically not allowed
((and (= escape-mode 1)
(char= c +cap-u+))
(raise-error "invalid U"))
;; other escape codes
((= escape-mode 1)
(add-escape-char c collector))
((and (= escape-mode 0)
(char= c +quote+))
(loop-finish)) ;; return the collector
((and (= escape-mode 0)
(< (char-code c) 32))
(raise-error "unescaped control-code"))
;; if in-unicode and pos equals in-unicode+4, then calc the character and place
;; it in the buffer
(in-unicode
(if (= (length in-unicode) 4)
(progn
(vector-push-extend (code-char (parse-integer (map 'string (lambda (x) x) in-unicode)
:radix 16))
collector)
(setf in-unicode
nil))
(vector-push c in-unicode)))
;; otherwise just push it in
((= escape-mode 0)
(vector-push-extend c collector)))))
((eq mode +in-number+)
(cond
((null c)
(loop-finish))
((delimiter? c)
(progn
(next-char)
(loop-finish)))
((or (char= c +right-bracket+)
(char= c +right-brace+)
(char= c +colon+))
(loop-finish))
((digit-char-p c)
(progn
(when (and exponent-present
(> (- (length collector) exponent-present) max-exponent-length))
(raise-error "exponent too large"))
(vector-push-extend (next-char) collector)))
((or (char= c +e+)
(char= c +CAP-E+))
(progn
(next-char)
(vector-push-extend +CAP-E+ collector)
(setf exponent-present (length collector))))
((char= c +period+)
(progn
(vector-push-extend (next-char) collector)
(setf is-float? T)))
((and (or (char= c +hyphen+)
(char= c +plus+))
(> (length collector) 0)
(or (char= +e+ (elt collector (- (length collector) 1)))
(char= +CAP-E+ (elt collector (- (length collector) 1)))))
(vector-push-extend (next-char) collector))
(T
(raise-error "invalid char - expected digit"))))
((eq mode +in-key+)
(let
((key (if (char= +quote+ c)
(read-obj +in-string+)
(read-obj +in-number+)
)))
(cond
((and (or (typep key 'string)
(typep key 'number))
(not (null (peek-next-char)))
(char= (peek-next-char) +colon+))
(progn
(next-char) ;; grab the colon and return the key
(setf rval key)
(loop-finish)))
(T
(raise-error "invalid key format")))))
((eq mode +in-boolean+)
(progn
(when (null c)
(raise-error "invalid nil"))
(vector-push-extend c collector)
(cond
((< (length collector) 4)
nil) ;; keep collecting
((> (length collector) 5)
(raise-error "invalid boolean"))
(T
(progn
(setf str-val (map 'STRING (lambda (x) x) collector))
(cond
((equal str-val "false")
(progn
(setf rval nil)
(loop-finish))) ;; toss collector and return the common lisp false value
((equal str-val "true")
(progn
(setf rval T)
(loop-finish)))))))))
;; in array mode, build the vector in a loop until we hit a "]". Then return the vector.
((eq mode +in-array+)
(progn
(loop for ch = (peek-next-char)
do
(cond
((null ch)
(raise-error "invalid nil"))
((char= ch +right-bracket+)
(progn
(next-char) ;; consume the ] to account for it
(loop-finish)))
((delimiter? ch)
(next-char))
(T
(vector-push-extend (read-obj) collector))))
(setf rval
collector)
(loop-finish)))
;; in pairs mode (JSON object), build pairs of objects in the collector, then
;; build a hash-table and return.
((eq mode +in-pairs+)
(progn
(loop for ch = (peek-next-char)
with pair = nil
with k = nil
with v = nil
do
(cond
((null ch)
(raise-error "invalid nil"))
((delimiter? ch)
(next-char))
((char= ch +right-brace+)
(progn
(next-char)
(setf rval (make-hash-table :test 'equal))
(loop for (key value) across collector
do
(setf (gethash key rval)
value))
(loop-finish)))
(T
(setf k (read-obj +in-key+))
(cond
(use-keywords-for-keys
(setf k
(alexandria:make-keyword (str:replace-all "_" "-" (str:upcase k)))))
(object-key-handler
(setf k
(funcall object-key-handler k))))
(setf v (read-obj))
(setf pair (list k v))
(vector-push-extend pair
collector))))
(loop-finish)))
;; otherwise we need to collect the value and return the correct lisp type
(T
(cond
((delimiter? c)
(progn
(setf rval (map 'string (lambda (x) x) collector))
(cond
((equal rval "null")
(progn
(setf rval nil)
(loop-finish)))
((> (length collector) 0)
(raise-error (format nil "invalid symbol: ~A" rval)))
((and (null c)
(= 0 (length collector)))
(loop-finish))
((= 0 (length collector))
nil)
(T
(loop-finish)))))
;; JSON array
((char= +left-bracket+ c)
(progn
(setf rval (read-obj +in-array+))
(loop-finish)))
;; JSON Object
((char= +left-brace+ c)
(progn
(setf rval (read-obj +in-pairs+))
(loop-finish)))
;; there are a few special cases, numbers and booleans, which we accomodate here
((eq (length collector) 0)
(cond
((or (digit-char-p c)
(char= c +hyphen+)
(char= c +plus+))
(progn
(setf rval (read-obj +in-number+ c))
(loop-finish)))
((char= c +f-char+) (progn
(setf rval (read-obj +in-boolean+ c))
(loop-finish)))
((char= c +t-char+) (progn
(setf rval (read-obj +in-boolean+ c))
(loop-finish)))
((char= c +quote+) (progn
(setf rval (read-obj +in-string+))
(loop-finish)))
(T
(vector-push-extend c collector))))
((and (eq (length collector) 3)
(equal "nul" (map 'string (lambda (x) x) collector))
(char= c #\l))
(progn
(setf rval nil)
(loop-finish)))
;; finally, if we are here just add our character to our collector
(T
(vector-push-extend c collector)))))) ;; just add the character to our collector
;; return the collector based on our mode
(handler-case
(cond
((and (eq nil rval)
(or (eq mode +in-boolean+)
(eq mode +in-code+)))
(setf rval nil))
((not (null rval)) rval)
((and (eq mode +in-number+)
(or is-float?
exponent-present))
(setf rval (parse-float:parse-float (map 'string (lambda (x) x) collector))))
((eq mode +in-number+)
(progn
(setf rval (parse-integer (map 'string (lambda (x) x) collector)))))
(T
(setf rval (map 'string (lambda (x) x) collector))))
(error ()
(raise-error (format nil "invalid value"))))
(decf depth)
rval)))
(read-obj))))
;; Encoders ---------------------------------
;; Main entry
(defun stringify (data &key (case-encoder #'lisp-to-snakecase) unencodable-items)
"Converts the given data structure to a stringified JSON form, suitable for serialization and other uses.
An optional function can be passed for case encoding of native lisp keyword structures.
If a function for unencodable-items is provided, this function will be called, and should return a JSON
compliant string representing the encoded items. If no value is provided for unencodable-items, the
JSON null value is used."
(encode-entry data case-encoder unencodable-items))
;; The central dispatcher
;; Simple types are handled directly otherwise they have specific
;; handlers
(defun encode-entry (entry &optional (case-encoder #'lisp-to-snakecase) unencodable-items)
(cond
((typep entry 'STRING)
(encode-string entry))
((typep entry 'KEYWORD)
(write-to-string (keyword-to-string entry case-encoder)))
((typep entry 'FLOAT)
(format nil "~F" entry))
((typep entry 'INTEGER)
(write-to-string entry))
((typep entry 'RATIONAL)
(format nil "~F" entry))
((typep entry 'NULL)
"null")
((typep entry 'BOOLEAN)
"true")
((typep entry 'HASH-TABLE)
(encode-hash-table entry case-encoder unencodable-items))
((and (typep entry 'LIST)
(not (typep (rest entry) 'CONS)))
(encode-cons entry case-encoder unencodable-items))
((typep entry 'LIST)
(encode-list entry case-encoder unencodable-items))
((typep entry 'VECTOR)
(encode-vector entry case-encoder unencodable-items))
(T
(if unencodable-items
(let
((encoded-value (funcall unencodable-items)))
(if (typep encoded-value 'string)
encoded-value
(error (format nil "non-string value returned from unencodable-items: ~A"
(type-of encoded-value)))))
"null"))))
(defun lisp-to-snakecase (text)
(str:snake-case text))
(defun snakecase-to-lisp (text)
(str:param-case text))
(defun lisp-to-camelcase (text)
(str:camel-case text))
(defun keyword-to-string (kw &optional (case-encoder #'lisp-to-snakecase))
(funcall case-encoder
(str:downcase (format nil "~A" kw))))
(defun string-to-symbol (text &key (case-encoder #'snakecase-to-lisp) package)
(if package
(intern (str:upcase (funcall case-encoder text))
package)
(intern (str:upcase (funcall case-encoder text)))))
(defun encode-string (text)
(let
((collector (make-array (length text) :initial-element +quote+ :element-type 'character :adjustable T :fill-pointer 1))
(cnum nil))
(if (= (length text) 0)
"\"\""
(progn
(loop for c across text
do
(setf cnum
(char-code c))
(if (char= c +quote+)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend +quote+ collector))
(if (or (< cnum 32)
(= cnum 92))
;; if it is a special char, encode it appropriately, otherwise discard it
(cond
((= cnum 10)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend #\n collector)))
((= cnum 9)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend #\t collector)))
((= cnum 12)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend #\f collector)))
((= cnum 13)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend #\r collector)))
((= cnum 92)
(progn
(vector-push-extend +escape-char+ collector)
(vector-push-extend +escape-char+ collector))))
(vector-push-extend c collector))))
(vector-push-extend +quote+ collector)
(babel:octets-to-string (babel:string-to-octets collector) :encoding :utf-8)))))
;; (map 'string (lambda (x) x) collector)))
;; Container Encoding
;; Vectors and Arrays, Lists and Hash Tables
(defun encode-vector(vect &optional (case-encoder #'lisp-to-snakecase) unencodable-items)
(let
((encoded-items nil))
(setf encoded-items
(loop for entry across vect
collect (encode-entry entry case-encoder unencodable-items)))
(str:concat "["
(str:join ", " encoded-items)
"]")))
(defun encode-list (items &optional (case-encoder #'lisp-to-snakecase) unencodable-items)
(let
((encoded-items nil))
(setf encoded-items
(loop for entry in items
collect
(encode-entry entry case-encoder unencodable-items)))
(str:concat "["
(str:join ", " encoded-items)
"]")))
(defun encode-cons (items &optional (case-encoder #'lisp-to-snakecase) unencodable-items)
(let
((encoded-items nil))
(setf encoded-items
(list (encode-entry (first items) case-encoder unencodable-items)
(encode-entry (rest items) case-encoder unencodable-items)))
(str:concat "["
(str:join ", " encoded-items)
"]")))
(defun encode-hash-table (table &optional (case-encoder #'lisp-to-snakecase) unencodable-items)
(let
((encoded-values nil))
(setf encoded-values
(loop for k being the hash-keys of table
using (hash-value v)
collect
(list (encode-entry k case-encoder unencodable-items)
(encode-entry v))))
(str:concat "{"
(if (> (length encoded-values) 0)
(str:join ", "
(loop for (k v) in encoded-values
collect (str:concat k ": " v)))
"")
"}")))