PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
jsmn.h File Reference
#include <stddef.h>

Go to the source code of this file.

Classes

struct  jsmntok_t
 
struct  jsmn_parser
 

Macros

#define JSMN_API   extern
 

Enumerations

enum  jsmntype_t {
  JSMN_UNDEFINED = 0 , JSMN_OBJECT = 1 , JSMN_ARRAY = 2 , JSMN_STRING = 3 ,
  JSMN_PRIMITIVE = 4
}
 
enum  jsmnerr { JSMN_ERROR_NOMEM = -1 , JSMN_ERROR_INVAL = -2 , JSMN_ERROR_PART = -3 }
 

Functions

JSMN_API void jsmn_init (jsmn_parser *parser)
 
JSMN_API int jsmn_parse (jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens)
 

Macro Definition Documentation

◆ JSMN_API

#define JSMN_API   extern

Definition at line 36 of file jsmn.h.

Enumeration Type Documentation

◆ jsmnerr

enum jsmnerr
Enumerator
JSMN_ERROR_NOMEM 
JSMN_ERROR_INVAL 
JSMN_ERROR_PART 

Definition at line 54 of file jsmn.h.

54 {
55 /* Not enough tokens were provided */
57 /* Invalid character inside JSON string */
59 /* The string is not a full JSON packet, more bytes expected */
61};
@ JSMN_ERROR_INVAL
Definition jsmn.h:58
@ JSMN_ERROR_PART
Definition jsmn.h:60
@ JSMN_ERROR_NOMEM
Definition jsmn.h:56

◆ jsmntype_t

enum jsmntype_t

JSON type identifier. Basic types are: o Object o Array o String o Other primitive: number, boolean (true/false) or null

Enumerator
JSMN_UNDEFINED 
JSMN_OBJECT 
JSMN_ARRAY 
JSMN_STRING 
JSMN_PRIMITIVE 

Definition at line 46 of file jsmn.h.

