From 77f0addb59a42126134ab37ee1aade02afeaf99a Mon Sep 17 00:00:00 2001 From: Matthias Felleisen Date: Fri, 9 Oct 2020 09:31:38 -0400 Subject: [PATCH] explain how to read sequences of JSON texts from ports, etc --- pkgs/racket-doc/json/json.scrbl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkgs/racket-doc/json/json.scrbl b/pkgs/racket-doc/json/json.scrbl index 13389e1815..75f1c01c6f 100644 --- a/pkgs/racket-doc/json/json.scrbl +++ b/pkgs/racket-doc/json/json.scrbl @@ -112,8 +112,14 @@ the @rfc for more information about JSON. ] } +@; ----------------------------------------------------------------------------- @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)] [#:null jsnull any/c (json-null)]) (or/c jsexpr? eof-object?)]{ @@ -125,10 +131,28 @@ the @rfc for more information about JSON. (with-input-from-string "{\"arr\" : [1, 2, 3, 4]}" (λ () (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 (with-input-from-string "sandwich sandwich" (code:comment "invalid JSON") (λ () (read-json)))) + + (eval:error + (with-input-from-string + "false42" (code:comment "invalid JSON text sequence") + (λ () (read-json)))) ] }