KJSON_PUTSTRING(3)
π KJSON_PUTSTRING(3) β put string content for kcgijson π
okay so bestie, welcome to KJSON_PUTSTRING(3) from the Library Functions Manual, we're literally about to make JSON strings happen and it's gonna be adorable. this is kjson_putstring, kjson_putstringp, and kjson_string_puts, all chillin' together in one page π
πΈ LIBRARY πΈ
lowkey important: these live in the library βlibkcgijsonβ. that's the vibe. that's the home. π β¨
β¨ SYNOPSIS β¨
like, here's the whole cast, in code, exactly as written because i would never mess with a synopsis:
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#include <kcgijson.h>
enum kcgi_err
kjson_putstring(struct kjsonreq *req, const char *v);
enum kcgi_err
kjson_putstringp(struct kjsonreq *req, const char *name, const char *v);
enum kcgi_err
kjson_string_puts(struct kjsonreq *req, const char *v);
three functions, one header stack, zero drama. slay π
π¦ DESCRIPTION π¦
so what do they do? they write a string v to a context req that was initialised with khtml_open(3). that's the whole assignment. π
and here's a cute little detail: if v is NULL, these do nothing and return KCGI_OK. no crash, no tantrum, just vibes. π§ββοΈ
which one do you use? depends on the scope, bestie:
kjson_putstring()β for array scopes πkjson_putstringp()β for objects πkjson_string_puts()β for open strings π
and the safety feature?? all values are JSON escaped so that content cannot prematurely end its context. literally not me being protected from my own string content ππ₯
also, passing a NULL value to name for kjson_putstringp() is equivalent to calling kjson_putstring(). same thing, different door. iykyk πͺβ¨
π RETURN VALUES π
returns an enum kcgi_err indicating the error state. so like, it's always telling you how it's feeling. let's go through the whole mood ring:
KCGI_OKβ success (not an error). we love a happy ending πKCGI_ENOMEMβ internal memory allocation failure. the malloc said no πKCGI_HUPβ the output connection has been terminated. and for FastCGI connections, the current connection should be released withkhttp_free(3)and the parse loop reentered. so, cleanup era π§ΉKCGI_FORMβ the connection is still expecting headers withkhttp_head(3). this indicates thatkhttp_body(3)did not return with success or was not invoked. and again, for FastCGI connections, the current connection should be released withkhttp_free(3)and the parse loop reentered. same energy, different error πKCGI_SYSTEMβ internal system error writing to the output stream. oop πKCGI_WRITERβ the incorrect form was used for the current scope, for example, an array-type write within an object. yeah that's a vibe check fail, bestie π«
β EXAMPLES β
here's the flex: this outputs a simple JSON document. it assumes r is a struct kreq pointer. and for brevity, it does not do any error checking (bold of it, 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, "bar", "shbar");
kjson_arrayp_open(&req, "baz");
kjson_putstring(&req, "xyzzy"));
kjson_close(&req);
look at that little JSON being born π₯Ή it's giving main character energy fr fr π
π«Ά SEE ALSO π«Ά
kcgijson(3)β go read the whole family, bestie, they're iconic π
π AUTHORS π
written by Kristaps Dzonsons <kristaps@bsd.lv>. absolute legend, no cap. π«‘π
aaaaand that's the page!! this lil' manual page landed via Debian, dated $Mdocdate$, and it's literally titled KJSON_PUTSTRING(3) β so go forth, escape those strings, respect the scope, and stan KCGI_OK forever ππβ¨
π¦ 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