| 1 |
;;; |
| 2 |
;;; json.scm - JSON (RFC7159) Parser |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2006 Rui Ueyama ([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 |
;;; http://www.ietf.org/rfc/rfc7159.txt |
| 35 |
|
| 36 |
(define-module rfc.json |
| 37 |
(use gauche.sequence) |
| 38 |
(use gauche.generator) |
| 39 |
(use gauche.unicode) |
| 40 |
(use scheme.charset) |
| 41 |
(use parser.peg) |
| 42 |
(use srfi.13) |
| 43 |
(use srfi.113) |
| 44 |
(export <json-parse-error> <json-construct-error> |
| 45 |
<json-mixin> |
| 46 |
|
| 47 |
parse-json parse-json-string |
| 48 |
parse-json* |
| 49 |
construct-json construct-json-string |
| 50 |
|
| 51 |
json-array-handler json-object-handler json-special-handler |
| 52 |
json-nesting-depth-limit |
| 53 |
|
| 54 |
json-parser json-tokenizer |
| 55 |
)) |
| 56 |
(select-module rfc.json) |
| 57 |
|
| 58 |
;; NB: We have <json-parse-error> independent from <parse-error> for |
| 59 |
;; now, since parser.peg's interface may be changed later. |
| 60 |
(define-condition-type <json-parse-error> <error> #f |
| 61 |
(position) ;stream position |
| 62 |
(objects)) ;offending object(s) or messages |
| 63 |
|
| 64 |
(define-condition-type <json-construct-error> <error> #f |
| 65 |
(object)) ;offending object |
| 66 |
|
| 67 |
(define json-array-handler (make-parameter list->vector)) |
| 68 |
(define json-object-handler (make-parameter identity)) |
| 69 |
(define json-special-handler (make-parameter identity)) |
| 70 |
|
| 71 |
(define (build-array elts) ((json-array-handler) elts)) |
| 72 |
(define (build-object pairs) ((json-object-handler) pairs)) |
| 73 |
(define (build-special symbol) ((json-special-handler) symbol)) |
| 74 |
|
| 75 |
;; This is to bail out the input nesting is too deep (see SRFI-180) |
| 76 |
(define json-nesting-depth-limit (make-parameter +inf.0)) |
| 77 |
(define json-current-nesting-depth (make-parameter 0)) |
| 78 |
|
| 79 |
;; JSON-serializable class. |
| 80 |
;; In fact, we can just use object inspection to provide the default |
| 81 |
;; serializer without a specialized mixin class. However, such implicit |
| 82 |
;; handling tends to be error-prone. We'd rather be explicit. |
| 83 |
;; In a class inheriting <json-mixin>, a slot can have :json-name |
| 84 |
;; to be serializable. |
| 85 |
(define-class <json-mixin> () ()) |
| 86 |
|
| 87 |
|
| 88 |
;;;============================================================ |
| 89 |
;;; Parser |
| 90 |
;;; |
| 91 |
(define %ws ($many_ ($. #[ \t\r\n]))) |
| 92 |
|
| 93 |
(define %begin-array ($seq ($. #\[) %ws)) |
| 94 |
(define %begin-object ($seq ($. #\{) %ws)) |
| 95 |
(define %end-array ($seq ($. #\]))) |
| 96 |
(define %end-object ($seq ($. #\}))) |
| 97 |
(define %name-separator ($seq ($. #\:) %ws)) |
| 98 |
(define %value-separator ($seq ($. #\,) %ws)) |
| 99 |
|
| 100 |
(define %special |
| 101 |
($lift ($ build-special $ string->symbol $ rope-finalize $) |
| 102 |
($or ($. "false") ($. "true") ($. "null")))) |
| 103 |
|
| 104 |
(define %value |
| 105 |
($lazy ($or %special %object %array %number %string))) |
| 106 |
|
| 107 |
(define ($depth-check parser) |
| 108 |
(^s (let1 d (json-current-nesting-depth) |
| 109 |
(if (>= d (json-nesting-depth-limit)) |
| 110 |
(error <json-parse-error> |
| 111 |
"Input JSON nesting is too deep.") |
| 112 |
(parameterize ([json-current-nesting-depth (+ d 1)]) |
| 113 |
(parser s)))))) |
| 114 |
|
| 115 |
(define %array |
| 116 |
($depth-check |
| 117 |
($lift ($ build-array $ rope-finalize $) |
| 118 |
($between %begin-array |
| 119 |
($sep-by ($seq0 %value %ws) %value-separator) |
| 120 |
%end-array)))) |
| 121 |
|
| 122 |
(define %sign ($optional ($. #[+-]))) |
| 123 |
(define %digits ($many1 ($. #[\d]))) |
| 124 |
|
| 125 |
(define %number |
| 126 |
($binding ($: sign %sign) |
| 127 |
($: digits %digits) |
| 128 |
($: frac ($optional ($->rope ($. #\.) ($many1 ($. #[\d]))))) |
| 129 |
($: exp ($optional ($->rope ($. #[eE]) %sign %digits))) |
| 130 |
(string->number (rope->string (list sign digits frac exp))))) |
| 131 |
|
| 132 |
(define %unicode |
| 133 |
(let ([%hex4 ($lift (^s (string->number (list->string s) 16)) |
| 134 |
($many ($. #[\da-fA-F]) 4 4))]) |
| 135 |
($let ([ ($. #\u) ] |
| 136 |
[c %hex4]) |
| 137 |
(cond [(<= #xd800 c #xdbff) |
| 138 |
($or ($try ($let ([ ($. "\\u") ] |
| 139 |
[c2 %hex4]) |
| 140 |
(receive (cc x) |
| 141 |
(utf16->ucs4 `(,c ,c2) 'permissive) |
| 142 |
(and (null? x) |
| 143 |
($return (ucs->char cc)))))) |
| 144 |
($raise (format "unpaired high surrogate: \\u~4,'0x" c)))] |
| 145 |
[(<= #xdc00 c #xdfff) |
| 146 |
($raise (format "unpaired low surrogate: \\u~4,'0x" c))] |
| 147 |
[else ($return (ucs->char c))])))) |
| 148 |
|
| 149 |
(define %string |
| 150 |
(let* ([%dquote ($. #\")] |
| 151 |
[%escape ($. #\\)] |
| 152 |
[%special-char |
| 153 |
($seq %escape |
| 154 |
($or ($. #\") |
| 155 |
($. #\\) |
| 156 |
($. #\/) |
| 157 |
($seq ($. #\b) ($return #\x08)) |
| 158 |
($seq ($. #\f) ($return #\page)) |
| 159 |
($seq ($. #\n) ($return #\newline)) |
| 160 |
($seq ($. #\r) ($return #\return)) |
| 161 |
($seq ($. #\t) ($return #\tab)) |
| 162 |
%unicode))] |
| 163 |
[%unescaped ($none-of #[\"])] |
| 164 |
[%body-char ($or %special-char %unescaped)] |
| 165 |
[%string-body ($->rope ($many %body-char))]) |
| 166 |
($between %dquote %string-body %dquote))) |
| 167 |
|
| 168 |
(define %object |
| 169 |
(let1 %member ($binding ($: k %string) |
| 170 |
%ws |
| 171 |
%name-separator |
| 172 |
($: v %value) |
| 173 |
%ws |
| 174 |
(cons k v)) |
| 175 |
($depth-check |
| 176 |
($between %begin-object |
| 177 |
($lift ($ build-object $ rope-finalize $) |
| 178 |
($sep-by %member %value-separator)) |
| 179 |
%end-object)))) |
| 180 |
|
| 181 |
(define json-parser ($parameterize ([json-current-nesting-depth 0]) |
| 182 |
($seq %ws ($or ($eos) %value)))) |
| 183 |
|
| 184 |
;; for streaming parser |
| 185 |
(define json-tokenizer |
| 186 |
($seq %ws |
| 187 |
($or %special %number %string |
| 188 |
($seq %begin-array ($return 'array-start)) |
| 189 |
($seq %end-array ($return 'array-end)) |
| 190 |
($seq %begin-object ($return 'object-start)) |
| 191 |
($seq %end-object ($return 'object-end)) |
| 192 |
($seq %name-separator ($return #\:)) |
| 193 |
($seq %value-separator ($return #\,))))) |
| 194 |
|
| 195 |
;; entry point |
| 196 |
(define (parse-json :optional (port (current-input-port))) |
| 197 |
(guard (e [(<parse-error> e) |
| 198 |
;; not to expose parser.peg's <parse-error>. |
| 199 |
(error <json-parse-error> |
| 200 |
:position (~ e'position) :objects (~ e'objects) |
| 201 |
:message (condition-message e e))]) |
| 202 |
(peg-parse-port json-parser port))) |
| 203 |
|
| 204 |
(define (parse-json-string str) |
| 205 |
(call-with-input-string str (cut parse-json <>))) |
| 206 |
|
| 207 |
(define (parse-json* :optional (port (current-input-port))) |
| 208 |
(guard (e [(<parse-error> e) |
| 209 |
;; not to expose parser.peg's <parse-error>. |
| 210 |
(error <json-parse-error> |
| 211 |
:position (~ e'position) :objects (~ e'objects) |
| 212 |
:message (~ e'message))]) |
| 213 |
(generator->list (peg-parser->generator json-parser port)))) |
| 214 |
|
| 215 |
;;;============================================================ |
| 216 |
;;; Writer |
| 217 |
;;; |
| 218 |
|
| 219 |
(define (print-value obj) |
| 220 |
(cond [(or (eq? obj 'false) (eq? obj #f)) (display "false")] |
| 221 |
[(or (eq? obj 'true) (eq? obj #t)) (display "true")] |
| 222 |
[(eq? obj 'null) (display "null")] |
| 223 |
[(list? obj) (print-object obj)] |
| 224 |
[(string? obj) (print-string obj)] |
| 225 |
[(number? obj) (print-number obj)] |
| 226 |
[(is-a? obj <dictionary>) (print-object obj)] |
| 227 |
[(is-a? obj <sequence>) (print-array obj)] |
| 228 |
[(is-a? obj <json-mixin>) (print-instance obj)] |
| 229 |
[else (error <json-construct-error> :object obj |
| 230 |
"can't convert Scheme object to json:" obj)])) |
| 231 |
|
| 232 |
(define (print-object obj) |
| 233 |
(write-char #\{) |
| 234 |
(fold (^[attr comma-needed?] |
| 235 |
(unless (pair? attr) |
| 236 |
(error <json-construct-error> :object obj |
| 237 |
"construct-json needs an assoc list or dictionary, \ |
| 238 |
but got:" obj)) |
| 239 |
(when comma-needed? |
| 240 |
(write-char #\,)) |
| 241 |
(print-string (x->string (car attr))) |
| 242 |
(write-char #\:) |
| 243 |
(print-value (cdr attr)) |
| 244 |
#t) |
| 245 |
#f obj) |
| 246 |
(write-char #\})) |
| 247 |
|
| 248 |
(define (print-array obj) |
| 249 |
(write-char #\[) |
| 250 |
(for-each-with-index (^[i val] |
| 251 |
(unless (zero? i) (write-char #\,)) |
| 252 |
(print-value val)) |
| 253 |
obj) |
| 254 |
(write-char #\])) |
| 255 |
|
| 256 |
(define (print-instance obj) ;<json-mixin> |
| 257 |
(let1 class (class-of obj) |
| 258 |
(write-char #\{) |
| 259 |
(fold (^[slot comma-needed?] |
| 260 |
(if-let1 json-name (slot-definition-option slot :json-name #f) |
| 261 |
(begin |
| 262 |
(when comma-needed? |
| 263 |
(write-char #\,)) |
| 264 |
(print-string (if (eqv? json-name #t) |
| 265 |
(x->string (slot-definition-name slot)) |
| 266 |
(x->string json-name))) |
| 267 |
(write-char #\:) |
| 268 |
(print-value (slot-ref obj (slot-definition-name slot))) |
| 269 |
#t) |
| 270 |
comma-needed?)) |
| 271 |
#f (class-slots class)) |
| 272 |
(write-char #\}))) |
| 273 |
|
| 274 |
(define (print-number num) |
| 275 |
(cond [(or (not (real? num)) (not (finite? num))) |
| 276 |
(error <json-construct-error> :object num |
| 277 |
"json cannot represent a number" num)] |
| 278 |
[(and (rational? num) (not (integer? num))) |
| 279 |
(write (exact->inexact num))] |
| 280 |
[else (write num)])) |
| 281 |
|
| 282 |
(define (print-string str) |
| 283 |
(define specials |
| 284 |
'((#\" . #\") (#\\ . #\\) (#\x08 . #\b) (#\page . #\f) |
| 285 |
(#\newline . #\n) (#\return . #\r) (#\tab . #\t))) |
| 286 |
(define (hexescape code) (format #t "\\u~4,'0x" code)) |
| 287 |
(define (print-char c) |
| 288 |
(cond [(assv c specials) => (^p (write-char #\\) (write-char (cdr p)))] |
| 289 |
[(and (char-set-contains? char-set:ascii c) |
| 290 |
(not (eq? (char-general-category c) 'Cc))) |
| 291 |
(write-char c)] |
| 292 |
[else |
| 293 |
(let1 code (char->ucs c) |
| 294 |
(if (>= code #x10000) |
| 295 |
(for-each hexescape (ucs4->utf16 code)) |
| 296 |
(hexescape code)))])) |
| 297 |
(write-char #\") |
| 298 |
(string-for-each print-char str) |
| 299 |
(write-char #\")) |
| 300 |
|
| 301 |
(define (construct-json x :optional (oport (current-output-port))) |
| 302 |
(with-output-to-port oport |
| 303 |
(^() (print-value x)))) |
| 304 |
|
| 305 |
(define (construct-json-string x) |
| 306 |
(call-with-output-string (cut construct-json x <>))) |