17#ifndef GP_JSON_READER_H
18#define GP_JSON_READER_H
32#define GP_JSON_READER_INIT(buf, buf_len) { \
33 .max_depth = GP_JSON_RECURSION_MAX, \
34 .err_print = GP_JSON_ERR_PRINT, \
35 .err_print_priv = GP_JSON_ERR_PRINT_PRIV, \
58 void (*
err_print)(
void *err_print_priv,
const char *line);
184 return !!self->err[0];
239#define GP_JSON_OBJ_FOREACH(self, res) \
240 for (gp_json_obj_first(self, res); gp_json_val_valid(res); gp_json_obj_next(self, res))
280static inline size_t gp_json_obj_lookup(
const gp_json_obj *obj,
const char *key)
286#define GP_JSON_OBJ_ATTR(keyv, typev) \
287 {.key = keyv, .type = typev}
290#define GP_JSON_OBJ_ATTR_IDX(key_idx, keyv, typev) \
291 [key_idx] = {.key = keyv, .type = typev}
348#define GP_JSON_OBJ_FOREACH_FILTER(self, res, obj, ign) \
349 for (gp_json_obj_first_filter(self, res, obj, ign); \
350 gp_json_val_valid(res); \
351 gp_json_obj_next_filter(self, res, obj, ign))
398#define GP_JSON_ARR_FOREACH(self, res) \
399 for (gp_json_arr_first(self, res); gp_json_val_valid(res); gp_json_arr_next(self, res))
431 .depth = self->
depth,
448 self->
off = state.off;
450 self->
depth = state.depth;
504 return self->
off >= self->
len;
A compiler dependent macros.
#define GP_FMT_PRINTF(fmt, list)
Expands to format printf attribute when supported by the compiler.
Common JSON reader/writer definitions.
#define GP_JSON_ID_MAX
Maximal id string lenght including terminating null element.
gp_json_type
A JSON data type.
#define GP_JSON_ERR_MAX
Maximal error message length.
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.
void gp_json_err_print(gp_json_reader *self)
Prints error stored in the buffer.
enum gp_json_type gp_json_reader_start(gp_json_reader *self)
Returns if first element in JSON is object or array.
void gp_json_reader_finish(gp_json_reader *self)
Prints errors and warnings at the end of parsing.
void gp_json_err(gp_json_reader *self, const char *fmt,...)
Fills the reader error.
static gp_json_reader_state gp_json_reader_state_save(gp_json_reader *self)
Returns a parser state at the start of current object/array.
int gp_json_arr_next(gp_json_reader *self, struct gp_json_val *res)
Parses next value from a JSON array.
gp_json_val * gp_json_val_alloc(size_t buf_size)
Allocates a JSON value.
static int gp_json_reader_consumed(gp_json_reader *self)
Returns non-zero if whole buffer has been consumed.
int gp_json_arr_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of a JSON array.
static void gp_json_reader_reset(gp_json_reader *self)
Resets the parser to a start.
int gp_json_arr_skip(gp_json_reader *self)
Skips parsing of a JSON array.
static void gp_json_reader_state_load(gp_json_reader *self, gp_json_reader_state state)
Returns the parser to a saved state.
int gp_json_obj_next(gp_json_reader *self, struct gp_json_val *res)
Parses next value from a JSON object.
int gp_json_obj_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of a JSON object.
void gp_json_warn(gp_json_reader *self, const char *fmt,...)
Prints a warning.
int gp_json_obj_next_filter(gp_json_reader *self, struct gp_json_val *res, const struct gp_json_obj *obj, const struct gp_json_obj *ign)
Parses next value from a JSON object with attribute lists.
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)
Starts parsing of a JSON object with attribute lists.
void gp_json_reader_free(gp_json_reader *self)
Frees an gp_json_reader buffer.
enum gp_json_type gp_json_next_type(gp_json_reader *self)
Returns the type of next element in buffer.
gp_json_reader * gp_json_reader_load(const char *path)
Loads a file into an gp_json_reader buffer.
void gp_json_val_free(gp_json_val *self)
Frees a JSON value.
int gp_json_obj_skip(gp_json_reader *self)
Skips parsing of a JSON object.
static int gp_json_reader_err(gp_json_reader *self)
Returns true if error was encountered.
static int gp_json_val_valid(struct gp_json_val *res)
Checks is result has valid type.
A JSON object attribute description i.e. key and type.
const char * key
A JSON object key name.
enum gp_json_type type
A JSON object value type.
A JSON object description.
const gp_json_obj_attr * attrs
A list of object attributes.
size_t attr_cnt
A size of attrs array.
A JSON parser internal state.
void(* err_print)(void *err_print_priv, const char *line)
A parsed JSON key value pair.
int val_bool
A boolean value.
long long val_int
An integer value.
enum gp_json_type type
A value type.
double val_float
A floating point value.
const char * val_str
A string value.