46 {
48 JSMN_OBJECT = 1,
49 JSMN_ARRAY = 2,
50 JSMN_STRING = 3,
jsmntype_t
Definition jsmn.h:46
@ JSMN_PRIMITIVE
Definition jsmn.h:51
@ JSMN_OBJECT
Definition jsmn.h:48
@ JSMN_UNDEFINED
Definition jsmn.h:47
@ JSMN_ARRAY
Definition jsmn.h:49
@ JSMN_STRING
Definition jsmn.h:50

Function Documentation

◆ jsmn_init()

JSMN_API void jsmn_init ( jsmn_parser parser)

Create JSON parser over an array of tokens

Creates a new parser based over a given buffer with an array of tokens available.

Definition at line 456 of file jsmn.h.

456 {
457 parser->pos = 0;
458 parser->toknext = 0;
459 parser->toksuper = -1;
460}
unsigned int pos
Definition jsmn.h:84
int toksuper
Definition jsmn.h:86
unsigned int toknext
Definition jsmn.h:85

References jsmn_parser::pos, jsmn_parser::toknext, and jsmn_parser::toksuper.

Referenced by PEBLUtility::ParseJSON().

◆ jsmn_parse()

JSMN_API int jsmn_parse ( jsmn_parser parser,
const char *  js,
const size_t  len,
jsmntok_t tokens,
const unsigned int  num_tokens 
)

Run JSON parser. It parses a JSON data string into and array of tokens, each describing a single JSON object.

Parse JSON string and fill tokens.

Definition at line 265 of file jsmn.h.

266 {
267 int r;
268 int i;
269 jsmntok_t *token;
270 int count = parser->toknext;
271
272 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
273 char c;
274 jsmntype_t type;
275
276 c = js[parser->pos];
277 switch (c) {
278 case '{':
279 case '[':
280 count++;
281 if (tokens == NULL) {
282 break;
283 }
284 token = jsmn_alloc_token(parser, tokens, num_tokens);
285 if (token == NULL) {
286 return JSMN_ERROR_NOMEM;
287 }
288 if (parser->toksuper != -1) {
289 jsmntok_t *t = &tokens[parser->toksuper];
290#ifdef JSMN_STRICT
291 /* In strict mode an object or array can't become a key */
292 if (t->type == JSMN_OBJECT) {
293 return JSMN_ERROR_INVAL;
294 }
295#endif
296 t->size++;
297#ifdef JSMN_PARENT_LINKS
298 token->parent = parser->toksuper;
299#endif
300 }
301 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
302 token->start = parser->pos;
303 parser->toksuper = parser->toknext - 1;
304 break;
305 case '}':
306 case ']':
307 if (tokens == NULL) {
308 break;
309 }
310 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
311#ifdef JSMN_PARENT_LINKS
312 if (parser->toknext < 1) {
313 return JSMN_ERROR_INVAL;
314 }
315 token = &tokens[parser->toknext - 1];
316 for (;;) {
317 if (token->start != -1 && token->end == -1) {
318 if (token->type != type) {
319 return JSMN_ERROR_INVAL;
320 }
321 token->end = parser->pos + 1;
322 parser->toksuper = token->parent;
323 break;
324 }
325 if (token->parent == -1) {
326 if (token->type != type || parser->toksuper == -1) {
327 return JSMN_ERROR_INVAL;
328 }
329 break;
330 }
331 token = &tokens[token->parent];
332 }
333#else
334 for (i = parser->toknext - 1; i >= 0; i--) {
335 token = &tokens[i];
336 if (token->start != -1 && token->end == -1) {
337 if (token->type != type) {
338 return JSMN_ERROR_INVAL;
339 }
340 parser->toksuper = -1;
341 token->end = parser->pos + 1;
342 break;
343 }
344 }
345 /* Error if unmatched closing bracket */
346 if (i == -1) {
347 return JSMN_ERROR_INVAL;
348 }
349 for (; i >= 0; i--) {
350 token = &tokens[i];
351 if (token->start != -1 && token->end == -1) {
352 parser->toksuper = i;
353 break;
354 }
355 }
356#endif
357 break;
358 case '\"':
359 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
360 if (r < 0) {
361 return r;
362 }
363 count++;
364 if (parser->toksuper != -1 && tokens != NULL) {
365 tokens[parser->toksuper].size++;
366 }
367 break;
368 case '\t':
369 case '\r':
370 case '\n':
371 case ' ':
372 break;
373 case ':':
374 parser->toksuper = parser->toknext - 1;
375 break;
376 case ',':
377 if (tokens != NULL && parser->toksuper != -1 &&
378 tokens[parser->toksuper].type != JSMN_ARRAY &&
379 tokens[parser->toksuper].type != JSMN_OBJECT) {
380#ifdef JSMN_PARENT_LINKS
381 parser->toksuper = tokens[parser->toksuper].parent;
382#else
383 for (i = parser->toknext - 1; i >= 0; i--) {
384 if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
385 if (tokens[i].start != -1 && tokens[i].end == -1) {
386 parser->toksuper = i;
387 break;
388 }
389 }
390 }
391#endif
392 }
393 break;
394#ifdef JSMN_STRICT
395 /* In strict mode primitives are: numbers and booleans */
396 case '-':
397 case '0':
398 case '1':
399 case '2':
400 case '3':
401 case '4':
402 case '5':
403 case '6':
404 case '7':
405 case '8':
406 case '9':
407 case 't':
408 case 'f':
409 case 'n':
410 /* And they must not be keys of the object */
411 if (tokens != NULL && parser->toksuper != -1) {
412 const jsmntok_t *t = &tokens[parser->toksuper];
413 if (t->type == JSMN_OBJECT ||
414 (t->type == JSMN_STRING && t->size != 0)) {
415 return JSMN_ERROR_INVAL;
416 }
417 }
418#else
419 /* In non-strict mode every unquoted value is a primitive */
420 default:
421#endif
422 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
423 if (r < 0) {
424 return r;
425 }
426 count++;
427 if (parser->toksuper != -1 && tokens != NULL) {
428 tokens[parser->toksuper].size++;
429 }
430 break;
431
432#ifdef JSMN_STRICT
433 /* Unexpected char in strict mode */
434 default:
435 return JSMN_ERROR_INVAL;
436#endif
437 }
438 }
439
440 if (tokens != NULL) {
441 for (i = parser->toknext - 1; i >= 0; i--) {
442 /* Unmatched opened object or array */
443 if (tokens[i].start != -1 && tokens[i].end == -1) {
444 return JSMN_ERROR_PART;
445 }
446 }
447 }
448
449 return count;
450}
#define NULL
Definition BinReloc.cpp:317
int start
Definition jsmn.h:71
int size
Definition jsmn.h:73
int end
Definition jsmn.h:72
jsmntype_t type
Definition jsmn.h:70
int count
Definition test.cpp:12

References count, jsmntok_t::end, JSMN_ARRAY, JSMN_ERROR_INVAL, JSMN_ERROR_NOMEM, JSMN_ERROR_PART, JSMN_OBJECT, JSMN_STRING, NULL, jsmn_parser::pos, jsmntok_t::size, jsmntok_t::start, jsmn_parser::toknext, jsmn_parser::toksuper, and jsmntok_t::type.

Referenced by PEBLUtility::ParseJSON().