KJSON_PUTDOUBLE(3)

πŸ“¦ version 1.0.1, in alpine 3.24

πŸ’… KJSON_PUTDOUBLE β€” put floating point content for kcgijson

okay so bestie, welcome to the kjson_putdouble(3) manual page, brought to you by the fine folks over at library β€œlibkcgijson” 🫢✨ (yes that's the library name, no cap). this whole family of functions is literally just here to write your doubles into JSON without you having to think about it. slay.


✨ SYNOPSIS ✨

like, first you gotta include all of these, no skipping:

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

and then the three icons themselves:

enum kcgi_err
kjson_putdouble(struct kjsonreq *req, double v);

enum kcgi_err
kjson_putdoublep(struct kjsonreq *req, const char *name, double v);

enum kcgi_err
kjson_string_putdouble(struct kjsonreq *req, double v);

🌸 DESCRIPTION 🌸

so the whole vibe here: you take a double-precision floating point v and you write it into a context req that was initialised with khtml_open(3). πŸ’–

it formats that value using the "g" format character of printf(3) β€” that's the format char, keep it in mind. and here's the spicy part: if v does not classify as normal, subnormal, or zero, it is output as null. so like, NaN? infinity? yeah, you get null. it's giving "we don't do that here" 😭

which one do you use? depends on your scope, bestie:

  • kjson_putdouble() β€” for array scopes
  • kjson_putdoublep() β€” for objects
  • kjson_string_putdouble() β€” for open strings

and a cute lil edge case: passing a NULL value to name for kjson_putdoublep() is equivalent to calling kjson_putdouble(). same energy, different name. iykyk πŸ’…


πŸ¦‹ RETURN VALUES πŸ¦‹

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

  • KCGI_OK β€” success! not an error, we love to see it πŸ‘‘
  • KCGI_ENOMEM β€” internal memory allocation failure. tragic, honestly 😭
  • KCGI_HUP β€” the output connection has been terminated. for FastCGI connections, the current connection should be released with khttp_free(3) and the parse loop reentered.
  • KCGI_FORM β€” the connection is still expecting headers with khttp_head(3). this means 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 the parse loop reentered. (yes, same recovery dance as above β€” consistency queen ✨)
  • 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. wrong function, wrong place, vibe check failed πŸ™ˆ

πŸŽ€ EXAMPLES πŸŽ€

okay so here's a whole little JSON document being output. it assumes r is a struct kreq pointer. and for brevity, it does not do any error checking β€” which, bestie, in real code you absolutely should, but this is a demo, we're keeping it cute 🌈

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_putdoublep(&req, "bar", 0.12345);
kjson_arrayp_open(&req, "baz");
kjson_putdouble(&req, (1.0 / 0.0)); /* null */
kjson_close(&req);

notice that (1.0 / 0.0) line? that's infinity, and per the rule above it comes out as null. chef's kiss πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€


πŸ’œ AUTHORS πŸ’œ

written by Kristaps Dzonsons <kristaps@bsd.lv>. an icon, honestly 🫢


and that's the page, babes 🧚 this little gem ships with the Debian build, stamped $Mdocdate$, and it's the KJSON_PUTDOUBLE(3) man page β€” go forth and write your doubles responsibly πŸ’–βœ¨πŸ“

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