2 * Copyright (c) 2010 Serge A. Zaitsev
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * Slightly modified by AK to not assume 0 terminated input.
30 * Allocates a fresh unused token from the token pool.
32 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
33 jsmntok_t *tokens, size_t num_tokens)
37 if ((unsigned)parser->toknext >= num_tokens)
39 tok = &tokens[parser->toknext++];
40 tok->start = tok->end = -1;
46 * Fills token type and boundaries.
48 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
58 * Fills next available token with JSON primitive.
60 static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
62 jsmntok_t *tokens, size_t num_tokens)
69 for (; parser->pos < len; parser->pos++) {
70 switch (js[parser->pos]) {
73 * In strict mode primitive must be followed by ","
89 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
91 return JSMN_ERROR_INVAL;
96 * In strict mode primitive must be followed by a
100 return JSMN_ERROR_PART;
104 token = jsmn_alloc_token(parser, tokens, num_tokens);
107 return JSMN_ERROR_NOMEM;
109 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
110 parser->pos--; /* parent sees closing brackets */
115 * Fills next token with JSON string.
117 static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
119 jsmntok_t *tokens, size_t num_tokens)
122 int start = parser->pos;
124 /* Skip starting quote */
127 for (; parser->pos < len; parser->pos++) {
128 char c = js[parser->pos];
130 /* Quote: end of string */
132 token = jsmn_alloc_token(parser, tokens, num_tokens);
135 return JSMN_ERROR_NOMEM;
137 jsmn_fill_token(token, JSMN_STRING, start+1,
142 /* Backslash: Quoted symbol expected */
145 switch (js[parser->pos]) {
146 /* Allowed escaped symbols */
156 /* Allows escaped symbol \uXXXX */
160 /* Unexpected symbol */
163 return JSMN_ERROR_INVAL;
168 return JSMN_ERROR_PART;
172 * Parse JSON string and fill tokens.
174 jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
175 jsmntok_t *tokens, unsigned int num_tokens)
182 * Keeps track of whether a new object/list/primitive is expected. New items are only
183 * allowed after an opening brace, comma or colon. A closing brace after a comma is not
186 int expecting_item = 1;
189 for (; parser->pos < len; parser->pos++) {
199 return JSMN_ERROR_INVAL;
201 token = jsmn_alloc_token(parser, tokens, num_tokens);
203 return JSMN_ERROR_NOMEM;
204 if (parser->toksuper != -1)
205 tokens[parser->toksuper].size++;
206 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
207 token->start = parser->pos;
208 parser->toksuper = parser->toknext - 1;
214 return JSMN_ERROR_INVAL;
216 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
217 for (i = parser->toknext - 1; i >= 0; i--) {
219 if (token->start != -1 && token->end == -1) {
220 if (token->type != type)
221 return JSMN_ERROR_INVAL;
222 parser->toksuper = -1;
223 token->end = parser->pos + 1;
227 /* Error if unmatched closing bracket */
229 return JSMN_ERROR_INVAL;
230 for (; i >= 0; i--) {
232 if (token->start != -1 && token->end == -1) {
233 parser->toksuper = i;
241 return JSMN_ERROR_INVAL;
244 r = jsmn_parse_string(parser, js, len, tokens,
248 if (parser->toksuper != -1)
249 tokens[parser->toksuper].size++;
260 return JSMN_ERROR_INVAL;
264 * In strict mode primitives are:
265 * numbers and booleans.
286 * In non-strict mode every unquoted value
295 return JSMN_ERROR_INVAL;
298 r = jsmn_parse_primitive(parser, js, len, tokens,
302 if (parser->toksuper != -1)
303 tokens[parser->toksuper].size++;
307 /* Unexpected char in strict mode */
309 return JSMN_ERROR_INVAL;
314 for (i = parser->toknext - 1; i >= 0; i--) {
315 /* Unmatched opened object or array */
316 if (tokens[i].start != -1 && tokens[i].end == -1)
317 return JSMN_ERROR_PART;
321 return expecting_item ? JSMN_ERROR_INVAL : JSMN_SUCCESS;
328 * Creates a new parser based over a given buffer with an array of tokens
331 void jsmn_init(jsmn_parser *parser)
335 parser->toksuper = -1;
338 const char *jsmn_strerror(jsmnerr_t err)
341 case JSMN_ERROR_NOMEM:
342 return "No enough tokens";
343 case JSMN_ERROR_INVAL:
344 return "Invalid character inside JSON string";
345 case JSMN_ERROR_PART:
346 return "The string is not a full JSON packet, more bytes expected";
350 return "Unknown json error";