KJSON_ARRAY_OPEN(3) Library Functions Manual KJSON_ARRAY_OPEN(3)

kjson_array_open, kjson_arrayp_open
open array scope for kcgijson

library “libkcgijson”

#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#include <kcgijson.h>

enum kcgi_err
kjson_array_open(struct kjsonreq *req);

enum kcgi_err
kjson_arrayp_open(struct kjsonreq *req, const char *name);

Open an array scope in a context req initialised with khtml_open(3). Scopes are closed with kjson_array_close(3) or kjson_close(3). Array scopes may be filled in with un-named values such as with kjson_putstring(3).

The kjson_array_open() form is for array scopes, while kjson_arrayp_open() for objects.

Passing a NULL value to name for kjson_arrayp_open(), is equivalent to calling kjson_array_open().

Returns an enum kcgi_err indicating the error state.
Success (not an error).
Internal memory allocation failure, including reaching the maximum number of possible JSON scopes.
The output connection has been terminated. For FastCGI connections, the current connection should be released with khttp_free(3) and parse loop reentered.
The connection is still expecting headers with khttp_head(3). Indicates that khttp_body(3) did not return with success or was not invoked. For FastCGI connections, the current connection should be released with khttp_free(3) and parse loop reentered.
Internal system error writing to the output stream.
The incorrect form was used for the current scope, for example, an array-type write within an object.

The following outputs a simple JSON document. It assumes r is a struct kreq pointer. For brevity, it does not do any error checking.
khttp_head(r, kresps[KRESP_STATUS],
  "%s", khttps[KHTTP_200]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
  "%s", kmimetypes[KMIME_APP_JSON]);
khttp_body(r);
kjson_open(&req, r);
kjson_array_open(&req);
kjson_putstring(&req, "bar");
kjson_putstring(&req, "baz");
kjson_close(&req);

kcgijson(3)

Written by Kristaps Dzonsons <kristaps@bsd.lv>.
March 27, 2020 OpenBSD 6.5