Fix json treatment of eof, plus tests.

(Also, no need for a keyword in the interna function.)
This commit is contained in:
Eli Barzilay 2012-10-05 11:48:26 -04:00
parent c26faaebfa
commit 73526b449b
2 changed files with 8 additions and 5 deletions

View File

@ -156,9 +156,10 @@
(list (string->symbol k) (read-json)))
(apply hasheq (apply append (read-list 'object #rx#"^}" read-pair))))
;;
(define (read-json #:eof-ok? [eof-ok? #false])
(define (read-json [top? #f])
(skip-whitespace)
(cond
[(and top? (eof-object? (peek-char i))) eof]
[(regexp-try-match #px#"^true\\b" i) #t]
[(regexp-try-match #px#"^false\\b" i) #f]
[(regexp-try-match #px#"^null\\b" i) jsnull]
@ -171,10 +172,9 @@
(cond [(equal? m #"\"") (read-string)]
[(equal? m #"[") (read-list 'array #rx#"^\\]" read-json)]
[(equal? m #"{") (read-hash)])))]
[(and eof-ok? (regexp-try-match #px#"$" i)) eof]
[else (err "bad input")]))
;;
(read-json #:eof-ok? #true))
(read-json #t))
;; ----------------------------------------------------------------------------
;; Convenience functions

View File

@ -100,8 +100,11 @@
(open-input-string
@T{ 1 [2,3] "four" }))])
je)
=>
'(1 (2 3) "four")
=> '(1 (2 3) "four")
(string->jsexpr "]") =error> "read-json:"
(string->jsexpr "foo") =error> "read-json:"
(string->jsexpr "") => eof
(string->jsexpr " \t\r\n") => eof
))
(test do (pred-tests)