tjson
1 Introduction
A typed Json parser and emitter.
1.1 Object Mappings
A Json Object is reified as a Racket hashmap, Json array to a Racket list and Strings, Booleans and Numbers mapped to Racket’s types. A special symbol is reserved to indicating a Json null value.
A parsed Json graph follows the naive recursive data structure definition as Json is a string, number, boolean (true or false), list of Json or an object with a multiplicity of unique symbol labeled Json.
(require tjson) | package: tjson |
2 Json Parsed Data Structure
value
JsList : (Listof Json)
value
JsNull : 'JsNull
While the type Symbol has all symbols as inhabiting values, the type ’JsNull has only the single inhabiting value ’JsNull.
3 Json Parsing
procedure
(string->json json) → Json
json : String
> (string->json "42") - : Json
42
> (string->json "\"Mary had a little lamb\"") - : Json
"Mary had a little lamb"
> (string->json "[\"lamb\", \"cat\", \"dog\"]") - : Json
'("lamb" "cat" "dog")
> (string->json "{\"cat\" : \"meow\", \"dog\" : \"bark\", \"lamb\" : \"bleet\"}") - : Json
'#hasheq((lamb . "bleet") (dog . "bark") (cat . "meow"))
4 Json Construction
> (jsobject '((cat . "meow") (dog . "bark") (lamb . "bleet") (turtle . JsNull))) - : JsObject
'#hasheq((lamb . "bleet") (turtle . JsNull) (dog . "bark") (cat . "meow"))
5 JsObject
5.1 JsObject Attribute Lookup
Lookup an JsObject attribute’s (key) json value.
Note: For boolean values attributes, given how Option values are implementinted in Typed Racket, the #f return value cannot be used to descriminate between an existing attribute whose value is #f or a missing attribute.
procedure
(jsattribute-orelse jobj key default) → Json
jobj : JsObject key : Symbol default : (-> Json)
5.2 JsObject Modification
By design JsObjects are currently implemented as mutable hashmaps and may be mutated. Recall that a JsObject is a type alias for a Racket mutable hashmap. For details concerning concurrent modification see the Racket documantation for hashmaps.
The Racket implemetation of hashmap operations is rich and only a subset are currently wrapped in this library. Additional capabilities such as union, jsobject-union! will added at a later time.
procedure
(jsobject-update! jobj key updater) → Void
jobj : JsObject key : Symbol updater : (-> Json Json)
> (define j (jsobject '((cat . "meow")))) > (jsobject-update! j 'cat (λ (says) (string-append (cast says String) " purr"))) > j - : JsObject
'#hasheq((cat . "meow purr"))
6 Json Emission
procedure
(json->string json) → String
json : Json
> (json->string (jsobject '((cat . "meow") (dog . "bark") (lamb . "bleet") (turtle . JsNull)))) - : String
"{\"dog\": \"bark\", \"cat\": \"meow\", \"turtle\": null, \"lamb\": \"bleet\"}"
7 Port Serialization
procedure
(write-json json outp) → Void
json : Json outp : Output-Port
procedure
(read-json inp) → Json
inp : Input-Port