KJSON_PUTINT(3)

πŸ“¦ version 1.0.1, in alpine 3.24

πŸ’– KJSON_PUTINT(3) πŸ’–

okay so this is the kcgijson integer-writing page β€” the whole squad is kjson_putint, kjson_putintp, kjson_putintstr, kjson_putintstrp, and kjson_string_putint. bestie, they all just put integers πŸ’…


🌸 LIBRARY 🌸

it lives in the library "libkcgijson" β€” that's the one, no cap 🫢


✨ SYNOPSIS ✨

#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
#include <kcgijson.h>

enum kcgi_err
kjson_putint(struct kjsonreq *req, int64_t v);

enum kcgi_err
kjson_putintp(struct kjsonreq *req, const char *name, int64_t v);

enum kcgi_err
kjson_string_putint(struct kjsonreq *req, int64_t v);

enum kcgi_err
kjson_putintstr(struct kjsonreq *r, int64_t v);

enum kcgi_err
kjson_putintstrp(struct kjsonreq *r, const char *name, int64_t v);

πŸ’œ DESCRIPTION πŸ’œ

so basically you're writing a 64-bit integer v into a context req that you already initialised with khtml_open(3). it formats the value as [-]?[0-9]+. slay, that's it ✨

now the vibe check on which function is which scope, because using the wrong one is literally a whole thing (see KCGI_WRITER below πŸ‘€):

  • kjson_putint() and kjson_putintstr() β€” for array scopes
  • kjson_putintp() and kjson_putintstrp() β€” for objects
  • kjson_string_putint() β€” for open strings
⚠️ okay this part matters, bestie: JavaScript is limited to 53 bits, so it's possible that integers may be truncated with kjson_putint() and family. 😭 not a drill!

the alternative forms, kjson_putintstr() and kjson_putintstrp(), emit the value within a string. why? so JavaScript can parse the integer and handle overflow and underflow explicitly. main character energy honestly πŸ‘‘

and one more little detail πŸŽ€ β€” passing a NULL value to name for kjson_putintp() or kjson_putintstrp() is equivalent to calling kjson_putint() or kjson_putintstr(), respectively. cute, right?


πŸ”₯ RETURN VALUES πŸ”₯

returns an enum kcgi_err indicating the error state. here's the whole roster:

  • KCGI_OK β€” success (not an error) πŸŽ‰
  • KCGI_ENOMEM β€” internal memory allocation failure πŸ’€
  • KCGI_HUP β€” the output connection has been terminated. for FastCGI connections, the current connection should be released with khttp_free(3) and parse loop reentered.
  • KCGI_FORM β€” the connection is still expecting headers with khttp_head(3). indicates that khttp_body(3) did not return with success or was not invoked. for FastCGI connections, the current connection should be released with khttp_free(3) and parse loop reentered.
  • KCGI_SYSTEM β€” internal system error writing to the output stream 😬
  • KCGI_WRITER β€” the incorrect form was used for the current scope, for example, an array-type write within an object. (yes, this is the one from the vibe check above πŸ™ˆ)

πŸ¦‹ EXAMPLES πŸ¦‹

the following outputs a simple JSON document. it assumes r is a struct kreq pointer. for brevity, it does not do any error checking.

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_putintp(&req, "bar", 12345);
kjson_arrayp_open(&req, "baz");
kjson_putint(&req, 1)); /* 1 */
kjson_putintstr(&req, 1)); /* "1" */
kjson_close(&req);

see how kjson_putint(&req, 1) gives you the raw 1 and kjson_putintstr(&req, 1) gives you the stringy "1"? iconic difference, iykyk πŸ’…


🌈 SEE ALSO 🌈

kcgijson(3) β€” go read that one too, fr fr 🫢


πŸ‘‘ AUTHORS πŸ‘‘

written by Kristaps Dzonsons <kristaps@bsd.lv>. legend behavior honestly πŸŽ€


and that's the whole page, bestie! this lil beauty comes to you from Debian, rocking the $Mdocdate$ date stamp, and it's filed under KJSON_PUTINT(3) in the manual. go forth and put some integers βœ¨πŸ’–πŸŒΈ

πŸ“¦ 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