explain how to read sequences of JSON texts from ports, etc

This commit is contained in:
Matthias Felleisen 2020-10-09 09:31:38 -04:00
parent 965fa1a541
commit 77f0addb59

View File

@ -112,8 +112,14 @@ the @rfc for more information about JSON.
] ]
} }
@; -----------------------------------------------------------------------------
@section{Parsing JSON Text into JS-Expressions} @section{Parsing JSON Text into JS-Expressions}
The following parsing functions will read several JSON texts from
ports, strings, and bytes as long as they are separated like regular
S-expressions in ports, strinngs, and values.
@defproc[(read-json [in input-port? (current-input-port)] @defproc[(read-json [in input-port? (current-input-port)]
[#:null jsnull any/c (json-null)]) [#:null jsnull any/c (json-null)])
(or/c jsexpr? eof-object?)]{ (or/c jsexpr? eof-object?)]{
@ -125,10 +131,28 @@ the @rfc for more information about JSON.
(with-input-from-string (with-input-from-string
"{\"arr\" : [1, 2, 3, 4]}" "{\"arr\" : [1, 2, 3, 4]}"
(λ () (read-json))) (λ () (read-json)))
(with-input-from-string
"\"sandwich\""
(λ () (read-json)))
(with-input-from-string
"true false"
(λ () (list (read-json) (read-json))))
(with-input-from-string
"true[1,2,3]"
(λ () (list (read-json) (read-json))))
(eval:error (eval:error
(with-input-from-string (with-input-from-string
"sandwich sandwich" (code:comment "invalid JSON") "sandwich sandwich" (code:comment "invalid JSON")
(λ () (read-json)))) (λ () (read-json))))
(eval:error
(with-input-from-string
"false42" (code:comment "invalid JSON text sequence")
(λ () (read-json))))
] ]
} }