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.
31 * Allocates a fresh unused token from the token pool.
33 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
34 jsmntok_t *tokens, size_t num_tokens)
38 if ((unsigned)parser->toknext >= num_tokens)
40 tok = &tokens[parser->toknext++];
41 tok->start = tok->end = -1;
47 * Fills token type and boundaries.
49 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
59 * Fills next available token with JSON primitive.
61 static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
63 jsmntok_t *tokens, size_t num_tokens)
70 for (; parser->pos < len; parser->pos++) {
71 switch (js[parser->pos]) {
74 * In strict mode primitive must be followed by ","
90 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
92 return JSMN_ERROR_INVAL;
97 * In strict mode primitive must be followed by a
101 return JSMN_ERROR_PART;
105 token = jsmn_alloc_token(parser, tokens, num_tokens);
108 return JSMN_ERROR_NOMEM;
110 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
111 parser->pos--; /* parent sees closing brackets */
116 * Fills next token with JSON string.
118 static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
120 jsmntok_t *tokens, size_t num_tokens)
123 int start = parser->pos;
125 /* Skip starting quote */
128 for (; parser->pos < len; parser->pos++) {
129 char c = js[parser->pos];
131 /* Quote: end of string */
133 token = jsmn_alloc_token(parser, tokens, num_tokens);
136 return JSMN_ERROR_NOMEM;
138 jsmn_fill_token(token, JSMN_STRING, start+1,
143 /* Backslash: Quoted symbol expected */
146 switch (js[parser->pos]) {
147 /* Allowed escaped symbols */
157 /* Allows escaped symbol \uXXXX */
161 /* Unexpected symbol */
164 return JSMN_ERROR_INVAL;
169 return JSMN_ERROR_PART;
173 * Parse JSON string and fill tokens.
175 jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
176 jsmntok_t *tokens, unsigned int num_tokens)
183 * Keeps track of whether a new object/list/primitive is expected. New items are only
184 * allowed after an opening brace, comma or colon. A closing brace after a comma is not
187 int expecting_item = 1;
190 for (; parser->pos < len; parser->pos++) {
200 return JSMN_ERROR_INVAL;
202 token = jsmn_alloc_token(parser, tokens, num_tokens);
204 return JSMN_ERROR_NOMEM;
205 if (parser->toksuper != -1)
206 tokens[parser->toksuper].size++;
207 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
208 token->start = parser->pos;
209 parser->toksuper = parser->toknext - 1;
215 return JSMN_ERROR_INVAL;
217 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
218 for (i = parser->toknext - 1; i >= 0; i--) {
220 if (token->start != -1 && token->end == -1) {
221 if (token->type != type)
222 return JSMN_ERROR_INVAL;
223 parser->toksuper = -1;
224 token->end = parser->pos + 1;
228 /* Error if unmatched closing bracket */
230 return JSMN_ERROR_INVAL;
231 for (; i >= 0; i--) {
233 if (token->start != -1 && token->end == -1) {
234 parser->toksuper = i;
242 return JSMN_ERROR_INVAL;
245 r = jsmn_parse_string(parser, js, len, tokens,
249 if (parser->toksuper != -1)
250 tokens[parser->toksuper].size++;
261 return JSMN_ERROR_INVAL;
265 * In strict mode primitives are:
266 * numbers and booleans.
287 * In non-strict mode every unquoted value
296 return JSMN_ERROR_INVAL;
299 r = jsmn_parse_primitive(parser, js, len, tokens,
303 if (parser->toksuper != -1)
304 tokens[parser->toksuper].size++;
308 /* Unexpected char in strict mode */
310 return JSMN_ERROR_INVAL;
315 for (i = parser->toknext - 1; i >= 0; i--) {
316 /* Unmatched opened object or array */
317 if (tokens[i].start != -1 && tokens[i].end == -1)
318 return JSMN_ERROR_PART;
322 return expecting_item ? JSMN_ERROR_INVAL : JSMN_SUCCESS;
329 * Creates a new parser based over a given buffer with an array of tokens
332 void jsmn_init(jsmn_parser *parser)
336 parser->toksuper = -1;
339 const char *jsmn_strerror(jsmnerr_t err)
342 case JSMN_ERROR_NOMEM:
343 return "No enough tokens";
344 case JSMN_ERROR_INVAL:
345 return "Invalid character inside JSON string";
346 case JSMN_ERROR_PART:
347 return "The string is not a full JSON packet, more bytes expected";
351 return "Unknown json error";