GFXprim
2D bitmap graphics library with emphasis on speed and correctness
|
A JSON parser. More...
Go to the source code of this file.
Data Structures | |
struct | gp_json_reader |
A JSON parser internal state. More... | |
struct | gp_json_val |
A parsed JSON key value pair. More... | |
struct | gp_json_obj_attr |
A JSON object attribute description i.e. key and type. More... | |
struct | gp_json_obj |
A JSON object description. More... | |
struct | gp_json_state |
A JSON parser state. More... | |
Typedefs | |
typedef struct gp_json_val | gp_json_val |
A parsed JSON key value pair. | |
typedef struct gp_json_obj_attr | gp_json_obj_attr |
A JSON object attribute description i.e. key and type. | |
typedef struct gp_json_obj | gp_json_obj |
A JSON object description. | |
typedef struct gp_json_state | gp_json_state |
A JSON parser state. | |
Functions | |
gp_json_val * | gp_json_val_alloc (size_t buf_size) |
Allocates a JSON value. | |
void | gp_json_val_free (gp_json_val *self) |
Frees a JSON value. | |
const char * | gp_json_type_name (enum gp_json_type type) |
Returns type name. | |
void | gp_json_err (gp_json_reader *self, const char *fmt,...) |
Fills the reader error. | |
void | gp_json_err_print (gp_json_reader *self) |
Prints error stored in the buffer. | |
void | gp_json_warn (gp_json_reader *self, const char *fmt,...) |
Prints a warning. | |
static int | gp_json_reader_err (gp_json_reader *self) |
Returns true if error was encountered. | |
static int | gp_json_valid (struct gp_json_val *res) |
Checks is result has valid type. | |
enum gp_json_type | gp_json_next_type (gp_json_reader *self) |
Returns the type of next element in buffer. | |
enum gp_json_type | gp_json_start (gp_json_reader *self) |
Returns if first element in JSON is object or array. | |
int | gp_json_obj_first (gp_json_reader *self, struct gp_json_val *res) |
Starts parsing of an JSON object. | |
size_t | gp_json_lookup (const void *arr, size_t memb_size, size_t list_len, const char *key) |
Utility function for log(n) lookup in a sorted array. | |
int | gp_json_obj_first_filter (gp_json_reader *self, struct gp_json_val *res, const struct gp_json_obj *obj, const struct gp_json_obj *ign) |
Object parsing functions with with attribute list. | |
int | gp_json_obj_skip (gp_json_reader *self) |
Skips parsing of an JSON object. | |
int | gp_json_arr_skip (gp_json_reader *self) |
Skips parsing of an JSON array. | |
static struct gp_json_state | gp_json_state_start (gp_json_reader *self) |
Returns a parser state at the start of current object/array. | |
static void | gp_json_state_load (gp_json_reader *self, struct gp_json_state state) |
Returns the parser to a saved state. | |
static void | gp_json_reset (gp_json_reader *self) |
Resets the parser to a start. | |
gp_json_reader * | gp_json_reader_load (const char *path) |
Loads a file into an gp_json_reader buffer. | |
void | gp_json_reader_free (gp_json_reader *self) |
Frees an gp_json_reader buffer. | |
void | gp_json_reader_finish (gp_json_reader *self) |
Prints errors and warnings at the end of parsing. | |
static int | gp_json_empty (gp_json_reader *self) |
Returns non-zero if whole buffer has been consumed. | |
A JSON parser.
Definition in file gp_json_reader.h.
int gp_json_arr_skip | ( | gp_json_reader * | self | ) |
Skips parsing of an JSON array.
self | A gp_json_reader |
|
inlinestatic |
Returns non-zero if whole buffer has been consumed.
self | A gp_json_reader. |
Definition at line 354 of file gp_json_reader.h.
References gp_json_reader::len, and gp_json_reader::off.
void gp_json_err | ( | gp_json_reader * | self, |
const char * | fmt, | ||
... | |||
) |
Fills the reader error.
Once buffer error is set all parsing functions return immediatelly with type set to GP_JSON_VOID.
self | An gp_json_reader |
fmt | A printf like format string |
... | A printf like parameters |
void gp_json_err_print | ( | gp_json_reader * | self | ) |
Prints error stored in the buffer.
The error takes into consideration the current offset in the buffer and prints a few preceding lines along with the exact position of the error.
The error is passed to the err_print() handler.
self | A gp_json_reader |
size_t gp_json_lookup | ( | const void * | arr, |
size_t | memb_size, | ||
size_t | list_len, | ||
const char * | key | ||
) |
Utility function for log(n) lookup in a sorted array.
list | Analphabetically sorted array. |
list_len | Array length. |
enum gp_json_type gp_json_next_type | ( | gp_json_reader * | self | ) |
Returns the type of next element in buffer.
self | A gp_json_reader |
int gp_json_obj_first | ( | gp_json_reader * | self, |
struct gp_json_val * | res | ||
) |
Starts parsing of an JSON object.
self | A gp_json_reader @res An gp_json result. |
int gp_json_obj_first_filter | ( | gp_json_reader * | self, |
struct gp_json_val * | res, | ||
const struct gp_json_obj * | obj, | ||
const struct gp_json_obj * | ign | ||
) |
Object parsing functions with with attribute list.
These functions allows you to efficiently filter a set of keys for a given object passed in gp_json_obj.
self | A gp_json_reader |
res | An gp_json result. |
obj | An gp_json_obj object description. |
f | A file to print warnings to. |
int gp_json_obj_skip | ( | gp_json_reader * | self | ) |
Skips parsing of an JSON object.
self | A gp_json_reader |
|
inlinestatic |
Returns true if error was encountered.
self | A gp_json_reader |
Definition at line 146 of file gp_json_reader.h.
void gp_json_reader_finish | ( | gp_json_reader * | self | ) |
Prints errors and warnings at the end of parsing.
Checks if self->err is set and prints the error with gp_json_reader_err()
Checks if there is any text left after the parser has finished and prints a warning if so.
self | A gp_json_reader |
void gp_json_reader_free | ( | gp_json_reader * | self | ) |
Frees an gp_json_reader buffer.
self | A gp_json_reader allocated by gp_json_load() function. |
gp_json_reader * gp_json_reader_load | ( | const char * | path | ) |
Loads a file into an gp_json_reader buffer.
The reader has to be later freed by gp_json_reader_free().
path | A path to a file. |
|
inlinestatic |
Resets the parser to a start.
self | A gp_json_reader |
Definition at line 311 of file gp_json_reader.h.
References gp_json_reader::depth, gp_json_reader::off, and gp_json_reader::sub_off.
enum gp_json_type gp_json_start | ( | gp_json_reader * | self | ) |
Returns if first element in JSON is object or array.
self | A gp_json_reader |
|
inlinestatic |
Returns the parser to a saved state.
This function could be used for the parser to return to the start of object or array saved by t the gp_json_state_get() function.
self | A gp_json_reader |
state | An parser state as returned by the gp_json_state_get(). |
Definition at line 299 of file gp_json_reader.h.
References gp_json_reader::depth, gp_json_reader::off, and gp_json_reader::sub_off.
|
inlinestatic |
Returns a parser state at the start of current object/array.
This function could be used for the parser to return to the start of the currently parsed object or array.
self | A gp_json_reader |
Definition at line 280 of file gp_json_reader.h.
const char * gp_json_type_name | ( | enum gp_json_type | type | ) |
Returns type name.
type | A json type. |
gp_json_val * gp_json_val_alloc | ( | size_t | buf_size | ) |
Allocates a JSON value.
buf_size | A maximal buffer size for a string value, pass 0 for default. |
void gp_json_val_free | ( | gp_json_val * | self | ) |
Frees a JSON value.
self | A JSON value previously allocated by gp_json_val_alloc(). |
|
inlinestatic |
Checks is result has valid type.
res | An gp_json value. |
Definition at line 157 of file gp_json_reader.h.
void gp_json_warn | ( | gp_json_reader * | self, |
const char * | fmt, | ||
... | |||
) |
Prints a warning.
Uses the print handler in the buffer to print a warning along with a few lines of context from the JSON at the current position.
self | A gp_json_reader |
fmt | A printf-like error string. |
... | A printf-like parameters. |