D. Ben Knoble
This package provides a convienient format to embed JSON data in racket programs by making the JSON data (surprise!) racket programs.
Suppose, for example, we had some JSON data consisting of a list of albums and singles.
["Cycles", "Flatspin", "Long Way to Climb"]
We also have a map of user favorites (each value is an index in the albums array).
{
"Jonathan Gordon": 0,
"Brad Rubinstein": 2,
"Paul Willmott": 1,
"Jason Hall": 0,
"Adam Rich": 1
}
Normally, we would need to require the json library and use read-json to read these data files. With jsond, however, we can make the files into a program:
#:namealbums["Cycles","Flatspin","LongWaytoClimb"]#:namefavorites{"JonathanGordon":0,"BradRubinstein":2,"PaulWillmott":1,"JasonHall":0,"AdamRich":1}
If we put this in a file lisp.rkt, we suddenly have a program!
> albums'("Cycles" "Flatspin" "Long Way to Climb")
> favorites'#hasheq((|Adam Rich| . 1)
(|Brad Rubinstein| . 2)
(|Jason Hall| . 0)
(|Jonathan Gordon| . 0)
(|Paul Willmott| . 1))
'#hash((|Adam Rich| . "Flatspin")
(|Brad Rubinstein| . "Long Way to Climb")
(|Jason Hall| . "Cycles")
(|Jonathan Gordon| . "Cycles")
(|Paul Willmott| . "Flatspin"))
The program can also be run to recover the original JSON. The following racket code is equivalent to running a command-line
# racket lisp.rkt
{"albums":["Cycles","Flatspin","Long Way to Climb"],"favorites":{"Adam Rich":1,"Brad Rubinstein":2,"Jason Hall":0,"Jonathan Gordon":0,"Paul Willmott":1}}
The jsond language supports all of the JSON specification in JSON values, as well as regular racket comments between value definitions:
> abc"abc"
> num123
> array'(1 2 #hasheq((xyz . "abc")) #t #f null)
‹jsond›
::=
‹name-json›*
‹name-json›
::=
#:name‹id›‹json›
There may be any style of racket comment between ‹name-json›s. Each use of #:name, ‹id›, and ‹json› must be whitespace-separated. ‹json› can be any valid JSON value as determined by read-json . ‹id› can be any valid Racket identifier.
The jsond language produces a module that provide s each of the ‹id›s as a binding to the jsexpr? corresponding to the adjacent ‹json›.
The module has a submodule main that, when run, calls write-json on a hash mapping each provide d ‹id› to its value.