This library provides Scheme object -> JSON string and vice versa utilities.
JSON object builder is a Schem object which contains information to
construct a Scheme object from JSON representation. Currently this can be
created only via json-object-builder
macro.
Returns #t if the given obj is a JSON object builder.
A DSL which constructs JSON object builder.
The spec must be one of the followings:
(@ ->array spec)
(@ ->array)
(ctr mapping ...)
ctr/builder
->array must be a procedure which accepts variable length of
arguments, such as list
or vector
.
ctr must be a procedure which accepts the same number of the specified keys in the mapping and constucts object.
ctr/builder must be either object constructor described above
or JSON object builder created by the json-object-builder
.
If the first 2 form is used, then the created builder handles JSON array.
If the 3rd form is used, then the created builder handles JSON object (a.k.a map).
If the lsst form is used, then the created builder handles simple JSON values, such as JSON string and number.
The mapping must be one of the followings:
(? key default spec)
(? key default)
(key spec)
key
key must be a string represents the JSON object's key.
default must be a Scheme object which is used when the key is absent.
The first 2 forms represetns optional values. If the JSON object key key is not present, then default is mapped to the result Scheme object.
Here are some of examples:
(json-object-builder
(make-image-holder
("Image"
(make-image
"Width"
"Height"
"Title"
("Thumbnail"
(make-thumbnail
"Url"
"Height"
"Width"))
"Animated"
("IDs" ( list))))))
#|
Above construct Scheme object from JSON like the following:
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
|#
(json-object-builder
( list
(make-location
"precision"
"Latitude"
"Longitude"
(? "Address" #f)
"City"
"State"
"Zip"
"Country")))
#|
Above construct Scheme object from JSON like the following:
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
|#
(current-input-port)
)
missing-key-handler
Constructs Scheme object from given json, _json-string_or in-port, according to the given builder.
If the first form is used, then json must be a vector type JSON
representation specified by the *json-map-type*
parameter.
(let ((json-string "{\"bar\": {\"buz\": 1}}"))
(define-record-type foo
(fields bar))
(define-record-type bar
(fields buz))
(define bar-builder (json-object-builder (make-bar "buz")))
(define builder (json-object-builder (make-foo ("bar" bar-builder))))
(json-string->object json-string builder))
foo
If missing-key-handler is given, then it must be a procedure accepts 2 arguments. This procedure is called when the conversion procedure met keys which is not defined in builder. The default behaviour is raising an error.
These parameters hold a procedure which is called when an object is constructed from JSON object (map) or JSON array, respectively.
JSON object serializer is a Schem object which contains information to
construct a JSON representaion from Scheme object. Currently this can be
created only via json-object-serializer
macro.
Returns #t if the given obj is a JSON object serializer.
A DSL which constructs JSON object serializer.
The spec must be one of the followings:
(-> car cdr null? spec)
(-> car cdr null?)
(-> spec)
(->)
(@ ref length spec)
(@ ref length)
(@ spec)
(@)
(mapping mapping* ...)
converter/serializer
->
indicates that the given object is a listlike object which can
be accessed sequentially. car, cdr and null? specifies
how to retrieve the car part and cdr part, and how to check if the object
is empty or not, respectively. If these are not given then the macro
uses car
, cdr
and null?
.
@
indicates that the given object is a vectorlike object which
can be accessed randomly. ref and length specifies how to access
the element of the object, and how to retrieve the length of the object,
respectively. If these are not given then the macro uses vector-ref
,
and vector-length
.
If both of the form don't have spec
, then the macro uses the given
value.
mapping
must be one of the followings:
(? name absent ref spec)
(? name absent ref)
(name ref spec)
(name ref)
?
indicates that referencing object might be absent.
name must be a string which represents JSON object's key.
absent must be an object indicating absent value. If the converting
object is equal to this value in sense of equal?
, then the constructed
JSON representaion don't have name.
ref must be a accessor which is a procedure accepts one argument.
converter/serializer must be either a JSON object serializer or a procedure which accepts one argument and returns JSON representaion.
(json-object-serializer
(-> (("precision" location-precision)
("Latitude" location-latitude)
("Longitude" location-longitude)
(? "Address" #f location-address)
("City" location-city)
("State" location-state)
("Zip" location-zip)
("Country" location-country))))
;; Above constructs JSON representaion from the following record type.
(define-record-type location
(fields precision latitude longitude address city state zip country))
(json-object-serializer
(("Image" image-holder-image
(("Width" image-width)
("Height" image-height)
("Title" image-title)
("Thumbnail" image-thumbnail
(("Url" thumbnail-url)
("Height" thumbnail-height)
("Width" thumbnail-width)))
("Animated" image-animated)
("IDs" image-ids (->))))))
;; Above constructs JSON representaion from the following record type.
(define-record-type image-holder
(fields image))
(define-record-type image
(fields width height title thumbnail animated ids))
(define-record-type thumbnail
(fields url height width))
Converts Scheme object to JSON representaion.
The converted JSON representaion is the same as 'vector
representaion.
Converts Scheme object to JSON string.
Writes JSON string converted from obj to out-port.