KJSON_PUTSTRING(3)

πŸ“¦ version 1.0.1, in alpine 3.24

πŸ’– 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 with khttp_free(3) and the parse loop reentered. so, cleanup era 🧹
  • KCGI_FORM β€” the connection is still expecting headers with khttp_head(3). this indicates that khttp_body(3) did not return with success or was not invoked. and again, for FastCGI connections, the current connection should be released with khttp_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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms