Add to/from bytes functions to the json library.

Also add a distribution specs line.
This commit is contained in:
Eli Barzilay 2012-03-16 02:55:23 -04:00
parent 818e434c60
commit 9d239170cf
3 changed files with 24 additions and 2 deletions

View File

@ -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

View File

@ -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))

View File

@ -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")