11#ifndef GP_JSON_READER_H
12#define GP_JSON_READER_H
18#define GP_JSON_READER_INIT(buf, buf_len) { \
19 .max_depth = GP_JSON_RECURSION_MAX, \
20 .err_print = GP_JSON_ERR_PRINT, \
21 .err_print_priv = GP_JSON_ERR_PRINT_PRIV, \
44 void (*
err_print)(
void *err_print_priv,
const char *line);
47 char err[GP_JSON_ERR_MAX];
74 char id[GP_JSON_ID_MAX];
148 return !!self->err[0];
187#define GP_JSON_OBJ_FOREACH(buf, res) \
188 for (gp_json_obj_first(buf, res); gp_json_valid(res); gp_json_obj_next(buf, res))
214static inline size_t gp_json_obj_lookup(
const gp_json_obj *obj,
const char *key)
219#define GP_JSON_OBJ_ATTR(keyv, typev) \
220 {.key = keyv, .type = typev}
238#define GP_JSON_OBJ_FILTER(buf, res, obj, ign) \
239 for (gp_json_obj_first_filter(buf, res, obj, ign); gp_json_valid(res); gp_json_obj_next_filter(buf, res, obj, ign))
252#define GP_JSON_ARR_FOREACH(buf, res) \
253 for (gp_json_arr_first(buf, res); gp_json_valid(res); gp_json_arr_next(buf, res))
283 .off = self->sub_off,
284 .depth = self->depth,
301 self->
off = state.off;
303 self->
depth = state.depth;
356 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.
gp_json_type
A JSON data type.
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_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 void gp_json_reset(gp_json_reader *self)
Resets the parser to a start.
static int gp_json_empty(gp_json_reader *self)
Returns non-zero if whole buffer has been consumed.
gp_json_val * gp_json_val_alloc(size_t buf_size)
Allocates a JSON value.
static void gp_json_state_load(gp_json_reader *self, struct gp_json_state state)
Returns the parser to a saved state.
const char * gp_json_type_name(enum gp_json_type type)
Returns type name.
int gp_json_arr_skip(gp_json_reader *self)
Skips parsing of an JSON array.
static int gp_json_valid(struct gp_json_val *res)
Checks is result has valid type.
int gp_json_obj_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of an JSON object.
void gp_json_warn(gp_json_reader *self, const char *fmt,...)
Prints a warning.
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.
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 an JSON object.
static int gp_json_reader_err(gp_json_reader *self)
Returns true if error was encountered.
static struct gp_json_state gp_json_state_start(gp_json_reader *self)
Returns a parser state at the start of current object/array.
A JSON object attribute description i.e. key and type.
A JSON object description.
const gp_json_obj_attr * attrs
A JSON parser internal state.
void(* err_print)(void *err_print_priv, const char *line)
A parsed JSON key value pair.