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