Fix json treatment of eof, plus tests.
(Also, no need for a keyword in the interna function.)
This commit is contained in:
parent
c26faaebfa
commit
73526b449b
|
@ -156,9 +156,10 @@
|
||||||
(list (string->symbol k) (read-json)))
|
(list (string->symbol k) (read-json)))
|
||||||
(apply hasheq (apply append (read-list 'object #rx#"^}" read-pair))))
|
(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)
|
(skip-whitespace)
|
||||||
(cond
|
(cond
|
||||||
|
[(and top? (eof-object? (peek-char i))) eof]
|
||||||
[(regexp-try-match #px#"^true\\b" i) #t]
|
[(regexp-try-match #px#"^true\\b" i) #t]
|
||||||
[(regexp-try-match #px#"^false\\b" i) #f]
|
[(regexp-try-match #px#"^false\\b" i) #f]
|
||||||
[(regexp-try-match #px#"^null\\b" i) jsnull]
|
[(regexp-try-match #px#"^null\\b" i) jsnull]
|
||||||
|
@ -171,10 +172,9 @@
|
||||||
(cond [(equal? m #"\"") (read-string)]
|
(cond [(equal? m #"\"") (read-string)]
|
||||||
[(equal? m #"[") (read-list 'array #rx#"^\\]" read-json)]
|
[(equal? m #"[") (read-list 'array #rx#"^\\]" read-json)]
|
||||||
[(equal? m #"{") (read-hash)])))]
|
[(equal? m #"{") (read-hash)])))]
|
||||||
[(and eof-ok? (regexp-try-match #px#"$" i)) eof]
|
|
||||||
[else (err "bad input")]))
|
[else (err "bad input")]))
|
||||||
;;
|
;;
|
||||||
(read-json #:eof-ok? #true))
|
(read-json #t))
|
||||||
|
|
||||||
;; ----------------------------------------------------------------------------
|
;; ----------------------------------------------------------------------------
|
||||||
;; Convenience functions
|
;; Convenience functions
|
||||||
|
|
|
@ -100,8 +100,11 @@
|
||||||
(open-input-string
|
(open-input-string
|
||||||
@T{ 1 [2,3] "four" }))])
|
@T{ 1 [2,3] "four" }))])
|
||||||
je)
|
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)
|
(test do (pred-tests)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user