diff --git a/collects/json/json.scrbl b/collects/json/json.scrbl index 6072aad6b4..450508ba85 100644 --- a/collects/json/json.scrbl +++ b/collects/json/json.scrbl @@ -68,6 +68,13 @@ the @rfc for more information about JSON. string?]{ Generates a JSON source string for the @tech{jsexpr} @racket[x].} +@defproc[(jsexpr->bytes [x jsexpr?] + [#:null jsnull any? (json-null)] + [#:encode encode (or/c 'control 'all) 'control]) + bytes?]{ + Generates a JSON source byte string for the @tech{jsexpr} @racket[x]. + (The byte string is encoded in UTF-8.)} + @section{Parsing JSON Text into JS-Expressions} @defproc[(read-json [in input-port? (current-input-port)] @@ -80,6 +87,10 @@ the @rfc for more information about JSON. jsexpr?]{ Parses the JSON string @racket[str] as an immutable @tech{jsexpr}.} +@defproc[(bytes->jsexpr [str bytes?] [#:null jsnull any? (json-null)]) + jsexpr?]{ + Parses the JSON bytes string @racket[str] as an immutable @tech{jsexpr}.} + @section{A word about design} JSON syntactically distinguishes ``@tt{null}'', array literals, and diff --git a/collects/json/main.rkt b/collects/json/main.rkt index 37981b9131..850b74a220 100644 --- a/collects/json/main.rkt +++ b/collects/json/main.rkt @@ -178,12 +178,20 @@ ;; ---------------------------------------------------------------------------- ;; Convenience functions -(provide jsexpr->string) +(provide jsexpr->string jsexpr->bytes) (define (jsexpr->string x #:null [jsnull (json-null)] #:encode [enc 'control]) (define o (open-output-string)) (write-json x o #:null jsnull #:encode enc) (get-output-string o)) +(define (jsexpr->bytes x #:null [jsnull (json-null)] #:encode [enc 'control]) + (define o (open-output-bytes)) + (write-json x o #:null jsnull #:encode enc) + (get-output-bytes o)) -(provide string->jsexpr) +(provide string->jsexpr bytes->jsexpr) (define (string->jsexpr str #:null [jsnull (json-null)]) + (unless (string? str) (raise-type-error 'string->jsexpr "string" str)) (read-json (open-input-string str) #:null jsnull)) +(define (bytes->jsexpr str #:null [jsnull (json-null)]) + (unless (bytes? str) (raise-type-error 'bytes->jsexpr "bytes" str)) + (read-json (open-input-bytes str) #:null jsnull)) diff --git a/collects/meta/dist-specs.rkt b/collects/meta/dist-specs.rkt index 1cfce929e2..369fbb025f 100644 --- a/collects/meta/dist-specs.rkt +++ b/collects/meta/dist-specs.rkt @@ -534,6 +534,9 @@ mz-extras :+= (- (package: "xml/") (cond* (not plt) => (srcfile: "*-{tool|snipclass}.rkt" "xml.png"))) +;; -------------------- json +mz-extras :+= (package: "json/") + ;; -------------------- ffi mz-extras :+= (collects: "ffi/") (doc: "objc")