Skip to content

Commit

Permalink
kill junk-allowed? abomination
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Sep 16, 2023
1 parent f438bce commit eacae1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
13 changes: 3 additions & 10 deletions src/std/text/json/input.ss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
(syntax-case stx ()
((macro name peek-char read-char)
(with-syntax ((read-json-object (stx-identifier #'macro "read-json-object/" #'name))
(read-json-object1 (stx-identifier #'macro "read-json-object1/" #'name))
(read-json-hash (stx-identifier #'macro "read-json-hash/" #'name))
(read-json-hash-key (stx-identifier #'macro "read-json-hash-key" #'name))
(read-json-list (stx-identifier #'macro "read-json-list/" #'name))
Expand All @@ -27,13 +26,7 @@
(skip-whitespace (stx-identifier #'macro "skip-whitespace/" #'name))
(skip-chars (stx-identifier #'macro "skip-chars/" #'name)))
#'(begin
(def (read-json-object input env (junk-allowed? #t))
(begin0 (read-json-object1 input env)
(unless junk-allowed?
(skip-whitespace input)
(unless (eof-object? (peek-char input))
(error "Junk after JSON object in input" input)))))
(def (read-json-object1 input env)
(def (read-json-object input env)
(skip-whitespace input)
(let (char (peek-char input))
(if (eof-object? char)
Expand Down Expand Up @@ -61,7 +54,7 @@
;; If you see a duplicate key, it's as likely an attack as a bug. #LangSec
(if (hash-key? obj key)
(error "Duplicate hash key in JSON input" key)
(let (val (read-json-object1 input env))
(let (val (read-json-object input env))
(hash-put! obj key val)
(skip-whitespace input)
(let (char (peek-char input))
Expand Down Expand Up @@ -117,7 +110,7 @@
(read-char input)
#!eof)
(else
(let (obj (read-json-object1 input env))
(let (obj (read-json-object input env))
(skip-whitespace input)
(let (char (peek-char input))
(case char
Expand Down
12 changes: 6 additions & 6 deletions src/std/text/json/util.ss
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

(export #t)

(def (read-json (input (current-input-port)) (junk-allowed? #t))
(def (read-json (input (current-input-port)))
(cond
((input-port? input)
(read-json-object/port input (make-env) junk-allowed?))
(read-json-object/port input (make-env)))
((is-BufferedStringReader? input)
(read-json-object/reader (BufferedStringReader input) (make-env) junk-allowed?))
(read-json-object/reader (BufferedStringReader input) (make-env)))
((is-BufferedReader? input)
(read-json-object/buffer (BufferedReader input) (make-env) junk-allowed?))
(read-json-object/buffer (BufferedReader input) (make-env)))
(else
(error "Bad input source; expected input port, BufferedStringReader or BufferedReader instance" input))))

(def (string->json-object str)
(read-json-object/reader (open-buffered-string-reader str) (make-env) #f))
(read-json-object/reader (open-buffered-string-reader str) (make-env)))

(def (bytes->json-object bytes)
(read-json (open-buffered-reader bytes) #f))
(read-json (open-buffered-reader bytes)))

(def (write-json obj (output (current-output-port)))
(cond
Expand Down

0 comments on commit eacae1f

Please sign in to comment.