xref: /minix3/crypto/external/bsd/netpgp/dist/src/libmj/mj.h (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2010,2011 Alistair Crooks <agc@NetBSD.org>
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
7*ebfedea0SLionel Sambuc  * are met:
8*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
10*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
11*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
12*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
13*ebfedea0SLionel Sambuc  *
14*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*ebfedea0SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*ebfedea0SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*ebfedea0SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*ebfedea0SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*ebfedea0SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*ebfedea0SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*ebfedea0SLionel Sambuc  */
25*ebfedea0SLionel Sambuc #ifndef MJ_H_
26*ebfedea0SLionel Sambuc #define MJ_H_	20110607
27*ebfedea0SLionel Sambuc 
28*ebfedea0SLionel Sambuc enum {
29*ebfedea0SLionel Sambuc 	MJ_NULL		= 1,
30*ebfedea0SLionel Sambuc 	MJ_FALSE	= 2,
31*ebfedea0SLionel Sambuc 	MJ_TRUE		= 3,
32*ebfedea0SLionel Sambuc 	MJ_NUMBER	= 4,
33*ebfedea0SLionel Sambuc 	MJ_STRING	= 5,
34*ebfedea0SLionel Sambuc 	MJ_ARRAY	= 6,
35*ebfedea0SLionel Sambuc 	MJ_OBJECT	= 7,
36*ebfedea0SLionel Sambuc 
37*ebfedea0SLionel Sambuc 	MJ_LAST		= MJ_OBJECT,
38*ebfedea0SLionel Sambuc 
39*ebfedea0SLionel Sambuc 	MJ_HUMAN	= 0,	/* human readable, not encoded */
40*ebfedea0SLionel Sambuc 	MJ_JSON_ENCODE	= 1	/* encoded JSON */
41*ebfedea0SLionel Sambuc };
42*ebfedea0SLionel Sambuc 
43*ebfedea0SLionel Sambuc /* a minimalist JSON node */
44*ebfedea0SLionel Sambuc typedef struct mj_t {
45*ebfedea0SLionel Sambuc 	unsigned	type;		/* type of JSON node */
46*ebfedea0SLionel Sambuc 	unsigned	c;		/* # of chars */
47*ebfedea0SLionel Sambuc 	unsigned	size;		/* size of array */
48*ebfedea0SLionel Sambuc 	union {
49*ebfedea0SLionel Sambuc 		struct mj_t	*v;	/* sub-objects */
50*ebfedea0SLionel Sambuc 		char		*s;	/* string value */
51*ebfedea0SLionel Sambuc 	} value;
52*ebfedea0SLionel Sambuc } mj_t;
53*ebfedea0SLionel Sambuc 
54*ebfedea0SLionel Sambuc /* creation and deletion */
55*ebfedea0SLionel Sambuc int mj_create(mj_t */*atom*/, const char */*type*/, .../*value*/);
56*ebfedea0SLionel Sambuc int mj_parse(mj_t */*atom*/, const char */*s*/, int */*from*/,
57*ebfedea0SLionel Sambuc 		int */*to*/, int */*token*/);
58*ebfedea0SLionel Sambuc int mj_append(mj_t */*atom*/, const char */*type*/, .../*value*/);
59*ebfedea0SLionel Sambuc int mj_append_field(mj_t */*atom*/, const char */*name*/, const char */*type*/,
60*ebfedea0SLionel Sambuc 		.../*value*/);
61*ebfedea0SLionel Sambuc int mj_deepcopy(mj_t */*dst*/, mj_t */*src*/);
62*ebfedea0SLionel Sambuc void mj_delete(mj_t */*atom*/);
63*ebfedea0SLionel Sambuc 
64*ebfedea0SLionel Sambuc /* JSON object access */
65*ebfedea0SLionel Sambuc int mj_arraycount(mj_t */*atom*/);
66*ebfedea0SLionel Sambuc int mj_object_find(mj_t */*atom*/, const char */*name*/,
67*ebfedea0SLionel Sambuc 		const unsigned /*from*/, const unsigned /*incr*/);
68*ebfedea0SLionel Sambuc mj_t *mj_get_atom(mj_t */*atom*/, ...);
69*ebfedea0SLionel Sambuc int mj_lint(mj_t */*atom*/);
70*ebfedea0SLionel Sambuc 
71*ebfedea0SLionel Sambuc /* textual output */
72*ebfedea0SLionel Sambuc int mj_snprint(char */*buf*/, size_t /*size*/, mj_t */*atom*/, int /*encoded*/);
73*ebfedea0SLionel Sambuc int mj_asprint(char **/*bufp*/, mj_t */*atom*/, int /*encoded*/);
74*ebfedea0SLionel Sambuc int mj_string_size(mj_t */*atom*/);
75*ebfedea0SLionel Sambuc int mj_pretty(mj_t */*atom*/, void */*fp*/, unsigned /*depth*/,
76*ebfedea0SLionel Sambuc 		const char */*trailer*/);
77*ebfedea0SLionel Sambuc const char *mj_string_rep(mj_t */*atom*/);
78*ebfedea0SLionel Sambuc 
79*ebfedea0SLionel Sambuc #endif
80