forked from zserge/jsmn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsmn.h
233 lines (207 loc) · 8.62 KB
/
jsmn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* MIT License
*
* Copyright (c) 2010 Serge Zaitsev
* Copyright (c) 2020 Mark Conway ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef JSMN_H_
#define JSMN_H_
#define JSMN_VERSION_MAJOR 2
#define JSMN_VERSION_MINOR 0
#define JSMN_VERSION_PATCH 0
#if defined(UNIT_TESTING)
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#endif
#include <stddef.h>
#include <limits.h>
#include "jsmn_defines.h"
#if defined(JSMN_SHORT_TOKENS)
typedef unsigned short jsmnint_t;
# define JSMNINT_MAX USHRT_MAX
#else
typedef unsigned int jsmnint_t;
# define JSMNINT_MAX UINT_MAX
#endif
#define JSMN_NEG ((jsmnint_t)-1)
/**
* JSON type identifier. Basic types are:
*/
typedef enum {
JSMN_UNDEFINED = 0x0000,
JSMN_OBJECT = 0x0001, /*!< Object */
JSMN_ARRAY = 0x0002, /*!< Array */
JSMN_STRING = 0x0004, /*!< String */
JSMN_PRIMITIVE = 0x0008, /*!< Other primitive: number, boolean (true/false) or null */
JSMN_KEY = 0x0010, /*!< is a key */
JSMN_VALUE = 0x0020, /*!< is a value */
/* Complex elements */
JSMN_CONTAINER = JSMN_OBJECT | JSMN_ARRAY,
#if !defined(JSMN_PERMISSIVE_KEY)
JSMN_KEY_TYPE = JSMN_STRING,
#else
JSMN_KEY_TYPE = JSMN_STRING | JSMN_PRIMITIVE,
#endif
JSMN_VAL_TYPE = JSMN_OBJECT | JSMN_ARRAY | JSMN_STRING | JSMN_PRIMITIVE,
JSMN_OBJ_VAL = JSMN_OBJECT | JSMN_VALUE,
JSMN_ARR_VAL = JSMN_ARRAY | JSMN_VALUE,
JSMN_STR_KEY = JSMN_STRING | JSMN_KEY,
JSMN_STR_VAL = JSMN_STRING | JSMN_VALUE,
JSMN_PRI_VAL = JSMN_PRIMITIVE | JSMN_VALUE,
#if defined(JSMN_PERMISSIVE_KEY)
JSMN_PRI_KEY = JSMN_PRIMITIVE | JSMN_KEY,
#endif
/* Primitive extension */
JSMN_PRI_LITERAL = 0x0040, /*!< true, false, null */
JSMN_PRI_INTEGER = 0x0080, /*!< 0, 1 - 9 */
JSMN_PRI_SIGN = 0x0100, /*!< minus sign, '-' or plus sign, '+' */
JSMN_PRI_DECIMAL = 0x0200, /*!< deminal point '.' */
JSMN_PRI_EXPONENT = 0x0400, /*!< exponent, 'e' or 'E' */
JSMN_PRI_MINUS = JSMN_PRI_SIGN,
/* Parsing validation, expectations, and state information */
JSMN_PRI_CONTINUE = 0x0800, /*!< Allow a continuation of a PRIMITIVE */
JSMN_CLOSE = 0x1000, /*!< Close OBJECT '}' or ARRAY ']' */
JSMN_COLON = 0x2000, /*!< Colon ':' expected after KEY */
JSMN_COMMA = 0x4000, /*!< Comma ',' expected after VALUE */
JSMN_INSD_OBJ = 0x8000, /*!< Inside an OBJECT */
/* Parsing rules */
#if !defined(JSMN_PERMISSIVE_RULESET)
JSMN_ROOT_INIT = JSMN_VAL_TYPE | JSMN_VALUE,
# if !defined(JSMN_MULTIPLE_JSON)
JSMN_ROOT = JSMN_UNDEFINED,
# else
JSMN_ROOT = JSMN_VAL_TYPE | JSMN_VALUE,
# endif
JSMN_OPEN_OBJECT = JSMN_KEY_TYPE | JSMN_KEY | JSMN_CLOSE | JSMN_INSD_OBJ,
JSMN_AFTR_OBJ_KEY = JSMN_VALUE | JSMN_INSD_OBJ | JSMN_COLON,
JSMN_AFTR_OBJ_VAL = JSMN_CLOSE | JSMN_INSD_OBJ | JSMN_COMMA,
JSMN_OPEN_ARRAY = JSMN_VAL_TYPE | JSMN_VALUE | JSMN_CLOSE,
JSMN_AFTR_ARR_VAL = JSMN_VALUE | JSMN_CLOSE | JSMN_COMMA,
JSMN_AFTR_CLOSE = JSMN_CLOSE | JSMN_COMMA,
JSMN_AFTR_COLON = JSMN_VAL_TYPE | JSMN_VALUE | JSMN_INSD_OBJ,
JSMN_AFTR_COMMA_O = JSMN_KEY_TYPE | JSMN_KEY | JSMN_INSD_OBJ,
JSMN_AFTR_COMMA_A = JSMN_VAL_TYPE | JSMN_VALUE,
#else
JSMN_ROOT_INIT = JSMN_VAL_TYPE | JSMN_VALUE | JSMN_KEY,
# if !defined(JSMN_MULTIPLE_JSON_FAIL)
JSMN_ROOT = JSMN_VAL_TYPE | JSMN_COLON | JSMN_COMMA,
JSMN_ROOT_AFTR_O = JSMN_VAL_TYPE | JSMN_COMMA,
# else
JSMN_ROOT = JSMN_UNDEFINED,
JSMN_ROOT_AFTR_O = JSMN_UNDEFINED,
# endif
JSMN_OPEN_OBJECT = JSMN_KEY_TYPE | JSMN_KEY | JSMN_CLOSE | JSMN_INSD_OBJ,
JSMN_AFTR_OBJ_KEY = JSMN_VALUE | JSMN_INSD_OBJ | JSMN_COLON,
JSMN_AFTR_OBJ_VAL = JSMN_VAL_TYPE | JSMN_CLOSE | JSMN_INSD_OBJ | JSMN_COMMA,
JSMN_OPEN_ARRAY = JSMN_VAL_TYPE | JSMN_VALUE | JSMN_CLOSE,
JSMN_AFTR_ARR_VAL = JSMN_VAL_TYPE | JSMN_CLOSE | JSMN_COMMA,
JSMN_AFTR_CLOSE = JSMN_VAL_TYPE | JSMN_CLOSE | JSMN_COMMA,
JSMN_AFTR_COLON = JSMN_VAL_TYPE | JSMN_VALUE | JSMN_INSD_OBJ,
JSMN_AFTR_COLON_R = JSMN_VAL_TYPE | JSMN_VALUE,
JSMN_AFTR_COMMA_O = JSMN_KEY_TYPE | JSMN_KEY | JSMN_INSD_OBJ,
JSMN_AFTR_COMMA_A = JSMN_VAL_TYPE | JSMN_VALUE,
JSMN_AFTR_COMMA_R = JSMN_VAL_TYPE,
#endif
} jsmntype_t;
/*!
* JSMN Error Codes
*/
typedef enum jsmnerr {
JSMN_SUCCESS = 0,
JSMN_ERROR_NOMEM = -1, /*!< Not enough tokens were provided */
JSMN_ERROR_LENGTH = -2, /*!< Input data too long */
JSMN_ERROR_INVAL = -3, /*!< Invalid character inside JSON string */
JSMN_ERROR_PART = -4, /*!< The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_BRACKETS = -5, /*!< The JSON string has unmatched brackets */
JSMN_ERROR_MAX = -5, /*!< "MAX" value to be tested against when checking for errors */
} jsmnerr;
/*!
* JSMN Boolean
*/
typedef enum jsmnbool {
JSMN_FALSE = 0, /*!< false */
JSMN_TRUE = 1, /*!< true */
} jsmnbool;
/**
* JSON token description.
*/
typedef struct jsmntok_t {
jsmntype_t type; /*!< type (object, array, string etc.) */
jsmnint_t start; /*!< start position in JSON data string */
jsmnint_t end; /*!< end position in JSON data string */
jsmnint_t size; /*!< number of children */
#if defined(JSMN_PARENT_LINKS)
jsmnint_t parent; /*!< parent id */
#endif
#if defined(JSMN_NEXT_SIBLING)
jsmnint_t next_sibling; /*!< next sibling id */
#endif
} jsmntok_t;
/**
* JSON parser
*
* Contains an array of token blocks available. Also stores
* the string being parsed now and current position in that string.
*/
typedef struct jsmn_parser {
jsmnint_t pos; /*!< offset in the JSON string */
jsmnint_t toknext; /*!< next token to allocate */
/*!< when tokens == NULL, keeps track of container types to a depth of (sizeof(jsmnint_t) * 8) */
jsmnint_t toksuper; /*!< superior token node, e.g. parent object or array */
/*!< when tokens == NULL, toksuper represents container depth */
jsmnint_t count; /*!< useful to have in the parser when you are continuing a failed parse with NULL tokens */
jsmntype_t expected; /*!< Expected jsmn type(s) */
} jsmn_parser;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Create JSON parser over an array of tokens
*
* @param[out] parser jsmn parser
*/
JSMN_API
void jsmn_init(jsmn_parser *parser);
/**
* @brief Run JSON parser
*
* It parses a JSON data string into and array of tokens, each
* describing a single JSON object.
*
* @param[in,out] parser jsmn parser
* @param[in] js JSON data string
* @param[in] len JSON data string length
* @param[in,out] tokens pointer to memory allocated for tokens or NULL
* @param[in] num_tokens number of tokens allocated
* @return jsmnint_t number of tokens found or ERRNO
*/
JSMN_API
jsmnint_t jsmn_parse(jsmn_parser *parser, const char *js,
const size_t len, jsmntok_t *tokens,
const size_t num_tokens);
#if !defined(JSMN_HEADER)
#include "jsmn.c"
#endif /* JSMN_HEADER */
#ifdef __cplusplus
}
#endif
#endif /* JSMN_H_ */