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