General-purpose function to interact with JSMN library, and create objects (potentially with circular references) from the parsed json. More...
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... | |
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.
#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.
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.
i | The 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.
fn_name | Name 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 | ) |
Macro for defining a jsmn_parsing_fn.
Each key in the JSON file should have an associated function defined using this macro.
fn_name | The 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 | |||
) |
Macro to descend into a JSON object and the corresponding C struct for parsing.
fn_name | The name of the function to later reference in the key_mapping |
parent_obj_type | The typename of the parent C struct already being parsed |
parent_obj_field | The field name of the parent C struct which should be descended into |
new_type | An 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 | |||
) |
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
fn_name | A name for the function to define |
init_fn | An INIT_OBJ_FN to create the new object |
Definition at line 103 of file jsmn_parser.h.
#define RETURN_OBJ | ( | data__, | |
type__ | |||
) |
Should be the last line in an INIT_OBJ_FN.
Returns the newly-created object.
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 | ) |
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
i | An integer into which to place the current index |
Definition at line 164 of file jsmn_parser.h.
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.
state | - current state being parsed |
index | - index into the current list |
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.
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 |
Definition at line 66 of file jsmn_parser.h.
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.
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 |
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.
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.
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 |
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.
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 |
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
tok | - JSMN token to extract |
j | - original json string |
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
tok | - JSMN token to extract |
j | - original json string |
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
tok | - JSMN token to extract |
j | - original json string |
Definition at line 69 of file jsmn_parser.c.