xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/libmj/mj.h (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*-
2  * Copyright (c) 2010 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_	20100718
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 
38 /* a minimalist JSON node */
39 typedef struct mj_t {
40 	unsigned	type;		/* type of JSON node */
41 	unsigned	c;		/* # of chars */
42 	unsigned	size;		/* size of array */
43 	union {
44 		struct mj_t	*v;	/* sub-objects */
45 		char		*s;	/* string value */
46 	} value;
47 } mj_t;
48 
49 /* creation and deletion */
50 int mj_create(mj_t *, const char *, ...);
51 int mj_parse(mj_t *, const char *, int *, int *, int *);
52 int mj_append(mj_t *, const char *, ...);
53 int mj_append_field(mj_t *, const char *, const char *, ...);
54 int mj_deepcopy(mj_t *, mj_t *);
55 void mj_delete(mj_t *);
56 
57 /* JSON object access */
58 int mj_arraycount(mj_t *);
59 int mj_object_find(mj_t *, const char *, const unsigned, const unsigned);
60 mj_t *mj_get_atom(mj_t *, ...);
61 int mj_lint(mj_t *);
62 
63 /* textual output */
64 int mj_snprint(char *, size_t, mj_t *);
65 int mj_asprint(char **, mj_t *);
66 int mj_string_size(mj_t *);
67 int mj_pretty(mj_t *, void *, unsigned, const char *);
68 
69 #endif
70