| 1 |
;;; |
| 2 |
;;; SRFI-180 - JSON |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; A wrapper of rfc.json |
| 35 |
|
| 36 |
(define-module srfi.180 |
| 37 |
(use gauche.vport) |
| 38 |
(use parser.peg) |
| 39 |
(use scheme.list) |
| 40 |
(use rfc.json) |
| 41 |
(export json-error? json-error-reason json-null? |
| 42 |
|
| 43 |
json-nesting-depth-limit ; in rfc.json |
| 44 |
json-number-of-character-limit |
| 45 |
|
| 46 |
json-generator json-fold |
| 47 |
json-read json-lines-read json-sequence-read |
| 48 |
json-accumulator json-write |
| 49 |
)) |
| 50 |
(select-module srfi.180) |
| 51 |
|
| 52 |
(define (json-error? obj) |
| 53 |
(or (is-a? obj <json-parse-error>) |
| 54 |
(is-a? obj <json-construct-error>))) |
| 55 |
|
| 56 |
(define (json-error-reason obj) |
| 57 |
(~ obj'message)) |
| 58 |
|
| 59 |
(define (json-null? obj) (eq? obj 'null)) |
| 60 |
|
| 61 |
;; json-nesting-depth-limit is in rfc.json |
| 62 |
|
| 63 |
(define json-number-of-character-limit (make-parameter +inf.0)) |
| 64 |
|
| 65 |
|
| 66 |
;; internal |
| 67 |
;; we use peek-char to keep the last read char in the port, since |
| 68 |
;; lseq read ahead one character and we don't want that character to |
| 69 |
;; be missed in the subsequent read from the port. |
| 70 |
(define (port/gen->json-lseq port) |
| 71 |
(define nchars 0) |
| 72 |
(if (port? port) |
| 73 |
(generator->lseq |
| 74 |
(^[] |
| 75 |
(unless (= nchars 0) (read-char port)) |
| 76 |
(let1 c (peek-char port) |
| 77 |
(cond [(eof-object? c) c] |
| 78 |
[(>= nchars (json-number-of-character-limit)) |
| 79 |
(error <json-parse-error> |
| 80 |
"Input length exceeds json-number-of-character-limit")] |
| 81 |
[else (inc! nchars) c])))) |
| 82 |
(generator->lseq port))) ;assume it's a char-generator |
| 83 |
|
| 84 |
;; API: streaming parser |
| 85 |
;; NB: srfi's json-generator doesn't take a char generator, but for |
| 86 |
;; the upper layers, we accept it for the convenience. |
| 87 |
(define (json-generator :optional (port (current-input-port))) |
| 88 |
(define inner-gen |
| 89 |
(peg-parser->generator json-tokenizer (port/gen->json-lseq port))) |
| 90 |
(define (nexttok) |
| 91 |
(guard (e ([<parse-error> e] |
| 92 |
;; not to expose parser.peg's <parse-error>. |
| 93 |
(error <json-parse-error> |
| 94 |
:position (~ e'position) :objects (~ e'objects) |
| 95 |
:message (~ e'message)))) |
| 96 |
(case (inner-gen) |
| 97 |
[(true) #t] |
| 98 |
[(false) #f] |
| 99 |
[else => identity]))) |
| 100 |
(define nesting '()) |
| 101 |
(define (push-nesting kind) |
| 102 |
(when (>= (length nesting) (json-nesting-depth-limit)) |
| 103 |
(error <json-parse-error> "Input JSON nesting is too deep.")) |
| 104 |
(push! nesting (cons kind gen))) |
| 105 |
(define (pop-nesting kind) |
| 106 |
(unless (pair? nesting) |
| 107 |
(errorf <json-parse-error> "Stray close ~a" kind)) |
| 108 |
(unless (eq? (caar nesting) kind) |
| 109 |
(errorf <json-parse-error> "Unmatched open ~a" (car nesting))) |
| 110 |
(set! gen (cdr (pop! nesting)))) |
| 111 |
(define (badtok tok) |
| 112 |
(error <json-parse-error> (format "Invalid token: ~s" tok))) |
| 113 |
|
| 114 |
;; Read one value. array-end and object-end are returned as-is, and the |
| 115 |
;; caller should handle it---they can appear in value context input is empty |
| 116 |
;; array and empty object. |
| 117 |
(define (value) |
| 118 |
(case (nexttok) |
| 119 |
[(array-start) |
| 120 |
(push-nesting 'array) |
| 121 |
(set! gen array-element) |
| 122 |
'array-start] |
| 123 |
[(object-start) |
| 124 |
(push-nesting 'object) |
| 125 |
(set! gen object-key) |
| 126 |
'object-start] |
| 127 |
[(#\: #\,) => badtok] |
| 128 |
[else => identity])) |
| 129 |
|
| 130 |
;; State machine |
| 131 |
;; Each state is a thunk, set! to variable 'gen'. |
| 132 |
|
| 133 |
;; Initial state |
| 134 |
(define (init) |
| 135 |
(set! gen fini) |
| 136 |
(case (value) |
| 137 |
[(array-end object-end) => badtok] |
| 138 |
[else => identity])) |
| 139 |
|
| 140 |
;; We've already read a whole item. |
| 141 |
(define (fini) (eof-object)) |
| 142 |
|
| 143 |
;; Reading an array element. |
| 144 |
(define (array-element) |
| 145 |
(set! gen array-element-after) |
| 146 |
(case (value) |
| 147 |
[(array-end) (pop-nesting 'array) 'array-end] |
| 148 |
[(object-end) => badtok] |
| 149 |
[else => identity])) |
| 150 |
|
| 151 |
;; Just read an array element. |
| 152 |
(define (array-element-after) |
| 153 |
(case (nexttok) |
| 154 |
[(#\,) (array-element)] |
| 155 |
[(array-end) (pop-nesting 'array) 'array-end] |
| 156 |
[else => badtok])) |
| 157 |
|
| 158 |
;; Reading an object key |
| 159 |
(define (object-key) |
| 160 |
(let1 t (nexttok) |
| 161 |
(cond [(string? t) (set! gen object-key-after) t] |
| 162 |
[(eq? t 'object-end) (pop-nesting 'object) 'object-end] |
| 163 |
[else (badtok t)]))) |
| 164 |
|
| 165 |
;; Just read an object key |
| 166 |
(define (object-key-after) |
| 167 |
(case (nexttok) |
| 168 |
[(#\:) |
| 169 |
(set! gen object-value-after) |
| 170 |
(case (value) |
| 171 |
[(array-end object-end) => badtok] |
| 172 |
[else => identity])] |
| 173 |
[else => badtok])) |
| 174 |
|
| 175 |
;; Just read an object value |
| 176 |
(define (object-value-after) |
| 177 |
(case (nexttok) |
| 178 |
[(#\,) (object-key)] |
| 179 |
[(object-end) (pop-nesting 'object) 'object-end] |
| 180 |
[else => badtok])) |
| 181 |
|
| 182 |
;; 'gen' will be set! to the next state handler. |
| 183 |
(define gen init) |
| 184 |
;; Entry point |
| 185 |
(^[] (gen))) |
| 186 |
|
| 187 |
;; API |
| 188 |
(define (json-fold proc array-start array-end object-start object-end seed |
| 189 |
:optional (port-or-generator (current-input-port))) |
| 190 |
(define gen (json-generator port-or-generator)) |
| 191 |
(define (rec seed stack) |
| 192 |
(let1 tok (gen) |
| 193 |
(cond [(eof-object? tok) seed] |
| 194 |
[(eq? tok 'array-start) (rec (array-start seed) (cons seed stack))] |
| 195 |
[(eq? tok 'array-end) (rec (proc (array-end seed) (car stack)) |
| 196 |
(cdr stack))] |
| 197 |
[(eq? tok 'object-start) (rec (object-start seed) |
| 198 |
(cons seed stack))] |
| 199 |
[(eq? tok 'object-end) (rec (proc (object-end seed) (car stack)) |
| 200 |
(cdr stack))] |
| 201 |
[else (rec (proc tok seed) stack)]))) |
| 202 |
(rec seed '())) |
| 203 |
|
| 204 |
;; Common utility to adapt rfc.json with SRFI-180 API. We skip |
| 205 |
;; json-fold/json-generator stuff entirely. |
| 206 |
(define (with-json-parser proc lseq) |
| 207 |
(parameterize ((json-special-handler (^x (case x |
| 208 |
[(true) #t] |
| 209 |
[(false) #f] |
| 210 |
[else x]))) |
| 211 |
(json-object-handler (^x (map (^p `(,(string->symbol (car p)) |
| 212 |
. ,(cdr p))) |
| 213 |
x)))) |
| 214 |
(guard (e ([<parse-error> e] |
| 215 |
;; not to expose parser.peg's <parse-error>. |
| 216 |
(error <json-parse-error> |
| 217 |
:position (~ e'position) :objects (~ e'objects) |
| 218 |
:message (~ e'message)))) |
| 219 |
(proc lseq)))) |
| 220 |
|
| 221 |
;; API |
| 222 |
;; We skip json-fold/json-generator stuff entirely. |
| 223 |
(define (json-read :optional (port-or-generator (current-input-port))) |
| 224 |
(with-json-parser |
| 225 |
(^s (values-ref (peg-run-parser json-parser s) 0)) |
| 226 |
(port/gen->json-lseq port-or-generator))) |
| 227 |
|
| 228 |
;; API |
| 229 |
(define (json-lines-read :optional (port-or-generator (current-input-port))) |
| 230 |
(let1 lseq (port/gen->json-lseq port-or-generator) |
| 231 |
(^[] |
| 232 |
(with-json-parser |
| 233 |
(^s (receive (r next) (peg-run-parser json-parser s) |
| 234 |
(set! lseq next) |
| 235 |
r)) |
| 236 |
lseq)))) |
| 237 |
|
| 238 |
;; API |
| 239 |
;; <json-sequence> : ( #x1e json-text )* |
| 240 |
;; Skip unparsable json-text. |
| 241 |
(define (json-sequence-read :optional (port-or-generator (current-input-port))) |
| 242 |
(define (skip-to-rs lseq) |
| 243 |
(drop-while (^c (not (eqv? c #\x1e))) lseq)) |
| 244 |
(define (skip-rs lseq) |
| 245 |
(drop-while (^c (eqv? c #\x1e)) lseq)) |
| 246 |
(define (fetch lseq) |
| 247 |
(let1 start (skip-rs lseq) |
| 248 |
(if (null? start) |
| 249 |
(values (eof-object) '()) |
| 250 |
(receive (json next) |
| 251 |
(guard (e [(<json-parse-error> e) |
| 252 |
(values 'error (skip-to-rs start))]) |
| 253 |
(with-json-parser (cut peg-run-parser json-parser <>) start)) |
| 254 |
(if (eq? json 'error) |
| 255 |
(fetch next) |
| 256 |
(values json next)))))) |
| 257 |
(let1 lseq (skip-to-rs (port/gen->json-lseq port-or-generator)) |
| 258 |
(^[] (receive (json next) (fetch lseq) |
| 259 |
(set! lseq next) |
| 260 |
json)))) |
| 261 |
|
| 262 |
;; API |
| 263 |
(define (json-accumulator port-or-accumulator) |
| 264 |
(define acc (if (port? port-or-accumulator) |
| 265 |
(^o (if (string? o) |
| 266 |
(write-string o port-or-accumulator) |
| 267 |
(write-char o port-or-accumulator))) |
| 268 |
port-or-accumulator)) |
| 269 |
(define (err-input obj) |
| 270 |
(error <json-construct-error> |
| 271 |
:message (format "Unexpected token ~a as json-accumulator input" |
| 272 |
obj))) |
| 273 |
(define stack '()) ; (kind . #<proc>) |
| 274 |
|
| 275 |
(define (push-state!) (push! stack gen)) |
| 276 |
(define (pop-state! tok) |
| 277 |
(when (null? stack) (err-input tok)) |
| 278 |
(set! gen (pop! stack))) |
| 279 |
|
| 280 |
(define (value obj) |
| 281 |
(cond |
| 282 |
[(eof-object? obj)] |
| 283 |
[(eq? obj 'array-start) (acc #\[) (push-state!) (set! gen a0)] |
| 284 |
[(eq? obj 'object-start) (acc #\{) (push-state!) (set! gen k0)] |
| 285 |
[(or (eq? obj 'null) (boolean? obj) |
| 286 |
(string? obj) (number? obj)) |
| 287 |
(acc (construct-json-string obj))] |
| 288 |
[else (err-input obj)])) |
| 289 |
|
| 290 |
(define (init obj) |
| 291 |
(set! gen fini) |
| 292 |
(value obj)) |
| 293 |
|
| 294 |
(define (fini obj) |
| 295 |
(unless (eof-object? obj) |
| 296 |
(error <json-construct-error> |
| 297 |
:message "json-accumulator no longer accepting events."))) |
| 298 |
|
| 299 |
;; initial element of array |
| 300 |
(define (a0 obj) |
| 301 |
(if (eq? obj 'array-end) |
| 302 |
(begin (acc #\]) (pop-state! obj)) |
| 303 |
(begin (set! gen a1) (value obj)))) |
| 304 |
|
| 305 |
;; subsequent element of array |
| 306 |
(define (a1 obj) |
| 307 |
(if (eq? obj 'array-end) |
| 308 |
(begin (acc #\]) (pop-state! obj)) |
| 309 |
(begin (acc #\,) (value obj)))) |
| 310 |
|
| 311 |
;; initial key of object |
| 312 |
(define (k0 obj) |
| 313 |
(cond |
| 314 |
[(eq? obj 'object-end) (acc #\}) (pop-state! obj)] |
| 315 |
[(string? obj) (acc obj) (set! gen v)] |
| 316 |
[else (err-input obj)])) |
| 317 |
|
| 318 |
;; value of object |
| 319 |
(define (v obj) |
| 320 |
(set! gen k1) |
| 321 |
(acc #\:) |
| 322 |
(value obj)) |
| 323 |
|
| 324 |
;; subsequent key of object |
| 325 |
(define (k1 obj) |
| 326 |
(cond |
| 327 |
[(eq? obj 'object-end) (acc #\}) (pop-state! obj)] |
| 328 |
[(string? obj) (acc #\,) (acc obj) (set! gen v)] |
| 329 |
[else (err-input obj)])) |
| 330 |
|
| 331 |
(define gen init) |
| 332 |
(^[obj] (gen obj))) |
| 333 |
|
| 334 |
;; API |
| 335 |
(define (json-write obj :optional (port-or-accumulator (current-output-port))) |
| 336 |
(construct-json obj (if (port? port-or-accumulator) |
| 337 |
port-or-accumulator |
| 338 |
(open-output-accumulator port-or-accumulator)))) |