7.7
tjson
Link to this document with
@other-doc['(lib "tjson/scribblings/tjson.scrbl")]
Link to this document with
@other-doc['(lib "tjson/scribblings/tjson.scrbl")]
1 Introduction
Link to this section with
@secref["Introduction" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Introduction" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
A typed Json parser and emitter.
1.1 Object Mappings
Link to this section with
@secref["Object_Mappings" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Object_Mappings" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
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.
2 Json Parsed Data Structure
Link to this section with
@secref["Json_Parsed_Data_Structure"
#:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Json_Parsed_Data_Structure"
#:doc '(lib "tjson/scribblings/tjson.scrbl")]
A translation of a parsed Json data structure reflectiing it recursive nature.
A define-type alias reflecting a Json Object as a hashmap of symbols and Json values.
A define-type alias reflecting a Json Array as a Racket List of Json.
A Json null value is reflected in Racket as a special reserved symbol.
JsNull is a define-type alias for the symbol value at the type level. In Typed Racket each symbol is given a unique type containing only that symbol.
While the type Symbol has all symbols as inhabiting values, the type ’JsNull has only the single inhabiting value ’JsNull.
3 Json Parsing
Link to this section with
@secref["Json_Parsing" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Json_Parsing" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
(string->json json) → Json
|
json : String |
Parse a string representation of Json into a Typed Racket Json structure.
Examples:
> (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
Link to this section with
@secref["Json_Construction" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Json_Construction" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Creates a JsObject instance from a list of symbol and json value associations. Note as a JsObject is just a transparent type alias for a HashMap any of the standard Racket hashmap construction procedures maybe used as well such as (
hash ...) or (make-hash ...).
Example:
> (jsobject '((cat . "meow") (dog . "bark") (lamb . "bleet") (turtle . JsNull))) |
- : JsObject |
'#hasheq((lamb . "bleet") (turtle . JsNull) (dog . "bark") (cat . "meow")) |
5 JsObject
Link to this section with
@secref["JsObject" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["JsObject" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
5.1 JsObject Attribute Lookup
Link to this section with
@secref["JsObject_Attribute_Lookup"
#:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["JsObject_Attribute_Lookup"
#:doc '(lib "tjson/scribblings/tjson.scrbl")]
Lookup an JsObject attribute’s (key) json value.
Whether the attribute exists for the jsobject.
(jsattribute jobj key) → (Option Json)
|
jobj : JsObject |
key : Symbol |
Returns the value of the given attribute key if it exists otherwise #f.
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.
(jsattribute-orelse jobj key default) → Json
|
jobj : JsObject |
key : Symbol |
default : (-> Json) |
Returns the json value of the attribute key if it exists otherwise the json returned by the provided thunk.
5.2 JsObject Modification
Link to this section with
@secref["JsObject_Modification" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["JsObject_Modification" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
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.
(jsobject-add! jobj key json) → Void
|
jobj : JsObject |
key : Symbol |
json : Json |
Adds the attribute to a JsObject overwriting an existing attribute.
(jsobject-remove! jobj key) → Void
|
jobj : JsObject |
key : Symbol |
Removes the attribute from the jsobject if it exists. It is not an error
to attempt to remove a non-existing attribute.
(jsobject-update! jobj key updater) → Void
|
jobj : JsObject |
key : Symbol |
updater : (-> Json Json) |
Modifies an existing attribute value. A fail:contract exception is thrown if the attribute does not exist.
Examples:
> (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
Link to this section with
@secref["Json_Emission" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Json_Emission" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
(json->string json) → String
|
json : Json |
Emit a string representation of a Json data structure.
Example:
> (json->string (jsobject '((cat . "meow") (dog . "bark") (lamb . "bleet") (turtle . JsNull)))) |
- : String |
"{\"dog\": \"bark\", \"cat\": \"meow\", \"turtle\": null, \"lamb\": \"bleet\"}" |
7 Port Serialization
Link to this section with
@secref["Port_Serialization" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Link to this section with
@secref["Port_Serialization" #:doc '(lib "tjson/scribblings/tjson.scrbl")]
Serialize a Json data structure out the provided port as a json string.
Read a json value from the input port.