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