ujson
Complete and simple JSON reader and writer written in C
|
A recursive descend JSON parser. More...
Go to the source code of this file.
Data Structures | |
struct | ujson_reader |
A JSON parser internal state. More... | |
struct | ujson_val |
A parsed JSON key value pair. More... | |
struct | ujson_obj_attr |
A JSON object attribute description i.e. key and type. More... | |
struct | ujson_obj |
A JSON object description. More... | |
struct | ujson_reader_state |
A JSON reader state. More... | |
Macros | |
#define | UJSON_READER_INIT(buf, buf_len, rflags) |
An ujson_reader initializer with default values. More... | |
#define | UJSON_VAL_INIT(sbuf, sbuf_size) |
An ujson_val initializer. More... | |
#define | UJSON_OBJ_FOREACH(self, res) for (ujson_obj_first(self, res); ujson_val_valid(res); ujson_obj_next(self, res)) |
A loop over a JSON object. More... | |
#define | UJSON_OBJ_ATTR(keyv, typev) {.key = keyv, .type = typev} |
An ujson_obj_attr initializer. | |
#define | UJSON_OBJ_ATTR_IDX(key_idx, keyv, typev) [key_idx] = {.key = keyv, .type = typev} |
An ujson_obj_attr intializer with an array index. | |
#define | UJSON_OBJ_FOREACH_FILTER(self, res, obj, ign) |
A loop over a JSON object with a pre-defined list of expected attributes. More... | |
#define | UJSON_ARR_FOREACH(self, res) for (ujson_arr_first(self, res); ujson_val_valid(res); ujson_arr_next(self, res)) |
A loop over a JSON array. More... | |
Typedefs | |
typedef struct ujson_obj_attr | ujson_obj_attr |
A JSON object attribute description i.e. key and type. | |
typedef struct ujson_obj | ujson_obj |
A JSON object description. | |
typedef struct ujson_reader_state | ujson_reader_state |
A JSON reader state. | |
Enumerations | |
enum | ujson_reader_flags { UJSON_READER_STRICT = 0x01 } |
Reader flags. More... | |
Functions | |
ujson_val * | ujson_val_alloc (size_t buf_size) |
Allocates a JSON value. More... | |
void | ujson_val_free (ujson_val *self) |
Frees a JSON value. More... | |
static int | ujson_val_valid (struct ujson_val *res) |
Checks is result has valid type. More... | |
void | ujson_err (ujson_reader *self, const char *fmt,...) __attribute__((format(printf |
Fills the reader error. More... | |
void void | ujson_err_print (ujson_reader *self) |
Prints error stored in the buffer. More... | |
void | ujson_warn (ujson_reader *self, const char *fmt,...) __attribute__((format(printf |
Prints a warning. More... | |
void static int | ujson_reader_err (ujson_reader *self) |
Returns true if error was encountered. More... | |
enum ujson_type | ujson_next_type (ujson_reader *self) |
Returns the type of next element in buffer. More... | |
enum ujson_type | ujson_reader_start (ujson_reader *self) |
Returns if first element in JSON is object or array. More... | |
int | ujson_obj_first (ujson_reader *self, struct ujson_val *res) |
Starts parsing of a JSON object. More... | |
int | ujson_obj_next (ujson_reader *self, struct ujson_val *res) |
Parses next value from a JSON object. More... | |
size_t | ujson_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. More... | |
int | ujson_obj_first_filter (ujson_reader *self, struct ujson_val *res, const struct ujson_obj *obj, const struct ujson_obj *ign) |
Starts parsing of a JSON object with attribute lists. More... | |
int | ujson_obj_next_filter (ujson_reader *self, struct ujson_val *res, const struct ujson_obj *obj, const struct ujson_obj *ign) |
Parses next value from a JSON object with attribute lists. More... | |
int | ujson_obj_skip (ujson_reader *self) |
Skips parsing of a JSON object. More... | |
int | ujson_arr_first (ujson_reader *self, struct ujson_val *res) |
Starts parsing of a JSON array. More... | |
int | ujson_arr_next (ujson_reader *self, struct ujson_val *res) |
Parses next value from a JSON array. More... | |
int | ujson_arr_skip (ujson_reader *self) |
Skips parsing of a JSON array. More... | |
static ujson_reader_state | ujson_reader_state_save (ujson_reader *self) |
Returns a parser state at the start of current object/array. More... | |
static void | ujson_reader_state_load (ujson_reader *self, ujson_reader_state state) |
Returns the parser to a saved state. More... | |
static void | ujson_reader_reset (ujson_reader *self) |
Resets the parser to a start. More... | |
ujson_reader * | ujson_reader_load (const char *path) |
Loads a file into an ujson_reader buffer. More... | |
void | ujson_reader_free (ujson_reader *self) |
Frees an ujson_reader buffer. More... | |
void | ujson_reader_finish (ujson_reader *self) |
Prints errors and warnings at the end of parsing. More... | |
static int | ujson_reader_consumed (ujson_reader *self) |
Returns non-zero if whole buffer has been consumed. More... | |
Variables | |
const struct ujson_obj * | ujson_empty_obj |
An empty object attribute list. More... | |
A recursive descend JSON parser.
All the function that parse JSON return zero on success and non-zero on a failure. Once an error has happened all subsequent attempts to parse more return with non-zero exit status immediatelly. This is designed so that we can parse several values without checking each return value and only check if error has happened at the end of the sequence.
Definition in file ujson_reader.h.
#define UJSON_ARR_FOREACH | ( | self, | |
res | |||
) | for (ujson_arr_first(self, res); ujson_val_valid(res); ujson_arr_next(self, res)) |
A loop over a JSON array.
self | An ujson_reader. |
res | An ujson_val to store the next parsed value to. |
Definition at line 431 of file ujson_reader.h.
#define UJSON_OBJ_FOREACH | ( | self, | |
res | |||
) | for (ujson_obj_first(self, res); ujson_val_valid(res); ujson_obj_next(self, res)) |
A loop over a JSON object.
self | An ujson_reader. |
res | An ujson_val to store the next parsed value to. |
Definition at line 262 of file ujson_reader.h.
#define UJSON_OBJ_FOREACH_FILTER | ( | self, | |
res, | |||
obj, | |||
ign | |||
) |
A loop over a JSON object with a pre-defined list of expected attributes.
self | An ujson_reader. |
res | An ujson_val to store the next parsed value to. |
obj | An ujson_obj with a description of attributes to parse. |
ign | An ujson_obj with a description of attributes to ignore. |
Definition at line 381 of file ujson_reader.h.
#define UJSON_READER_INIT | ( | buf, | |
buf_len, | |||
rflags | |||
) |
An ujson_reader initializer with default values.
buf | A pointer to a buffer with JSON data. |
buf_len | A JSON data buffer lenght. |
rflags | enum ujson_reader_flags. |
Definition at line 32 of file ujson_reader.h.
#define UJSON_VAL_INIT | ( | sbuf, | |
sbuf_size | |||
) |
An ujson_val initializer.
sbuf | A pointer to a buffer used for string values. |
sbuf_size | A length of the buffer used for string values. |
Definition at line 83 of file ujson_reader.h.
enum ujson_reader_flags |
Reader flags.
Enumerator | |
---|---|
UJSON_READER_STRICT | If set warnings are treated as errors. |
Definition at line 42 of file ujson_reader.h.
int ujson_arr_first | ( | ujson_reader * | self, |
struct ujson_val * | res | ||
) |
Starts parsing of a JSON array.
self | An ujson_reader. |
res | An ujson_val to store the parsed value to. |
int ujson_arr_next | ( | ujson_reader * | self, |
struct ujson_val * | res | ||
) |
Parses next value from a JSON array.
If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped before next call to this function.
self | An ujson_reader. |
res | An ujson_val to store the parsed value to. |
int ujson_arr_skip | ( | ujson_reader * | self | ) |
Skips parsing of a JSON array.
self | A ujson_reader. |
void ujson_err | ( | ujson_reader * | self, |
const char * | fmt, | ||
... | |||
) |
Fills the reader error.
Once buffer error is set all parsing functions return immediatelly with type set to UJSON_VOID.
self | An ujson_reader |
fmt | A printf like format string |
... | A printf like parameters |
void void ujson_err_print | ( | ujson_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 ujson_reader |
size_t ujson_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 ujson_type ujson_next_type | ( | ujson_reader * | self | ) |
Returns the type of next element in buffer.
self | An ujson_reader |
int ujson_obj_first | ( | ujson_reader * | self, |
struct ujson_val * | res | ||
) |
Starts parsing of a JSON object.
self | An ujson_reader |
res | An ujson_val to store the parsed value to. |
int ujson_obj_first_filter | ( | ujson_reader * | self, |
struct ujson_val * | res, | ||
const struct ujson_obj * | obj, | ||
const struct ujson_obj * | ign | ||
) |
Starts parsing of a JSON object with attribute lists.
self | An ujson_reader. |
res | An ujson_val to store the parsed value to. |
obj | An ujson_obj object description. |
ign | A list of keys to ignore. |
int ujson_obj_next | ( | ujson_reader * | self, |
struct ujson_val * | res | ||
) |
Parses next value from a JSON object.
If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped before next call to this function.
self | An ujson_reader. |
res | A ujson_val to store the parsed value to. |
int ujson_obj_next_filter | ( | ujson_reader * | self, |
struct ujson_val * | res, | ||
const struct ujson_obj * | obj, | ||
const struct ujson_obj * | ign | ||
) |
Parses next value from a JSON object with attribute lists.
If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped before next call to this function.
self | An ujson_reader. |
res | An ujson_val to store the parsed value to. |
obj | An ujson_obj object description. |
ign | A list of keys to ignore. If set to NULL all unknown keys are ignored, if set to ujson_empty_obj all unknown keys produce warnings. |
int ujson_obj_skip | ( | ujson_reader * | self | ) |
Skips parsing of a JSON object.
self | An ujson_reader. |
|
inlinestatic |
Returns non-zero if whole buffer has been consumed.
self | A ujson_reader. |
Definition at line 538 of file ujson_reader.h.
|
inlinestatic |
Returns true if error was encountered.
self | A ujson_reader |
Definition at line 205 of file ujson_reader.h.
Referenced by ujson_reader_state_load().
void ujson_reader_finish | ( | ujson_reader * | self | ) |
Prints errors and warnings at the end of parsing.
Checks if self->err is set and prints the error with ujson_reader_err()
Checks if there is any text left after the parser has finished with ujson_reader_consumed() and prints a warning if there were any non-whitespace characters left.
self | A ujson_reader |
void ujson_reader_free | ( | ujson_reader * | self | ) |
Frees an ujson_reader buffer.
self | A ujson_reader allocated by ujson_reader_load() function. |
ujson_reader* ujson_reader_load | ( | const char * | path | ) |
Loads a file into an ujson_reader buffer.
The reader has to be later freed by ujson_reader_free().
path | A path to a file. |
|
inlinestatic |
Resets the parser to a start.
self | A ujson_reader |
Definition at line 494 of file ujson_reader.h.
enum ujson_type ujson_reader_start | ( | ujson_reader * | self | ) |
Returns if first element in JSON is object or array.
self | A ujson_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 ujson_reader_state_get() function.
self | A ujson_reader |
state | An parser state as returned by the ujson_reader_state_get(). |
Definition at line 479 of file ujson_reader.h.
References ujson_reader_err().
|
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 ujson_reader |
Definition at line 460 of file ujson_reader.h.
ujson_val* ujson_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 ujson_val_free | ( | ujson_val * | self | ) |
Frees a JSON value.
self | A JSON value previously allocated by ujson_val_alloc(). |
|
inlinestatic |
Checks is result has valid type.
res | An ujson value. |
Definition at line 156 of file ujson_reader.h.
References ujson_val::type.
void ujson_warn | ( | ujson_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 ujson_reader |
fmt | A printf-like error string. |
... | A printf-like parameters. |
|
extern |
An empty object attribute list.
To be passed to UJSON_OBJ_FOREACH_FITLER() as ignore list.