From 73526b449b6acc426943c020c8696692e1dbdbbf Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Fri, 5 Oct 2012 11:48:26 -0400 Subject: [PATCH] Fix json treatment of eof, plus tests. (Also, no need for a keyword in the interna function.) --- collects/json/main.rkt | 6 +++--- collects/json/tests/json.rkt | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/collects/json/main.rkt b/collects/json/main.rkt index da661c8e21..c2edff0efd 100644 --- a/collects/json/main.rkt +++ b/collects/json/main.rkt @@ -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 diff --git a/collects/json/tests/json.rkt b/collects/json/tests/json.rkt index 4a3c8cdbf2..ea5131061b 100644 --- a/collects/json/tests/json.rkt +++ b/collects/json/tests/json.rkt @@ -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)