-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathjson.sls
More file actions
201 lines (188 loc) · 9.59 KB
/
json.sls
File metadata and controls
201 lines (188 loc) · 9.59 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
;; JSON implementation for Scheme
;; See http://www.json.org/ or http://www.crockford.com/JSON/index.html
;;
;; Copyright (c) 2005 Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
;; Copyright (c) 2005 LShift Ltd. <query@lshift.net>
;;
;; 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.
;; SPDX-License-Identifier: MIT
#!r6rs
(library (json)
(export json-write
json-read)
(import
(except (rnrs) define-record-type error)
(rnrs r5rs)
(rnrs mutable-pairs)
(only (srfi :1 lists) lset-union append-map fold)
(srfi :6 basic-string-ports)
(srfi :9 records)
(srfi :23 error)
(only (chezscheme) include)
(packrat))
(define json-write
(let ()
(define (write-ht vec p)
(display "{" p)
(do ((need-comma #f #t)
(vec vec (cdr vec)))
((null? vec))
(if need-comma
(display ", " p)
(set! need-comma #t))
(let* ((entry (car vec))
(k (car entry))
(v (cdr entry)))
(cond
((symbol? k) (write (symbol->string k) p))
((string? k) (write k p)) ;; for convenience
(else (error "Invalid JSON table key in json-write" k)))
(display ": " p)
(write-any v p)))
(display "}" p))
(define (write-array a p)
(display "[" p)
(let ((need-comma #f))
(for-each (lambda (v)
(if need-comma
(display ", " p)
(set! need-comma #t))
(write-any v p))
a))
(display "]" p))
(define (write-any x p)
(cond
((or (string? x)
(number? x)) (write x p))
((boolean? x) (display (if x "true" "false") p))
((symbol? x) (write (if (eq? x 'null) 'null (symbol->string x))
p)) ;; for convenience
((null? x) (display "null" p))
((and (list? x)
(pair? (car x))
(not (pair? (caar x))))
(write-ht x p))
((list? x) (write-array x p))
(else (error "Invalid JSON object in json-write" x))))
(lambda (x . maybe-port)
(write-any x (if (pair? maybe-port) (car maybe-port) (current-output-port))))))
(define json-read
(let ()
(define (generator p)
(let ((ateof #f)
(pos (top-parse-position "<?>")))
(lambda ()
(if ateof
(values pos #f)
(let ((x (read-char p)))
(if (eof-object? x)
(begin
(set! ateof #t)
(values pos #f))
(let ((old-pos pos))
(set! pos (update-parse-position pos x))
(values old-pos (cons x x)))))))))
(define parser
(packrat-parser (begin
(define (white results)
(if (char-whitespace? (parse-results-token-value results))
(white (parse-results-next results))
(comment results)))
(define (skip-comment-char results)
(comment-body (parse-results-next results)))
(define (skip-to-newline results)
(if (memv (parse-results-token-value results) '(#\newline #\return))
(white results)
(skip-to-newline (parse-results-next results))))
(define (token str)
(lambda (starting-results)
(let loop ((pos 0) (results starting-results))
(if (= pos (string-length str))
(make-result str results)
(if (char=? (parse-results-token-value results) (string-ref str pos))
(loop (+ pos 1) (parse-results-next results))
(make-expected-result (parse-results-position starting-results) str))))))
(define (interpret-string-escape results k)
(let ((ch (parse-results-token-value results)))
(k (cond
((assv ch '((#\b . #\backspace)
(#\n . #\newline)
(#\f . #\page)
(#\r . #\return)
(#\t . #\tab))) => cdr) ;; we don't support the "u" escape for unicode
(else ch))
(parse-results-next results))))
(define (jstring-body results)
(let loop ((acc '()) (results results))
(let ((ch (parse-results-token-value results)))
(case ch
((#\\) (interpret-string-escape (parse-results-next results)
(lambda (val results)
(loop (cons val acc) results))))
((#\") (make-result (list->string (reverse acc)) results))
(else (loop (cons ch acc) (parse-results-next results)))))))
(define (jnumber-body starting-results)
(let loop ((acc '()) (results starting-results))
(let ((ch (parse-results-token-value results)))
(if (memv ch '(#\- #\+ #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\. #\e #\E))
(loop (cons ch acc) (parse-results-next results))
(let ((n (string->number (list->string (reverse acc)))))
(if n
(make-result n results)
(make-expected-result (parse-results-position starting-results) 'number)))))))
any)
(any ((white '#\{ entries <- table-entries white '#\}) entries)
((white '#\[ entries <- array-entries white '#\]) entries)
((s <- jstring) s)
((n <- jnumber) n)
((white (token "true")) #t)
((white (token "false")) #f)
((white (token "null")) '()))
(comment (((token "/*") b <- comment-body) b)
(((token "//") b <- skip-to-newline) b)
(() 'whitespace))
(comment-body (((token "*/") w <- white) w)
((skip-comment-char) 'skipped-comment-char))
(table-entries ((a <- table-entries-nonempty) a)
(() '()))
(table-entries-nonempty ((entry <- table-entry white '#\, entries <- table-entries-nonempty) (cons entry entries))
((entry <- table-entry) (list entry)))
(table-entry ((key <- jstring white '#\: val <- any) (cons (string->symbol key) val)))
(array-entries ((a <- array-entries-nonempty) a)
(() '()))
(array-entries-nonempty ((entry <- any white '#\, entries <- array-entries-nonempty) (cons entry entries))
((entry <- any) (list entry)))
(jstring ((white '#\" body <- jstring-body '#\") body))
(jnumber ((white body <- jnumber-body) body))
))
(define (read-any p)
(let ((result (parser (base-generator->results (generator p)))))
(if (parse-result-successful? result)
(parse-result-semantic-value result)
(error "JSON Parse Error"
(let ((e (parse-result-error result)))
(list 'json-parse-error
(parse-position->string (parse-error-position e))
(parse-error-expected e)
(parse-error-messages e)))))))
(lambda maybe-port
(read-any (if (pair? maybe-port) (car maybe-port) (current-input-port))))))
)