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.
33 * Allocates a fresh unused token from the token pool.
35 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
36 jsmntok_t *tokens, size_t num_tokens)
40 if ((unsigned)parser->toknext >= num_tokens)
42 tok = &tokens[parser->toknext++];
43 tok->start = tok->end = -1;
49 * Fills token type and boundaries.
51 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
61 * Fills next available token with JSON primitive.
63 static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
65 jsmntok_t *tokens, size_t num_tokens)
72 for (; parser->pos < len; parser->pos++) {
73 switch (js[parser->pos]) {
76 * In strict mode primitive must be followed by ","
92 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
94 return JSMN_ERROR_INVAL;
99 * In strict mode primitive must be followed by a
100 * comma/object/array.
103 return JSMN_ERROR_PART;
107 token = jsmn_alloc_token(parser, tokens, num_tokens);
110 return JSMN_ERROR_NOMEM;
112 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
113 parser->pos--; /* parent sees closing brackets */
118 * Fills next token with JSON string.
120 static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
122 jsmntok_t *tokens, size_t num_tokens)
125 int start = parser->pos;
127 /* Skip starting quote */
130 for (; parser->pos < len; parser->pos++) {
131 char c = js[parser->pos];
133 /* Quote: end of string */
135 token = jsmn_alloc_token(parser, tokens, num_tokens);
138 return JSMN_ERROR_NOMEM;
140 jsmn_fill_token(token, JSMN_STRING, start+1,
145 /* Backslash: Quoted symbol expected */
148 switch (js[parser->pos]) {
149 /* Allowed escaped symbols */
159 /* Allows escaped symbol \uXXXX */
163 /* Unexpected symbol */
166 return JSMN_ERROR_INVAL;
171 return JSMN_ERROR_PART;
175 * Parse JSON string and fill tokens.
177 jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
178 jsmntok_t *tokens, unsigned int num_tokens)
185 * Keeps track of whether a new object/list/primitive is expected. New items are only
186 * allowed after an opening brace, comma or colon. A closing brace after a comma is not
189 int expecting_item = 1;
192 for (; parser->pos < len; parser->pos++) {
202 return JSMN_ERROR_INVAL;
204 token = jsmn_alloc_token(parser, tokens, num_tokens);
206 return JSMN_ERROR_NOMEM;
207 if (parser->toksuper != -1)
208 tokens[parser->toksuper].size++;
209 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
210 token->start = parser->pos;
211 parser->toksuper = parser->toknext - 1;
217 return JSMN_ERROR_INVAL;
219 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
220 for (i = parser->toknext - 1; i >= 0; i--) {
222 if (token->start != -1 && token->end == -1) {
223 if (token->type != type)
224 return JSMN_ERROR_INVAL;
225 parser->toksuper = -1;
226 token->end = parser->pos + 1;
230 /* Error if unmatched closing bracket */
232 return JSMN_ERROR_INVAL;
233 for (; i >= 0; i--) {
235 if (token->start != -1 && token->end == -1) {
236 parser->toksuper = i;
244 return JSMN_ERROR_INVAL;
247 r = jsmn_parse_string(parser, js, len, tokens,
251 if (parser->toksuper != -1)
252 tokens[parser->toksuper].size++;
263 return JSMN_ERROR_INVAL;
267 * In strict mode primitives are:
268 * numbers and booleans.
289 * In non-strict mode every unquoted value
298 return JSMN_ERROR_INVAL;
301 r = jsmn_parse_primitive(parser, js, len, tokens,
305 if (parser->toksuper != -1)
306 tokens[parser->toksuper].size++;
310 /* Unexpected char in strict mode */
312 return JSMN_ERROR_INVAL;
317 for (i = parser->toknext - 1; i >= 0; i--) {
318 /* Unmatched opened object or array */
319 if (tokens[i].start != -1 && tokens[i].end == -1)
320 return JSMN_ERROR_PART;
324 return expecting_item ? JSMN_ERROR_INVAL : JSMN_SUCCESS;
331 * Creates a new parser based over a given buffer with an array of tokens
334 void jsmn_init(jsmn_parser *parser)
338 parser->toksuper = -1;
341 const char *jsmn_strerror(jsmnerr_t err)
344 case JSMN_ERROR_NOMEM:
345 return "No enough tokens";
346 case JSMN_ERROR_INVAL:
347 return "Invalid character inside JSON string";
348 case JSMN_ERROR_PART:
349 return "The string is not a full JSON packet, more bytes expected";
353 return "Unknown json error";