KJSON_STRING_WRITE(3)
π KJSON_STRING_WRITE(3) β the kjson_string_write glow-up π
okay so bestie, you know how this whole thing lives in the Library Functions Manual? yeah. that's the vibe. this is the kjson_string_write page and it's literally about dropping content data into your kcgijson setup. let's get into it π«Ά
β¨ NAME β¨
kjson_string_write β puts content data for kcgijson, no cap π
π LIBRARY π
you'll find this one chilling in the library βlibkcgijsonβ β¨
π¦ SYNOPSIS π¦
so first you gotta include the whole little squad of headers, exactly like this:
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#include <kcgijson.h>
and then the function itself, looking all cute and official:
enum kcgi_err
kjson_string_write(const char *buf, size_t sz, void *arg);
πΈ DESCRIPTION πΈ
okay so what this does is write binary data buf of length sz into a string scope that you opened earlier with kjson_string_open(3) or kjson_stringp_open(3) π β and it writes into the context arg, which is a struct kjsonreq that you initialised with khtml_open(3) and then passed along as an opaque pointer. iykyk.
and here's the little safety net: if buf is NULL or sz is zero, it does absolutely nothing and just returns KCGI_OK. icon behaviour honestly π
every single character gets JSON escaped, so your content literally cannot prematurely end its context. it's giving "respectful houseguest" energy π§
π₯ RETURN VALUES π₯
it hands back an enum kcgi_err telling you the error state. let's meet the whole cast π
KCGI_OKβ success! not an error, we love to see it πKCGI_ENOMEMβ internal memory allocation said "nope", it failed πKCGI_HUPβ the output connection got terminated π. for FastCGI connections, you should release the current connection withkhttp_free(3)and reenter the parse loop.KCGI_FORMβ the connection is still expecting headers viakhttp_head(3). this one meanskhttp_body(3)didn't return with success, or it just wasn't invoked at all π. 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. rough.KCGI_WRITERβ the wrong form got used for the current scope, like an array-type write inside an object. mismatch energy π©
β EXAMPLES β
this little baddie outputs a simple JSON document. it assumes r is a struct kreq pointer. and for brevity it does zero error checking (not me being reckless, that's just what the page says π
):
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_string_open(&req);
kjson_string_write("hello", 5, &req);
kjson_close(&req);
slay. that's the whole flow π
π SEE ALSO π
kcgijson(3)β go read it, bestie, it's the parent page and it ate π
π AUTHORS π
written by Kristaps Dzonsons <kristaps@bsd.lv> β absolute main character energy, we stan π«Ά
and that's the page!! this came from Debian, with the version/date placeholder $Mdocdate$ sitting pretty next to KJSON_STRING_WRITE(3) down at the bottom β so yeah, that's the source, the version, and the date, all wrapped up with a bow ππβ¨
π¦ 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