xref: /freebsd/contrib/ntp/libjsmn/jsmn.h (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert #ifndef __JSMN_H_
2*2b15cb3dSCy Schubert #define __JSMN_H_
3*2b15cb3dSCy Schubert 
4*2b15cb3dSCy Schubert /**
5*2b15cb3dSCy Schubert  * JSON type identifier. Basic types are:
6*2b15cb3dSCy Schubert  * 	o Object
7*2b15cb3dSCy Schubert  * 	o Array
8*2b15cb3dSCy Schubert  * 	o String
9*2b15cb3dSCy Schubert  * 	o Other primitive: number, boolean (true/false) or null
10*2b15cb3dSCy Schubert  */
11*2b15cb3dSCy Schubert typedef enum {
12*2b15cb3dSCy Schubert 	JSMN_PRIMITIVE = 0,
13*2b15cb3dSCy Schubert 	JSMN_OBJECT = 1,
14*2b15cb3dSCy Schubert 	JSMN_ARRAY = 2,
15*2b15cb3dSCy Schubert 	JSMN_STRING = 3
16*2b15cb3dSCy Schubert } jsmntype_t;
17*2b15cb3dSCy Schubert 
18*2b15cb3dSCy Schubert typedef enum {
19*2b15cb3dSCy Schubert 	/* Not enough tokens were provided */
20*2b15cb3dSCy Schubert 	JSMN_ERROR_NOMEM = -1,
21*2b15cb3dSCy Schubert 	/* Invalid character inside JSON string */
22*2b15cb3dSCy Schubert 	JSMN_ERROR_INVAL = -2,
23*2b15cb3dSCy Schubert 	/* The string is not a full JSON packet, more bytes expected */
24*2b15cb3dSCy Schubert 	JSMN_ERROR_PART = -3,
25*2b15cb3dSCy Schubert 	/* Everything was fine */
26*2b15cb3dSCy Schubert 	JSMN_SUCCESS = 0
27*2b15cb3dSCy Schubert } jsmnerr_t;
28*2b15cb3dSCy Schubert 
29*2b15cb3dSCy Schubert /**
30*2b15cb3dSCy Schubert  * JSON token description.
31*2b15cb3dSCy Schubert  * @param		type	type (object, array, string etc.)
32*2b15cb3dSCy Schubert  * @param		start	start position in JSON data string
33*2b15cb3dSCy Schubert  * @param		end		end position in JSON data string
34*2b15cb3dSCy Schubert  */
35*2b15cb3dSCy Schubert typedef struct {
36*2b15cb3dSCy Schubert 	jsmntype_t type;
37*2b15cb3dSCy Schubert 	int start;
38*2b15cb3dSCy Schubert 	int end;
39*2b15cb3dSCy Schubert 	int size;
40*2b15cb3dSCy Schubert #ifdef JSMN_PARENT_LINKS
41*2b15cb3dSCy Schubert 	int parent;
42*2b15cb3dSCy Schubert #endif
43*2b15cb3dSCy Schubert } jsmntok_t;
44*2b15cb3dSCy Schubert 
45*2b15cb3dSCy Schubert /**
46*2b15cb3dSCy Schubert  * JSON parser. Contains an array of token blocks available. Also stores
47*2b15cb3dSCy Schubert  * the string being parsed now and current position in that string
48*2b15cb3dSCy Schubert  */
49*2b15cb3dSCy Schubert typedef struct {
50*2b15cb3dSCy Schubert 	unsigned int pos; /* offset in the JSON string */
51*2b15cb3dSCy Schubert 	int toknext; /* next token to allocate */
52*2b15cb3dSCy Schubert 	int toksuper; /* superior token node, e.g parent object or array */
53*2b15cb3dSCy Schubert } jsmn_parser;
54*2b15cb3dSCy Schubert 
55*2b15cb3dSCy Schubert /**
56*2b15cb3dSCy Schubert  * Create JSON parser over an array of tokens
57*2b15cb3dSCy Schubert  */
58*2b15cb3dSCy Schubert void jsmn_init(jsmn_parser *parser);
59*2b15cb3dSCy Schubert 
60*2b15cb3dSCy Schubert /**
61*2b15cb3dSCy Schubert  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
62*2b15cb3dSCy Schubert  * a single JSON object.
63*2b15cb3dSCy Schubert  */
64*2b15cb3dSCy Schubert jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js,
65*2b15cb3dSCy Schubert 		jsmntok_t *tokens, unsigned int num_tokens);
66*2b15cb3dSCy Schubert 
67*2b15cb3dSCy Schubert #endif /* __JSMN_H_ */
68