Raymond Racine <ray.racine@gmail.com>
A typed Json parser and emitter.
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.
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.
procedure
(string->jsonjson)→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((dog. "bark")(cat. "meow")(lamb. "bleet"))
> (jsobject'((cat. "meow")(dog. "bark")(lamb. "bleet")(turtle. JsNull)))- : JsObject
'#hasheq((dog. "bark")(turtle. JsNull)(cat. "meow")(lamb. "bleet"))
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.
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.
> j- : JsObject
'#hasheq((cat. "meow purr"))
procedure
(json->stringjson)→String
json:Json
> (json->string(jsobject'((cat. "meow")(dog. "bark")(lamb. "bleet")(turtle. JsNull))))- : String
"{\"turtle\": null, \"cat\": \"meow\", \"lamb\": \"bleet\", \"dog\": \"bark\"}"
procedure
(write-jsonjsonoutp)→Void
json:Jsonoutp:Output-Port
procedure
(read-jsoninp)→Json
inp:Input-Port