xref: /freebsd/lib/libpmc/pmu-events/jsmn.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /* SPDX-License-Identifier: MIT */
2 /* $FreeBSD$ */
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 } jsmntok_t;
43 
44 /*
45  * JSON parser. Contains an array of token blocks available. Also stores
46  * the string being parsed now and current position in that string
47  */
48 typedef struct {
49 	unsigned int pos; /* offset in the JSON string */
50 	int toknext; /* next token to allocate */
51 	int toksuper; /* superior token node, e.g parent object or array */
52 } jsmn_parser;
53 
54 /*
55  * Create JSON parser over an array of tokens
56  */
57 void jsmn_init(jsmn_parser *parser);
58 
59 /*
60  * Run JSON parser. It parses a JSON data string into and array of tokens,
61  * each describing a single JSON object.
62  */
63 jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js,
64 		     size_t len,
65 		     jsmntok_t *tokens, unsigned int num_tokens);
66 
67 const char *jsmn_strerror(jsmnerr_t err);
68 
69 #endif /* __JSMN_H_ */
70