KJSON_STRING_WRITE(3)

πŸ“¦ version 1.0.1, in alpine 3.24

πŸ’– 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 with khttp_free(3) and reenter the parse loop.
  • KCGI_FORM β€” the connection is still expecting headers via khttp_head(3). this one means khttp_body(3) didn't return with success, or it just wasn't invoked at all πŸ™ˆ. for FastCGI connections, same deal: release the current connection with khttp_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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms