My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Classes | Macros | Typedefs | Functions
jsmn_parser.h File Reference

General-purpose function to interact with JSMN library, and create objects (potentially with circular references) from the parsed json. More...

#include "jsmn.h"
#include <stdlib.h>
#include <stdio.h>

Go to the source code of this file.

Classes

struct  json_state
 Structure to hold state while parsing JSON. More...
 
struct  key_mapping
 Structure to map a key + state to a function. More...
 

Macros

#define ASSERT_JSMN_TYPE(tok, toktype, j)
 Asserts that the type of the current token is the provided type, and logs an error if that is not the case. More...
 
#define PARSE_FN(fn_name)
 Macro for defining a jsmn_parsing_fn. More...
 
#define GET_PARSE_OBJ()   (in__->data)
 Within a PARSE_FN, gets the object currently being constructed. More...
 
#define GET_STR_TOK()   tok_to_str(*tok__, j__)
 Within a PARSE_FN, gets the token being read as a string. More...
 
#define GET_INT_TOK()   tok_to_int(*tok__, j__)
 Within a PARSE_FN, gets the token being read as an integer. More...
 
#define GET_LONG_TOK()   tok_to_long(*tok__, j__)
 Within a PARSE_FN, gets the token being read as a long. More...
 
#define PARSE_OBJ_LIST_FN(fn_name, init_fn)
 Macro for parsing a list of objects in a json file. More...
 
#define INIT_OBJ_FN(fn_name)   static struct json_state fn_name(struct json_state *in__, int index__)
 Macro for instantiating a new struct based on the appearance of a new JSON object. More...
 
#define GET_OBJ_INDEX()   index__
 In an INIT_OBJ_FN, returns the index of the currently parsed object list. More...
 
#define RETURN_OBJ(data__, type__)
 Should be the last line in an INIT_OBJ_FN. More...
 
#define PARSE_OBJ_FN(fn_name, parent_obj_type, parent_obj_field, new_type)
 Macro to descend into a JSON object and the corresponding C struct for parsing. More...
 
#define START_ITER_TOK_LIST(i)
 Macro to iterate over a list of tokens. More...
 
#define END_ITER_TOK_LIST(i)   *tok__ = (*tok__) - (tok_size__ - i + 1);
 Macro that should appear at the end of the iteration of a list of tokens. More...
 

Typedefs

typedef int(* jsmn_parsing_fn )(jsmntok_t **tok, char *j, struct json_state *state, struct json_state **saved)
 Typedef for a jsmn parsing function. More...
 
typedef struct json_state(* jsmn_initializer )(struct json_state *state, int index)
 Typedef for a json_state initializer, used when iterating over lists of objects. More...
 

Functions

int parse_file_into_obj (const char *filename, void *obj, struct key_mapping *km)
 Using the provided functions, parses the JSON present in 'filename' and stores the resulting object in 'obj'. More...
 
int parse_str_into_obj (char *contents, void *obj, struct key_mapping *km)
 Using the provided functions, parses the JSON present in in the provided string and stores the resulting object in 'obj'. More...
 
int jsmn_ignore (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
 Can be used as a jsmn_parsing_fn that ignores any value passed into it. More...
 
int parse_jsmn_obj (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
 SHOULD NOT BE USED DIRECTLY. More...
 
int parse_jsmn_obj_list (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved, jsmn_initializer init)
 SHOULD NOT BE USED DIRECTLY. More...
 
void * get_root_jsmn_obj ()
 Returns the initial object passed into the JSON parser. More...
 
char * tok_to_str (jsmntok_t *tok, char *j)
 SHOULD NOT BE USED DIRECTLT. More...
 
int tok_to_int (jsmntok_t *tok, char *j)
 SHOULD NOT BE USED DIRECTLY. More...
 
long tok_to_long (jsmntok_t *tok, char *j)
 SHOULD NOT BE USED DIRECTLY. More...
 

Detailed Description

General-purpose function to interact with JSMN library, and create objects (potentially with circular references) from the parsed json.

Definition in file jsmn_parser.h.

Macro Definition Documentation

#define ASSERT_JSMN_TYPE (   tok,
  toktype,
 
)
Value:
if ((tok)->type != toktype){ \
log_error("Got type %d, expected %d while parsing %.50s", \
(tok)->type, toktype, &j[(tok)->start]); \
return -1; \
} \
#define log_error(fmt,...)
Definition: logging.h:101

Asserts that the type of the current token is the provided type, and logs an error if that is not the case.

Definition at line 39 of file jsmn_parser.h.

#define END_ITER_TOK_LIST (   i)    *tok__ = (*tok__) - (tok_size__ - i + 1);

Macro that should appear at the end of the iteration of a list of tokens.

Parameters
iThe same integer which was passed into START_ITER_TOK_LIST

Definition at line 172 of file jsmn_parser.h.

#define GET_INT_TOK ( )    tok_to_int(*tok__, j__)

Within a PARSE_FN, gets the token being read as an integer.

Definition at line 89 of file jsmn_parser.h.

#define GET_LONG_TOK ( )    tok_to_long(*tok__, j__)

Within a PARSE_FN, gets the token being read as a long.

Definition at line 93 of file jsmn_parser.h.

#define GET_OBJ_INDEX ( )    index__

In an INIT_OBJ_FN, returns the index of the currently parsed object list.

Definition at line 120 of file jsmn_parser.h.

#define GET_PARSE_OBJ ( )    (in__->data)

Within a PARSE_FN, gets the object currently being constructed.

Definition at line 81 of file jsmn_parser.h.

#define GET_STR_TOK ( )    tok_to_str(*tok__, j__)

Within a PARSE_FN, gets the token being read as a string.

Definition at line 85 of file jsmn_parser.h.

#define INIT_OBJ_FN (   fn_name)    static struct json_state fn_name(struct json_state *in__, int index__)

Macro for instantiating a new struct based on the appearance of a new JSON object.

Parameters
fn_nameName for the function, which should subsequently be defined to create a new object, which should be returned with RETURN_OBJ

Definition at line 114 of file jsmn_parser.h.

#define PARSE_FN (   fn_name)
Value:
static int fn_name(jsmntok_t **tok__, char *j__, \
struct json_state *in__, struct json_state **saved__)
JSON token description.
Definition: jsmn.h:61
Structure to hold state while parsing JSON.
Definition: jsmn_parser.h:49

Macro for defining a jsmn_parsing_fn.

Each key in the JSON file should have an associated function defined using this macro.

Parameters
fn_nameThe name of the function to define, which should return 0 on success, -1 on error, 1 to defer (see jsmn_parsing_fn)

Definition at line 76 of file jsmn_parser.h.

#define PARSE_OBJ_FN (   fn_name,
  parent_obj_type,
  parent_obj_field,
  new_type 
)
Value:
static int fn_name(jsmntok_t **tok__, char *j__, \
struct json_state *in__, struct json_state **saved__) { \
parent_obj_type *type_out__ = in__->data; \
struct json_state state_out__ = { \
.data = &type_out__->parent_obj_field, \
.parent_type = new_type, \
.tok = *tok__ \
}; \
if (parse_jsmn_obj(tok__, j__, &state_out__, saved__) < 0) { \
return -1; \
} \
return 0; \
}
JSON token description.
Definition: jsmn.h:61
Structure to hold state while parsing JSON.
Definition: jsmn_parser.h:49
void * data
The object currently under construction.
Definition: jsmn_parser.h:50
int parse_jsmn_obj(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:358

Macro to descend into a JSON object and the corresponding C struct for parsing.

Parameters
fn_nameThe name of the function to later reference in the key_mapping
parent_obj_typeThe typename of the parent C struct already being parsed
parent_obj_fieldThe field name of the parent C struct which should be descended into
new_typeAn identifier (enumerator) for the new type of object being created

Definition at line 143 of file jsmn_parser.h.

#define PARSE_OBJ_LIST_FN (   fn_name,
  init_fn 
)
Value:
static int fn_name(jsmntok_t **tok__, char *j__, \
struct json_state *in__, struct json_state **saved__) { \
return parse_jsmn_obj_list(tok__, j__, in__, saved__, init_fn); \
}
JSON token description.
Definition: jsmn.h:61
int parse_jsmn_obj_list(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved, jsmn_initializer init)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:318
Structure to hold state while parsing JSON.
Definition: jsmn_parser.h:49

Macro for parsing a list of objects in a json file.

For each element that is encountered, init_fn will be called to construct the object which is passed in to each of the keys within that object

Parameters
fn_nameA name for the function to define
init_fnAn INIT_OBJ_FN to create the new object

Definition at line 103 of file jsmn_parser.h.

#define RETURN_OBJ (   data__,
  type__ 
)
Value:
struct json_state out_obj__ = { \
.data = data__, \
.parent_type = type__ \
}; \
return out_obj__;
Structure to hold state while parsing JSON.
Definition: jsmn_parser.h:49
void * data
The object currently under construction.
Definition: jsmn_parser.h:50

Should be the last line in an INIT_OBJ_FN.

Returns the newly-created object.

Parameters
data__A pointer to the newly-created object which should be parsed into
type__The enumerator for the type of object which is new being parsed

Definition at line 128 of file jsmn_parser.h.

#define START_ITER_TOK_LIST (   i)
Value:
int tok_size__ = (*tok__)->size; \
for (i = 0, ++(*tok__); i< tok_size__; ++(*tok__), i++)

