KJSON_PUTDOUBLE(3)
π 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 scopeskjson_putdoublep()β for objectskjson_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 withkhttp_free(3)and the parse loop reentered.KCGI_FORMβ the connection is still expecting headers withkhttp_head(3). this meanskhttp_body(3)did not return with success or was not invoked. for FastCGI connections, the current connection should be released withkhttp_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
- 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