Commits

Anonymous committed 7803294

Add consume-trailing-whitespace keyword option

This is #t by default. Passing #t leaves trailing whitespace in the
input stream. This allows for read-json to be used e.g. in streaming
contexts.

  • Participants
  • Parent commits 19e9030

Comments (0)

Files changed (2)

 (define value
   (enclosed-by ws (any-of null false true object (array) number string) ws))
 
+(define (trailing-ws input)
+  (if (consume-trailing-whitespace?)
+      (ws input)
+      (cons #f input)))
+
 ;; document <- ws ( object / array ) ws
 
 (define document
-  (enclosed-by ws (any-of object (array)) ws))
+  (enclosed-by ws (any-of object (array)) trailing-ws))
 (define (json-char? c)
   (utf8-char-set-contains? char-set:json-char c))
 
+(define consume-trailing-whitespace?
+  (make-parameter #f))
+
 (include "grammar.scm")
 
-(define (read-json #!optional (input (current-input-port)) (memoize? #t))
-  (parse document
-         (cond ((input-port? input)
-                (input-port->lazy-seq input read-char))
-               ((string? input)
-                (string->list input))
-               (else input))
-         memoize: memoize?))
+(define (read-json #!optional (input (current-input-port))
+                   #!key (memoize? #t) (consume-trailing-whitespace #t))
+  (parameterize ((consume-trailing-whitespace? consume-trailing-whitespace))
+    (parse document
+           (cond ((input-port? input)
+                  (input-port->lazy-seq input read-char))
+                 ((string? input)
+                  (string->list input))
+                 (else input))
+           memoize: memoize?)))
 
 (define (unparse-string s)
   (let ((chars (string->list s)))