Macro to iterate over a list of tokens.

Once inside the iteration, can get the current token with GET_*_TOK(). Iteration should be ended with END_ITER_TOK_LIST

Parameters
iAn integer into which to place the current index

Definition at line 164 of file jsmn_parser.h.

Typedef Documentation

typedef struct json_state(* jsmn_initializer)(struct json_state *state, int index)

Typedef for a json_state initializer, used when iterating over lists of objects.

Parameters
state- current state being parsed
index- index into the current list
Returns
a new struct with the new data item of interest

Definition at line 223 of file jsmn_parser.h.

typedef int(* jsmn_parsing_fn)(jsmntok_t **tok, char *j, struct json_state *state, struct json_state **saved)

Typedef for a jsmn parsing function.

Each element in the key mapping should have one of these as the third element.

Parameters
tok- current token to be parsed
j- entire JSON string
state- current state being parsed
saved- items that have been deferred to parse until later are stored here
Returns
0 on success, -1 on error, 1 to defer parsing until a future pass

Definition at line 66 of file jsmn_parser.h.

Function Documentation

void* get_root_jsmn_obj ( )

Returns the initial object passed into the JSON parser.

Definition at line 92 of file jsmn_parser.c.

int jsmn_ignore ( jsmntok_t **  tok,
char *  j,
struct json_state in,
struct json_state **  saved 
)

Can be used as a jsmn_parsing_fn that ignores any value passed into it.

Can be used as a jsmn_parsing_fn that ignores any value passed into it.

Definition at line 120 of file jsmn_parser.c.

int parse_file_into_obj ( const char *  filename,
void *  obj,
struct key_mapping km 
)

Using the provided functions, parses the JSON present in 'filename' and stores the resulting object in 'obj'.

Can iterate multiple times on tokens which have dependencies.

Parameters
filename- JSON file to be parsed
obj- object to be filled with the results of parsing
keymap- the mapping between the JSON key and the function to be called
Returns
0 on success, -1 on error

Definition at line 135 of file jsmn_parser.c.

int parse_jsmn_obj ( jsmntok_t **  tok,
char *  j,
struct json_state state,
struct json_state **  saved 
)

SHOULD NOT BE USED DIRECTLY.

SHOULD NOT BE USED DIRECTLY.

Parameters
tok- pointer to current token to be parsed (advances automatically)
j- entire json string being parsed
state- curernt state of parsing, including the data structure being parsed into
saved- JSON entries on which parsing has been deferred for later

Definition at line 358 of file jsmn_parser.c.

int parse_jsmn_obj_list ( jsmntok_t **  tok,
char *  j,
struct json_state state,
struct json_state **  saved,
jsmn_initializer  init 
)

SHOULD NOT BE USED DIRECTLY.

SHOULD NOT BE USED DIRECTLY.

Calls init() for each new object, then passes the returned json_state to the next parsed object.

Parameters
tok- the current jsmn token to parse
j- the entire json string
state- the current state of parsing
saved- list of states to be re-interpreted laster
init- initialization function, returning a struct json_state
Returns
0 on success, -1 on failure

Definition at line 318 of file jsmn_parser.c.

int parse_str_into_obj ( char *  contents,
void *  obj,
struct key_mapping km 
)

Using the provided functions, parses the JSON present in in the provided string and stores the resulting object in 'obj'.

Can iterate multiple times on tokens which have dependencies.

Parameters
contents- Null-terminated string to be parsed
obj- object to be filled with the results of parsing
keymap- the mapping between the JSON key and the function to be called
Returns
0 on success, -1 on error

Definition at line 171 of file jsmn_parser.c.

int tok_to_int ( jsmntok_t tok,
char *  j 
)

SHOULD NOT BE USED DIRECTLY.

SHOULD NOT BE USED DIRECTLY.

Sets the "end" char to \0, and converts converts the resulting string to an integer

Parameters
tok- JSMN token to extract
j- original json string
Returns
- integer

Definition at line 42 of file jsmn_parser.c.

long tok_to_long ( jsmntok_t tok,
char *  j 
)

SHOULD NOT BE USED DIRECTLY.

SHOULD NOT BE USED DIRECTLY.

Sets the "end" char to \0, and converts converts the resulting string to a long integer

Parameters
tok- JSMN token to extract
j- original json string
Returns
- long

Definition at line 55 of file jsmn_parser.c.

char* tok_to_str ( jsmntok_t tok,
char *  j 
)

SHOULD NOT BE USED DIRECTLT.

SHOULD NOT BE USED DIRECTLT.

Sets the "end" char to \0, and returns a pointer to the start

Parameters
tok- JSMN token to extract
j- original json string
Returns
- null-terminated char *

Definition at line 69 of file jsmn_parser.c.