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