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