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