C-based memory friendly JSON parser based on Serge Zaitsev's JSMN (https://bitbucket.org/zserge/jsmn/wiki/Home)

Dependents:   _library_jsmn _library_jsmn _library_jsmn

JSMN

jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects.

You can find more information about JSON format at json.org

Library sources are available at https://bitbucket.org/zserge/jsmn

The web page with some information about jsmn can be found at http://zserge.com/jsmn.html

Committer:
yoonghm
Date:
Mon Nov 17 15:54:28 2014 +0000
Revision:
1:70061827a9c8
Parent:
0:46575249ef23
Child:
2:5167da2fcc4e
Update based on bug fixes at https://bitbucket.org/zserge/jsmn/overview as at 17-Nov-2014.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoonghm 0:46575249ef23 1 /* C-based low-memory footprint JSON parser for mbed
yoonghm 0:46575249ef23 2 * Based on Serge Zaitsev's JSMN https://bitbucket.org/zserge/jsmn/wiki/Home
yoonghm 0:46575249ef23 3 * JSMN is distributed under MIT license.
yoonghm 0:46575249ef23 4 *
yoonghm 0:46575249ef23 5 * Copyright (c) 2014 YoongHM
yoonghm 0:46575249ef23 6 *
yoonghm 0:46575249ef23 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
yoonghm 0:46575249ef23 8 * of this software and associated documentation files (the "Software"), to deal
yoonghm 0:46575249ef23 9 * in the Software without restriction, including without limitation the rights
yoonghm 0:46575249ef23 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yoonghm 0:46575249ef23 11 * copies of the Software, and to permit persons to whom the Software is
yoonghm 0:46575249ef23 12 * furnished to do so, subject to the following conditions:
yoonghm 0:46575249ef23 13 *
yoonghm 0:46575249ef23 14 * The above copyright notice and this permission notice shall be included in
yoonghm 0:46575249ef23 15 * all copies or substantial portions of the Software.
yoonghm 0:46575249ef23 16 *
yoonghm 0:46575249ef23 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yoonghm 0:46575249ef23 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yoonghm 0:46575249ef23 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yoonghm 0:46575249ef23 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yoonghm 0:46575249ef23 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yoonghm 0:46575249ef23 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yoonghm 0:46575249ef23 23 * THE SOFTWARE.
yoonghm 0:46575249ef23 24 */
yoonghm 0:46575249ef23 25
yoonghm 0:46575249ef23 26 #include <stdlib.h>
yoonghm 1:70061827a9c8 27 #include "mbed.h"
yoonghm 0:46575249ef23 28
yoonghm 0:46575249ef23 29 #include "jsmn.h"
yoonghm 0:46575249ef23 30
yoonghm 0:46575249ef23 31
yoonghm 0:46575249ef23 32 /**
yoonghm 0:46575249ef23 33 * Allocates a fresh unused token from the token pull.
yoonghm 0:46575249ef23 34 */
yoonghm 0:46575249ef23 35 static jsmntok_t *
yoonghm 0:46575249ef23 36 jsmn_alloc_token
yoonghm 0:46575249ef23 37 (jsmn_parser *parser
yoonghm 0:46575249ef23 38 ,jsmntok_t *tokens
yoonghm 0:46575249ef23 39 ,size_t num_tokens
yoonghm 0:46575249ef23 40 )
yoonghm 0:46575249ef23 41 {
yoonghm 0:46575249ef23 42 jsmntok_t *tok;
yoonghm 1:70061827a9c8 43
yoonghm 0:46575249ef23 44 if (parser->toknext >= num_tokens)
yoonghm 0:46575249ef23 45 {
yoonghm 0:46575249ef23 46 return NULL;
yoonghm 0:46575249ef23 47 }
yoonghm 0:46575249ef23 48 tok = &tokens[parser->toknext++];
yoonghm 0:46575249ef23 49 tok->start = tok->end = -1;
yoonghm 0:46575249ef23 50 tok->size = 0;
yoonghm 0:46575249ef23 51 tok->parent = -1;
yoonghm 0:46575249ef23 52 return tok;
yoonghm 0:46575249ef23 53 }
yoonghm 0:46575249ef23 54
yoonghm 0:46575249ef23 55
yoonghm 0:46575249ef23 56 /**
yoonghm 0:46575249ef23 57 * Fills token type and boundaries.
yoonghm 0:46575249ef23 58 */
yoonghm 0:46575249ef23 59 static void
yoonghm 0:46575249ef23 60 jsmn_fill_token
yoonghm 0:46575249ef23 61 (jsmntok_t *token
yoonghm 0:46575249ef23 62 ,jsmntype_t type
yoonghm 0:46575249ef23 63 ,int start
yoonghm 0:46575249ef23 64 ,int end
yoonghm 0:46575249ef23 65 )
yoonghm 0:46575249ef23 66 {
yoonghm 0:46575249ef23 67 token->type = type;
yoonghm 0:46575249ef23 68 token->start = start;
yoonghm 0:46575249ef23 69 token->end = end;
yoonghm 0:46575249ef23 70 token->size = 0;
yoonghm 0:46575249ef23 71 }
yoonghm 0:46575249ef23 72
yoonghm 0:46575249ef23 73
yoonghm 0:46575249ef23 74 /**
yoonghm 0:46575249ef23 75 * Fills next available token with JSON primitive.
yoonghm 0:46575249ef23 76 */
yoonghm 0:46575249ef23 77 static int
yoonghm 0:46575249ef23 78 jsmn_parse_primitive
yoonghm 0:46575249ef23 79 (jsmn_parser *parser
yoonghm 0:46575249ef23 80 ,const char *js
yoonghm 0:46575249ef23 81 ,size_t len
yoonghm 0:46575249ef23 82 ,jsmntok_t *tokens
yoonghm 0:46575249ef23 83 ,size_t num_tokens
yoonghm 0:46575249ef23 84 )
yoonghm 0:46575249ef23 85 {
yoonghm 0:46575249ef23 86 jsmntok_t *token;
yoonghm 0:46575249ef23 87 int start;
yoonghm 0:46575249ef23 88
yoonghm 0:46575249ef23 89 start = parser->pos;
yoonghm 0:46575249ef23 90
yoonghm 0:46575249ef23 91 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
yoonghm 0:46575249ef23 92 {
yoonghm 0:46575249ef23 93 switch (js[parser->pos])
yoonghm 0:46575249ef23 94 {
yoonghm 0:46575249ef23 95 case '\t':
yoonghm 0:46575249ef23 96 case '\r':
yoonghm 0:46575249ef23 97 case '\n':
yoonghm 0:46575249ef23 98 case ' ' :
yoonghm 0:46575249ef23 99 case ',' :
yoonghm 0:46575249ef23 100 case ']' :
yoonghm 0:46575249ef23 101 case '}' :
yoonghm 0:46575249ef23 102 goto found;
yoonghm 0:46575249ef23 103 }
yoonghm 0:46575249ef23 104 if (js[parser->pos] < 32 || js[parser->pos] >= 127)
yoonghm 0:46575249ef23 105 {
yoonghm 0:46575249ef23 106 parser->pos = start;
yoonghm 1:70061827a9c8 107 printf("%s:%d: parser->pos=%d\r\n", __FILE__, __LINE__, parser->pos);
yoonghm 0:46575249ef23 108 return JSMN_ERR_INVAL;
yoonghm 0:46575249ef23 109 }
yoonghm 0:46575249ef23 110 }
yoonghm 0:46575249ef23 111 parser->pos = start;
yoonghm 0:46575249ef23 112 return JSMN_ERR_PART;
yoonghm 0:46575249ef23 113
yoonghm 0:46575249ef23 114 found:
yoonghm 0:46575249ef23 115 if (tokens == NULL)
yoonghm 0:46575249ef23 116 {
yoonghm 0:46575249ef23 117 parser->pos--;
yoonghm 0:46575249ef23 118 return 0;
yoonghm 0:46575249ef23 119 }
yoonghm 0:46575249ef23 120
yoonghm 0:46575249ef23 121 token = jsmn_alloc_token(parser, tokens, num_tokens);
yoonghm 0:46575249ef23 122 if (token == NULL)
yoonghm 0:46575249ef23 123 {
yoonghm 0:46575249ef23 124 parser->pos = start;
yoonghm 0:46575249ef23 125 return JSMN_ERR_NOMEM;
yoonghm 0:46575249ef23 126 }
yoonghm 0:46575249ef23 127 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
yoonghm 0:46575249ef23 128
yoonghm 0:46575249ef23 129 token->parent = parser->toksuper;
yoonghm 0:46575249ef23 130 parser->pos--;
yoonghm 0:46575249ef23 131
yoonghm 0:46575249ef23 132 return 0;
yoonghm 0:46575249ef23 133 }
yoonghm 0:46575249ef23 134
yoonghm 0:46575249ef23 135
yoonghm 0:46575249ef23 136 /**
yoonghm 0:46575249ef23 137 * Fill next token with JSON string.
yoonghm 0:46575249ef23 138 */
yoonghm 0:46575249ef23 139 static int
yoonghm 0:46575249ef23 140 jsmn_parse_string
yoonghm 0:46575249ef23 141 (jsmn_parser *parser
yoonghm 0:46575249ef23 142 ,const char *js
yoonghm 0:46575249ef23 143 ,size_t len
yoonghm 0:46575249ef23 144 ,jsmntok_t *tokens
yoonghm 0:46575249ef23 145 ,size_t num_tokens
yoonghm 0:46575249ef23 146 )
yoonghm 0:46575249ef23 147 {
yoonghm 0:46575249ef23 148 jsmntok_t *token;
yoonghm 0:46575249ef23 149 int start = parser->pos;
yoonghm 0:46575249ef23 150
yoonghm 0:46575249ef23 151 parser->pos++;
yoonghm 0:46575249ef23 152
yoonghm 0:46575249ef23 153 /* Skip starting quote */
yoonghm 0:46575249ef23 154 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
yoonghm 0:46575249ef23 155 {
yoonghm 0:46575249ef23 156 char c = js[parser->pos];
yoonghm 0:46575249ef23 157
yoonghm 0:46575249ef23 158 /* Quote: end of string */
yoonghm 0:46575249ef23 159 if (c == '\"')
yoonghm 0:46575249ef23 160 {
yoonghm 0:46575249ef23 161 if (tokens == NULL)
yoonghm 0:46575249ef23 162 return 0;
yoonghm 0:46575249ef23 163
yoonghm 0:46575249ef23 164 token = jsmn_alloc_token(parser, tokens, num_tokens);
yoonghm 0:46575249ef23 165 if (token == NULL)
yoonghm 0:46575249ef23 166 {
yoonghm 0:46575249ef23 167 parser->pos = start;
yoonghm 0:46575249ef23 168 return JSMN_ERR_NOMEM;
yoonghm 0:46575249ef23 169 }
yoonghm 0:46575249ef23 170
yoonghm 0:46575249ef23 171 jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
yoonghm 0:46575249ef23 172 token->parent = parser->toksuper;
yoonghm 0:46575249ef23 173 return 0;
yoonghm 0:46575249ef23 174 }
yoonghm 0:46575249ef23 175
yoonghm 0:46575249ef23 176 /* Backslash: Quoted symbol expected */
yoonghm 1:70061827a9c8 177 if (c == '\\' && parser->pos + 1 < len)
yoonghm 0:46575249ef23 178 {
yoonghm 1:70061827a9c8 179 int i;
yoonghm 0:46575249ef23 180 parser->pos++;
yoonghm 0:46575249ef23 181 switch (js[parser->pos])
yoonghm 0:46575249ef23 182 {
yoonghm 0:46575249ef23 183 /* Allowed escaped symbols */
yoonghm 0:46575249ef23 184 case '\"':
yoonghm 0:46575249ef23 185 case '/' :
yoonghm 0:46575249ef23 186 case '\\':
yoonghm 0:46575249ef23 187 case 'b' :
yoonghm 0:46575249ef23 188 case 'f' :
yoonghm 0:46575249ef23 189 case 'r' :
yoonghm 0:46575249ef23 190 case 'n' :
yoonghm 0:46575249ef23 191 case 't' :
yoonghm 0:46575249ef23 192 break;
yoonghm 0:46575249ef23 193
yoonghm 0:46575249ef23 194 /* Allows escaped symbol \uXXXX */
yoonghm 0:46575249ef23 195 case 'u':
yoonghm 0:46575249ef23 196 parser->pos++;
yoonghm 1:70061827a9c8 197 for (i = 0;
yoonghm 1:70061827a9c8 198 i < 4 && parser->pos < len && js[parser->pos] != '\0';
yoonghm 1:70061827a9c8 199 i++)
yoonghm 0:46575249ef23 200 {
yoonghm 0:46575249ef23 201 /* If it isn't a hex character we have an ERR */
yoonghm 0:46575249ef23 202 if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
yoonghm 0:46575249ef23 203 (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
yoonghm 0:46575249ef23 204 (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
yoonghm 0:46575249ef23 205 parser->pos = start;
yoonghm 1:70061827a9c8 206 printf("%s:%d: parser->pos=%d\r\n", __FILE__, __LINE__, parser->pos);
yoonghm 0:46575249ef23 207 return JSMN_ERR_INVAL;
yoonghm 0:46575249ef23 208 }
yoonghm 0:46575249ef23 209 parser->pos++;
yoonghm 0:46575249ef23 210 }
yoonghm 0:46575249ef23 211 parser->pos--;
yoonghm 0:46575249ef23 212 break;
yoonghm 0:46575249ef23 213
yoonghm 0:46575249ef23 214 /* Unexpected symbol */
yoonghm 0:46575249ef23 215 default:
yoonghm 0:46575249ef23 216 parser->pos = start;
yoonghm 1:70061827a9c8 217 printf("%s:%d: parser->pos=%d\r\n", __FILE__, __LINE__, parser->pos);
yoonghm 0:46575249ef23 218 return JSMN_ERR_INVAL;
yoonghm 0:46575249ef23 219 }
yoonghm 0:46575249ef23 220 }
yoonghm 0:46575249ef23 221 }
yoonghm 0:46575249ef23 222 parser->pos = start;
yoonghm 0:46575249ef23 223 return JSMN_ERR_PART;
yoonghm 0:46575249ef23 224 }
yoonghm 0:46575249ef23 225
yoonghm 0:46575249ef23 226
yoonghm 0:46575249ef23 227 /**
yoonghm 0:46575249ef23 228 * Parse JSON string and fill tokens.
yoonghm 0:46575249ef23 229 */
yoonghm 0:46575249ef23 230 int
yoonghm 0:46575249ef23 231 jsmn_parse
yoonghm 0:46575249ef23 232 (jsmn_parser *parser
yoonghm 0:46575249ef23 233 ,const char *js
yoonghm 0:46575249ef23 234 ,size_t len
yoonghm 0:46575249ef23 235 ,jsmntok_t *tokens
yoonghm 0:46575249ef23 236 ,unsigned int num_tokens
yoonghm 0:46575249ef23 237 )
yoonghm 0:46575249ef23 238 {
yoonghm 0:46575249ef23 239 int r;
yoonghm 0:46575249ef23 240 int i;
yoonghm 0:46575249ef23 241 jsmntok_t *token;
yoonghm 0:46575249ef23 242 int count = 0;
yoonghm 0:46575249ef23 243
yoonghm 0:46575249ef23 244 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
yoonghm 0:46575249ef23 245 {
yoonghm 0:46575249ef23 246 char c;
yoonghm 0:46575249ef23 247 jsmntype_t type = JSMN_INVALID;
yoonghm 0:46575249ef23 248
yoonghm 0:46575249ef23 249 c = js[parser->pos];
yoonghm 0:46575249ef23 250 switch (c)
yoonghm 0:46575249ef23 251 {
yoonghm 0:46575249ef23 252 case '{' :
yoonghm 0:46575249ef23 253 case '[' :
yoonghm 0:46575249ef23 254 count++;
yoonghm 0:46575249ef23 255 if (tokens == NULL)
yoonghm 0:46575249ef23 256 break;
yoonghm 0:46575249ef23 257
yoonghm 0:46575249ef23 258 token = jsmn_alloc_token(parser, tokens, num_tokens);
yoonghm 0:46575249ef23 259
yoonghm 0:46575249ef23 260 if (token == NULL)
yoonghm 0:46575249ef23 261 return JSMN_ERR_NOMEM;
yoonghm 0:46575249ef23 262
yoonghm 0:46575249ef23 263 if (parser->toksuper != -1)
yoonghm 0:46575249ef23 264 {
yoonghm 0:46575249ef23 265 tokens[parser->toksuper].size++;
yoonghm 0:46575249ef23 266 token->parent = parser->toksuper;
yoonghm 0:46575249ef23 267 }
yoonghm 0:46575249ef23 268 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
yoonghm 0:46575249ef23 269 token->start = parser->pos;
yoonghm 0:46575249ef23 270 parser->toksuper = parser->toknext - 1;
yoonghm 0:46575249ef23 271 break;
yoonghm 0:46575249ef23 272
yoonghm 0:46575249ef23 273 case '}':
yoonghm 0:46575249ef23 274 case ']':
yoonghm 0:46575249ef23 275 if (tokens == NULL)
yoonghm 0:46575249ef23 276 break;
yoonghm 0:46575249ef23 277
yoonghm 0:46575249ef23 278 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
yoonghm 0:46575249ef23 279
yoonghm 1:70061827a9c8 280 if (parser->toknext < 1) {
yoonghm 1:70061827a9c8 281 printf("%s:%d: parser->pos=%d\r\n", __FILE__, __LINE__, parser->pos);
yoonghm 0:46575249ef23 282 return JSMN_ERR_INVAL;
yoonghm 1:70061827a9c8 283 }
yoonghm 0:46575249ef23 284
yoonghm 0:46575249ef23 285 token = &tokens[parser->toknext - 1];
yoonghm 0:46575249ef23 286 for (;;)
yoonghm 0:46575249ef23 287 {
yoonghm 0:46575249ef23 288 if (token->start != -1 && token->end == -1)
yoonghm 0:46575249ef23 289 {
yoonghm 1:70061827a9c8 290 if (token->type != type) {
yoonghm 1:70061827a9c8 291 printf("%s:%d: parser->pos=%d\r\n", __FILE__, __LINE__, parser->pos);
yoonghm 0:46575249ef23 292 return JSMN_ERR_INVAL;
yoonghm 1:70061827a9c8 293 }
yoonghm 0:46575249ef23 294
yoonghm 0:46575249ef23 295 token->end = parser->pos + 1;
yoonghm 0:46575249ef23 296 parser->toksuper = token->parent;
yoonghm 0:46575249ef23 297 break;
yoonghm 0:46575249ef23 298 }
yoonghm 0:46575249ef23 299
yoonghm 0:46575249ef23 300 if (token->parent == -1)
yoonghm 0:46575249ef23 301 break;
yoonghm 0:46575249ef23 302
yoonghm 0:46575249ef23 303 token = &tokens[token->parent];
yoonghm 0:46575249ef23 304 }
yoonghm 0:46575249ef23 305 break;
yoonghm 0:46575249ef23 306
yoonghm 0:46575249ef23 307 case '\"':
yoonghm 0:46575249ef23 308 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
yoonghm 0:46575249ef23 309 if (r < 0)
yoonghm 0:46575249ef23 310 return r;
yoonghm 0:46575249ef23 311
yoonghm 0:46575249ef23 312 count++;
yoonghm 0:46575249ef23 313 if (parser->toksuper != -1 && tokens != NULL)
yoonghm 0:46575249ef23 314 tokens[parser->toksuper].size++;
yoonghm 0:46575249ef23 315 break;
yoonghm 0:46575249ef23 316
yoonghm 0:46575249ef23 317 case '\t':
yoonghm 0:46575249ef23 318 case '\r':
yoonghm 0:46575249ef23 319 case '\n':
yoonghm 1:70061827a9c8 320 case ' ' :
yoonghm 1:70061827a9c8 321 break;
yoonghm 1:70061827a9c8 322
yoonghm 0:46575249ef23 323 case ':' :
yoonghm 1:70061827a9c8 324 parser->toksuper = parser->toknext - 1;
yoonghm 1:70061827a9c8 325 break;
yoonghm 1:70061827a9c8 326
yoonghm 0:46575249ef23 327 case ',' :
yoonghm 1:70061827a9c8 328 if (tokens != NULL &&
yoonghm 1:70061827a9c8 329 tokens[parser->toksuper].type != JSMN_ARRAY &&
yoonghm 1:70061827a9c8 330 tokens[parser->toksuper].type != JSMN_OBJECT)
yoonghm 1:70061827a9c8 331 {
yoonghm 1:70061827a9c8 332 parser->toksuper = tokens[parser->toksuper].parent;
yoonghm 1:70061827a9c8 333 }
yoonghm 0:46575249ef23 334 break;
yoonghm 0:46575249ef23 335
yoonghm 0:46575249ef23 336 case '-' :
yoonghm 0:46575249ef23 337 case '0' :
yoonghm 0:46575249ef23 338 case '1' :
yoonghm 0:46575249ef23 339 case '2' :
yoonghm 0:46575249ef23 340 case '3' :
yoonghm 0:46575249ef23 341 case '4' :
yoonghm 0:46575249ef23 342 case '5' :
yoonghm 0:46575249ef23 343 case '6' :
yoonghm 0:46575249ef23 344 case '7' :
yoonghm 0:46575249ef23 345 case '8' :
yoonghm 0:46575249ef23 346 case '9' :
yoonghm 0:46575249ef23 347 case 't' :
yoonghm 0:46575249ef23 348 case 'f' :
yoonghm 0:46575249ef23 349 case 'n' :
yoonghm 1:70061827a9c8 350 /* And they must not be keys of the object */
yoonghm 1:70061827a9c8 351 if (tokens != NULL)
yoonghm 1:70061827a9c8 352 {
yoonghm 1:70061827a9c8 353 jsmntok_t *t = &tokens[parser->toksuper];
yoonghm 1:70061827a9c8 354 if ( t->type == JSMN_OBJECT ||
yoonghm 1:70061827a9c8 355 (t->type == JSMN_STRING && t->size != 0))
yoonghm 1:70061827a9c8 356 {
yoonghm 1:70061827a9c8 357 printf("%s:%d: js[parser->pos]=%c\r\n", __FILE__, __LINE__, js[parser->pos]);
yoonghm 1:70061827a9c8 358 return JSMN_ERR_INVAL;
yoonghm 1:70061827a9c8 359 }
yoonghm 1:70061827a9c8 360 }
yoonghm 0:46575249ef23 361 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
yoonghm 0:46575249ef23 362 if (r < 0)
yoonghm 0:46575249ef23 363 return r;
yoonghm 0:46575249ef23 364
yoonghm 0:46575249ef23 365 count++;
yoonghm 0:46575249ef23 366
yoonghm 0:46575249ef23 367 if (parser->toksuper != -1 && tokens != NULL)
yoonghm 0:46575249ef23 368 tokens[parser->toksuper].size++;
yoonghm 0:46575249ef23 369 break;
yoonghm 0:46575249ef23 370
yoonghm 0:46575249ef23 371 /* Unexpected char in strict mode */
yoonghm 0:46575249ef23 372 default:
yoonghm 1:70061827a9c8 373 printf("%s:%d: js[parser->pos]=%c\r\n", __FILE__, __LINE__, js[parser->pos]);
yoonghm 0:46575249ef23 374 return JSMN_ERR_INVAL;
yoonghm 0:46575249ef23 375 }
yoonghm 0:46575249ef23 376 }
yoonghm 0:46575249ef23 377
yoonghm 0:46575249ef23 378 for (i = parser->toknext - 1; i >= 0; i--)
yoonghm 0:46575249ef23 379 {
yoonghm 0:46575249ef23 380 /* Unmatched opened object or array */
yoonghm 0:46575249ef23 381 if (tokens[i].start != -1 && tokens[i].end == -1)
yoonghm 0:46575249ef23 382 {
yoonghm 0:46575249ef23 383 return JSMN_ERR_PART;
yoonghm 0:46575249ef23 384 }
yoonghm 0:46575249ef23 385 }
yoonghm 0:46575249ef23 386
yoonghm 0:46575249ef23 387 return count;
yoonghm 0:46575249ef23 388 }
yoonghm 0:46575249ef23 389
yoonghm 0:46575249ef23 390 /**
yoonghm 0:46575249ef23 391 * Creates a new parser based over a given buffer with an array of tokens
yoonghm 0:46575249ef23 392 * available.
yoonghm 0:46575249ef23 393 */
yoonghm 0:46575249ef23 394 void
yoonghm 0:46575249ef23 395 jsmn_init
yoonghm 0:46575249ef23 396 (jsmn_parser *parser)
yoonghm 0:46575249ef23 397 {
yoonghm 0:46575249ef23 398 parser->pos = 0;
yoonghm 0:46575249ef23 399 parser->toknext = 0;
yoonghm 0:46575249ef23 400 parser->toksuper = -1;
yoonghm 0:46575249ef23 401 }