KJSON_OPEN(3)
π KJSON_OPEN(3) β ok so this is the kcgijson one, bestie π
Hey bestie! Welcome to KJSON_OPEN(3), a page from the Library Functions Manual. Yes it's a manual page, no it's not boring, stay with me π
β¨ NAME β¨
kjson_open β open a kcgijson context. That's it, that's the whole job, and she does it flawlessly π«Ά
π LIBRARY π
You're gonna need the library βlibkcgijsonβ. Iconic. No cap.
πΈ SYNOPSIS πΈ
Okay so before you do literally anything, you gotta pull in all these headers, in exactly this order, don't @ me:
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#include <kcgijson.h>
enum kcgi_err
kjson_open(struct kjsonreq *req, struct kreq *r);
π« DESCRIPTION π«
So basically kjson_open zeroes and initialises a brand-new context req, and it rides on top of the underlying kcgi(3) context r β the one you already got from khttp_parse(3) or khttp_fcgi_parse(3). Slay.
β οΈ Timing check bestie: you should only call this after khttp_body(3). Not before. Not during. After. Periodt.
Once she's initialised, the scope that's open is the βrootβ scope. And the root scope is a little different from the others β it accepts content similar to what arrays take, like kjson_putstring(3), instead of kjson_putstringp(3). Iykyk π¦
π And listen β if kcgi_writer_disable(3) has been called, your process will be killed with an uncatchable signal. Not a warning. Not an error return. Straight up murdered. So likeβ¦ don't do that π
π RETURN VALUES π
She returns an enum kcgi_err telling you what the error state is. Here's the whole squad:
KCGI_OKβ Success! Not an error, we love to see it πKCGI_ENOMEMβ Internal memory allocation failure π the allocator said no.KCGI_HUPβ The output connection has been terminated. For FastCGI connections, you should release the current connection withkhttp_free(3)and reenter the parse loop. (Relatable, honestly.)KCGI_FORMβ The connection is still expecting headers viakhttp_head(3). This meanskhttp_body(3)either didn't return success or literally was never invoked. For FastCGI connections, same deal: release the current connection withkhttp_free(3)and reenter the parse loop.KCGI_SYSTEMβ Internal system error while writing to the output stream. Rude.
π₯ EXAMPLES π₯
Okay here's the cute little example that outputs a simple JSON page. It assumes r is a struct kreq pointer. And for brevity, it does zero error checking β main character energy, honestly:
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_obj_open(&req);
kjson_putstringp(&req, "foo", "bar");
kjson_close(&req);
Look at her go π₯Ήβ¨
π SEE ALSO π
kcgijson(3)β go read that one next, bestie, it's giving essential.
π AUTHORS π
Written by Kristaps Dzonsons <kristaps@bsd.lv>. Thanks king π
β¨ that's the whole page β¨ This came from the Debian build, version $Mdocdate$, and she's filed under KJSON_OPEN(3). Now go open some JSON contexts, you got this ππ
π¦ where this page comes from
- alpine 3.24: kcgi-doc 1.0.1-r0 Β· ISC
- Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms