JSON
Document Type: Explanation
Handling JSON requests
We're going to look at some selections from the sire/demo_mandelbrot_ui.sire
cog that you saw when you first installed Pallas on your system.
Taking that one chunk at a time:
Import json.sire
, from which we'll need the parseJson
function soon.
We'll look at datacase
next, but consider it a sort of switch or if/else for now.
asJsonRow
takes some parsed JSON and returns it as a sire row (if there's anything there. That's where datacase
comes in).
asJsonNum
takes some parsed JSON and returns it as a nat.
sire/json.sire
has similar helpers for JSON's nulls, boolens, strings and maps (objects) (JNULL
, JFALSE
, JTRUE
, JSTR
, JMAP
).
The mandelbrot backend accepts a width
and height
from the client side and generates a fractal of these dimensions.
parseFract
takes a JSON-y bar (which it happens to expect to be a single "row" of json numbers) and uses the above-described two helpers to bind these values to w
and h
. Width and height.
Now that we have all the handlers in place, we can actually take a request from the browser:
We haven't yet looked at how a webserver cog works, but the point here is:
We get a
POST
request at a particular path from the web clientWe parse the request body as JSON
Assuming we got dimensions as expected, we generate the fractal (
fractBar
) and respond to the request with this value. Responding with JSON is what we're looking at next.
Responding with JSON
For an example of providing a JSON response from a cog to a web client, we'll pull some lines from sire/demo_runcog_site.sire
, an example app that allows a web client to start and stop toy timer cogs as well as check the current status of any cogs in the app:
Once again, json
is imported, which will give us many functions we need.
jsonContentType
is a convenient binding that will make it easier to set response headers.
And again, we're handling an HTTP request by switching on method
and path
in order to handle an HTTP GET /status
.
We haven't seen readRef
and a few other bits here, but suffice it to say that we're fetching some values from the cog's "state" and binding them to kvs
.
The jval
binding is doing most of the work here. The mandelbrot cog we saw previously used some helper functions for JSON parsing while here the JSON encoding is happening directly inline with JMAP
and related functions from the json
library.
It's good to see both approaches.
We won't go through this line-by-line - you can handle it by this stage. The broad strokes are:
For each of the timer cogs we know about, encode their statuses depending on which state they're in. These are encoded as a row of JSON objects (
JMAP
).Get a JSON bar and appropriate headers and provide an HTTP 200 response with these values.
After doing two POST /spin
requests to start a couple timers, a GET /status
HTTP response provides this JSON body:
For completeness, here are the headers:
Pallas is talking JSON to a web browser. Very nice.
We promised to tell you a bit about datacase
and cog state, so let's do that next.
Last updated