Files
kcgi/man/kcgijson.3
2020-03-27 21:26:37 +00:00

114 lines
3.0 KiB
Plaintext

.\" $Id$
.\"
.\" Copyright (c) 2014, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate$
.Dt KCGIJSON 3
.Os
.Sh NAME
.Nm kcgijson
.Nd JSON output for kcgi
.Sh LIBRARY
.Lb libkcgijson
.Sh SYNOPSIS
.In sys/types.h
.In stdarg.h
.In stdint.h
.In kcgi.h
.In kcgijson.h
.Sh DESCRIPTION
Produce output of JSON objects and arrays in a
.Xr kcgi 3
context allocated with
.Xr khttp_parse 3 .
.Pp
To compile and link, use
.Xr pkg-config 1
as follows:
.Bd -literal
% cc `pkg-config --cflags kcgi-json` -c sample.c
% cc -o sample sample.o `pkg-config --libs kcgi-json`
.Ed
.Pp
All
.Nm kcgijson
functions accept a
.Vt "struct kjsonreq"
object.
To use this properly, open a context with
.Xr kjson_open 3 ,
add content, then end with
.Xr kjson_close 3 .
.Sh EXAMPLES
Let
.Vt struct kreq *r
already be initialised, and the request be for
.Dv KMIME_APP_JSON .
The following fragment prints out a simple JSON object.
Error checking is omitted for brevity.
.Bd -literal -offset indent
kjson_open(&req, r);
kcgi_writer_disable(r);
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);
kjson_objp_open(&req, "anobj");
kjson_putstringp(&req, "astring", "baz");
kjson_putintp(&req, "anint", 12345);
kjson_stringp_open(&req, "alongstring");
kjson_string_puts(&req, "hello, ");
kjson_string_puts(&req, "world.");
kjson_string_close(&req); /* alongstring */
kjson_arrayp_open(&req, "anarray");
kjson_putint(&req, 1);
kjson_putint(&req, 2);
kjson_putdouble(&req, 3.0);
kjson_array_close(&req); /* anarray */
kjson_obj_close(&req); /* anobj */
kjson_close(&req);
.Ed
.Pp
Following the
.Xr kcgi_writer_disable 3
call, no further writers may be allocated.
.Sh SEE ALSO
.Xr kjson_array_close 3 ,
.Xr kjson_array_open 3 ,
.Xr kjson_close 3 ,
.Xr kjson_obj_close 3 ,
.Xr kjson_obj_open 3 ,
.Xr kjson_open 3 ,
.Xr kjson_putbool 3 ,
.Xr kjson_putdouble 3 ,
.Xr kjson_putint 3 ,
.Xr kjson_putnull 3 ,
.Xr kjson_putstring 3 ,
.Xr kjson_string_close 3 ,
.Xr kjson_string_open 3 ,
.Xr kjson_string_write 3
.Sh STANDARDS
The
.Nm kcgijson
functions conform to the ECMA-404 JSON Data Interchange Standard, also
documented as RFC 7159.
Parts of this document reference ECMAScript 5, commonly known as
JavaScript.
.Sh AUTHORS
Written by
.An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .