Little JSON Server is a "build your own" version of the famous JSON Server written in less than 500 lines of C. This is possible by using following libraries:
- Libmicrohttpd: A HTTP server
- Jansson: Encode, decode and manipulate JSON data
ljs uses GNU Build System, to build the project simply type:
mkdir _build
cd _build
../configure
makeYou can check if everything is fine with:
make checkljs has the following usage:
ljs --help
Usage: ljs [OPTION...] [FILE]
ljs -- Little JSON server
-p, --port=PORT Server port (default 8080)
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
Report bugs to <julien.rouzieres@mac.com>.Examples:
ljs data.jsonljs -p 9000 data.jsonecho '{"foo":[{"id":1,"bar":"baz"}]}' | ljs -p 9000
Consider following JSON file data.json:
{
"foo":[
{
"id":1,
"bar":"baz"
}
]
}Running ljs data.json exposes following routes :
| Method | Route |
|---|---|
| GET | /foo |
| GET | /foo/:id |
| POST | /foo |
| PUT | /foo/:id |
| DELETE | /foo/:id |
For example, to create a new node:
curl -X POST -d '{"id":2,"bar":"baz"}' http://localhost:8080/foo
{"id":2,"bar":"baz"}Or to update an existing one:
curl -X PUT -d '{"id":2,"baz":"bar"}' http://localhost:8080/foo/2
{"id":2,"baz":"bar"}You can send SIGUSR1 to ljs to write changes to input file, typically for
an existing ljs PID:
kill -s SIGUSR1 $PID
Writting changes to data.json