master
h 75 lines 1.71 KB
Raw
1 #ifndef __JSMN_H_
2 #define __JSMN_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <stddef.h>
9 /**
10 * JSON type identifier. Basic types are:
11 * o Object
12 * o Array
13 * o String
14 * o Other primitive: number, boolean (true/false) or null
15 */
16 typedef enum {
17 JSMN_PRIMITIVE = 0,
18 JSMN_OBJECT = 1,
19 JSMN_ARRAY = 2,
20 JSMN_STRING = 3
21 } jsmntype_t;
22
23 typedef enum {
24 /* Not enough tokens were provided */
25 JSMN_ERROR_NOMEM = -1,
26 /* Invalid character inside JSON string */
27 JSMN_ERROR_INVAL = -2,
28 /* The string is not a full JSON packet, more bytes expected */
29 JSMN_ERROR_PART = -3,
30 } jsmnerr_t;
31
32 /**
33 * JSON token description.
34 *
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_ */