My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions | Variables
jsmn_parser.c 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 "jsmn_parser.h"
#include "logging.h"
#include <string.h>
#include <strings.h>

Go to the source code of this file.

Macros

#define MAX_RETRIES   1024
 The maximum number of times that a PARSE_FN can return 1 before an error is raised. More...
 

Functions

int tok_to_int (jsmntok_t *tok, char *j)
 Destructively extracts an int from a jsmn token. More...
 
long tok_to_long (jsmntok_t *tok, char *j)
 Destructively extracts an int from a jsmn token. More...
 
char * tok_to_str (jsmntok_t *tok, char *j)
 Destructively extracts a c-string from a jsmn token. More...
 
static int retry_saved (struct json_state **saved, char *j)
 Retries tokens which have been deferred. More...
 
void * get_root_jsmn_obj ()
 Returns the initial object passed into the JSON parser. More...
 
int jsmn_ignore_list (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
 
int jsmn_ignore_obj (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
 
int jsmn_ignore (jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
 Ignores a JSMN value. More...
 
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...
 
static jsmn_parsing_fn get_parse_fn (char *key, int parent_type)
 Returns the function to parse a given key. More...
 
int parse_jsmn_obj_list (jsmntok_t **tok, char *j, struct json_state *state, struct json_state **saved, jsmn_initializer init)
 Parses an array of JSMN objects. More...
 
int parse_jsmn_obj (jsmntok_t **tok, char *j, struct json_state *state, struct json_state **saved)
 Parses a single JSMN object utilizing the provided (global) key map. More...
 

Variables

static struct key_mappingjsmn_key_map
 Global key mapping, giving which function should be called for each JSMN key. More...
 
static void * global_obj
 The object which is pased into the JSON parser to be filled. 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.c.

Macro Definition Documentation

#define MAX_RETRIES   1024

The maximum number of times that a PARSE_FN can return 1 before an error is raised.

Definition at line 168 of file jsmn_parser.c.

Function Documentation

static jsmn_parsing_fn get_parse_fn ( char *  key,
int  parent_type 
)
static

Returns the function to parse a given key.

Parameters
key- the JSON key to parse
parent_type- the type of object in which this key is located
Returns
reference to function for parsing this key

Definition at line 235 of file jsmn_parser.c.

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 
)

Ignores a JSMN value.

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 jsmn_ignore_list ( jsmntok_t **  tok,
char *  j,
struct json_state in,
struct json_state **  saved 
)

Definition at line 96 of file jsmn_parser.c.

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

Definition at line 106 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 
)

Parses a single JSMN object utilizing the provided (global) key map.

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 
)

Parses an array of JSMN objects.

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.

static int retry_saved ( struct json_state **  saved_states,
char *  j 
)
static

Retries tokens which have been deferred.

Retries the states that failed to parse the first time due to dependencies.

Parameters
saved_states- the states which failed to parse the first time (returned 1)
j- the entire JSON string
Returns
0 if at least one saved state was parsed, -1 otherwise

Definition at line 256 of file jsmn_parser.c.

int tok_to_int ( jsmntok_t tok,
char *  j 
)

Destructively extracts an int from a jsmn token.

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 
)

Destructively extracts an int from a jsmn token.

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 
)

Destructively extracts a c-string from a jsmn token.

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.

Variable Documentation

void* global_obj
static

The object which is pased into the JSON parser to be filled.

Definition at line 87 of file jsmn_parser.c.

struct key_mapping* jsmn_key_map
static

Global key mapping, giving which function should be called for each JSMN key.

Definition at line 77 of file jsmn_parser.c.