xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/libmj/mj.h (revision e63e4d57f9f9869fa8c09055eaf0acf0e1dea45c)
100d25ba4Sagc /*-
2*e63e4d57Sagc  * Copyright (c) 2010,2011 Alistair Crooks <agc@NetBSD.org>
300d25ba4Sagc  * All rights reserved.
400d25ba4Sagc  *
500d25ba4Sagc  * Redistribution and use in source and binary forms, with or without
600d25ba4Sagc  * modification, are permitted provided that the following conditions
700d25ba4Sagc  * are met:
800d25ba4Sagc  * 1. Redistributions of source code must retain the above copyright
900d25ba4Sagc  *    notice, this list of conditions and the following disclaimer.
1000d25ba4Sagc  * 2. Redistributions in binary form must reproduce the above copyright
1100d25ba4Sagc  *    notice, this list of conditions and the following disclaimer in the
1200d25ba4Sagc  *    documentation and/or other materials provided with the distribution.
1300d25ba4Sagc  *
1400d25ba4Sagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1500d25ba4Sagc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1600d25ba4Sagc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1700d25ba4Sagc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1800d25ba4Sagc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1900d25ba4Sagc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2000d25ba4Sagc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2100d25ba4Sagc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2200d25ba4Sagc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2300d25ba4Sagc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2400d25ba4Sagc  */
2500d25ba4Sagc #ifndef MJ_H_
26*e63e4d57Sagc #define MJ_H_	20110607
2700d25ba4Sagc 
2800d25ba4Sagc enum {
2900d25ba4Sagc 	MJ_NULL		= 1,
3000d25ba4Sagc 	MJ_FALSE	= 2,
3100d25ba4Sagc 	MJ_TRUE		= 3,
3200d25ba4Sagc 	MJ_NUMBER	= 4,
3300d25ba4Sagc 	MJ_STRING	= 5,
3400d25ba4Sagc 	MJ_ARRAY	= 6,
35*e63e4d57Sagc 	MJ_OBJECT	= 7,
36*e63e4d57Sagc 
37*e63e4d57Sagc 	MJ_LAST		= MJ_OBJECT,
38*e63e4d57Sagc 
39*e63e4d57Sagc 	MJ_HUMAN	= 0,	/* human readable, not encoded */
40*e63e4d57Sagc 	MJ_JSON_ENCODE	= 1	/* encoded JSON */
4100d25ba4Sagc };
4200d25ba4Sagc 
4300d25ba4Sagc /* a minimalist JSON node */
4400d25ba4Sagc typedef struct mj_t {
4500d25ba4Sagc 	unsigned	type;		/* type of JSON node */
4600d25ba4Sagc 	unsigned	c;		/* # of chars */
4700d25ba4Sagc 	unsigned	size;		/* size of array */
4800d25ba4Sagc 	union {
4900d25ba4Sagc 		struct mj_t	*v;	/* sub-objects */
5000d25ba4Sagc 		char		*s;	/* string value */
5100d25ba4Sagc 	} value;
5200d25ba4Sagc } mj_t;
5300d25ba4Sagc 
5400d25ba4Sagc /* creation and deletion */
55*e63e4d57Sagc int mj_create(mj_t */*atom*/, const char */*type*/, .../*value*/);
56*e63e4d57Sagc int mj_parse(mj_t */*atom*/, const char */*s*/, int */*from*/,
57*e63e4d57Sagc 		int */*to*/, int */*token*/);
58*e63e4d57Sagc int mj_append(mj_t */*atom*/, const char */*type*/, .../*value*/);
59*e63e4d57Sagc int mj_append_field(mj_t */*atom*/, const char */*name*/, const char */*type*/,
60*e63e4d57Sagc 		.../*value*/);
61*e63e4d57Sagc int mj_deepcopy(mj_t */*dst*/, mj_t */*src*/);
62*e63e4d57Sagc void mj_delete(mj_t */*atom*/);
6300d25ba4Sagc 
6400d25ba4Sagc /* JSON object access */
65*e63e4d57Sagc int mj_arraycount(mj_t */*atom*/);
66*e63e4d57Sagc int mj_object_find(mj_t */*atom*/, const char */*name*/,
67*e63e4d57Sagc 		const unsigned /*from*/, const unsigned /*incr*/);
68*e63e4d57Sagc mj_t *mj_get_atom(mj_t */*atom*/, ...);
69*e63e4d57Sagc int mj_lint(mj_t */*atom*/);
7000d25ba4Sagc 
7100d25ba4Sagc /* textual output */
72*e63e4d57Sagc int mj_snprint(char */*buf*/, size_t /*size*/, mj_t */*atom*/, int /*encoded*/);
73*e63e4d57Sagc int mj_asprint(char **/*bufp*/, mj_t */*atom*/, int /*encoded*/);
74*e63e4d57Sagc int mj_string_size(mj_t */*atom*/);
75*e63e4d57Sagc int mj_pretty(mj_t */*atom*/, void */*fp*/, unsigned /*depth*/,
76*e63e4d57Sagc 		const char */*trailer*/);
77*e63e4d57Sagc const char *mj_string_rep(mj_t */*atom*/);
7800d25ba4Sagc 
7900d25ba4Sagc #endif
80