libusual  0.1
Typedefs | Enumerations
usual/json.h File Reference

Read and write JSON. More...

Typedefs

typedef bool(* json_dict_iter_callback_f )(void *arg, struct JsonValue *key, struct JsonValue *val)
 Callback for dict iterator.
typedef bool(* json_list_iter_callback_f )(void *arg, struct JsonValue *elem)
 Callback for list iterator.

Enumerations

enum  JsonValueType {
  JSON_NULL, JSON_BOOL, JSON_INT, JSON_FLOAT,
  JSON_STRING, JSON_LIST, JSON_DICT
}
 JSON value types. More...
enum  JsonParseOptions { JSON_STRICT, JSON_PARSE_RELAXED, JSON_PARSE_IGNORE_ENCODING }
 Options for JSON parser. More...

Functions

Allocation context.
struct JsonContextjson_new_context (const void *cx_mem, size_t initial_mem)
 Create allocation context.
void json_free_context (struct JsonContext *ctx)
 Create allocation context.
const char * json_strerror (struct JsonContext *ctx)
 Create allocation context.
Parse JSON
struct JsonValuejson_parse (struct JsonContext *ctx, const char *src, size_t length)
 Parse JSON string.
void json_set_options (struct JsonContext *ctx, unsigned int options)
 Set parsing options.
Examine single value
enum JsonValueType json_value_type (struct JsonValue *jv)
 Return type for value.
size_t json_value_size (struct JsonValue *jv)
 Return element size.
static bool json_value_is_null (struct JsonValue *jv)
 Return true if value is null.
static bool json_value_is_bool (struct JsonValue *jv)
 Return true if value is boolean.
static bool json_value_is_int (struct JsonValue *jv)
 Return true if value is int.
static bool json_value_is_float (struct JsonValue *jv)
 Return true if value is float.
static bool json_value_is_string (struct JsonValue *jv)
 Return true if value is string.
static bool json_value_is_list (struct JsonValue *jv)
 Return true if value is list.
static bool json_value_is_dict (struct JsonValue *jv)
 Return true if value is dict.
bool json_value_as_bool (struct JsonValue *jv, bool *dst_p)
 Get bool value.
bool json_value_as_int (struct JsonValue *jv, int64_t *dst_p)
 Get int value.
bool json_value_as_float (struct JsonValue *jv, double *dst_p)
 Get double value.
bool json_value_as_string (struct JsonValue *jv, const char **dst_p, size_t *size_p)
 Get string value.
Get values from dict
bool json_dict_get_value (struct JsonValue *dict, const char *key, struct JsonValue **val_p)
 Get key value from dict.
bool json_dict_is_null (struct JsonValue *jv, const char *key)
 Return true if value is null or missing.
bool json_dict_get_bool (struct JsonValue *jv, const char *key, bool *dst_p)
 Get boolean value from dict.
bool json_dict_get_int (struct JsonValue *jv, const char *key, int64_t *dst_p)
 Get int value from dict.
bool json_dict_get_float (struct JsonValue *jv, const char *key, double *dst_p)
 Get float value from dict.
bool json_dict_get_string (struct JsonValue *jv, const char *key, const char **dst_p, size_t *len_p)
 Get string value from dict.
bool json_dict_get_dict (struct JsonValue *jv, const char *key, struct JsonValue **dst_p)
 Get sub-dict from dict.
bool json_dict_get_list (struct JsonValue *jv, const char *key, struct JsonValue **dst_p)
 Get list from dict.
bool json_dict_get_opt_bool (struct JsonValue *jv, const char *key, bool *dst_p)
 Get optional bool from dict.
bool json_dict_get_opt_int (struct JsonValue *jv, const char *key, int64_t *dst_p)
 Get optional int from dict.
bool json_dict_get_opt_float (struct JsonValue *jv, const char *key, double *dst_p)
 Get optional float from dict.
bool json_dict_get_opt_string (struct JsonValue *jv, const char *key, const char **dst_p, size_t *len_p)
 Get optional string from dict.
bool json_dict_get_opt_list (struct JsonValue *jv, const char *key, struct JsonValue **dst_p)
 Get optional list from dict.
bool json_dict_get_opt_dict (struct JsonValue *jv, const char *key, struct JsonValue **dst_p)
 Get optional dict from dict.
Get values from list.
bool json_list_get_value (struct JsonValue *list, size_t index, struct JsonValue **val_p)
 Get value from list.
bool json_list_is_null (struct JsonValue *list, size_t index)
 Return true if value is null or missing.
bool json_list_get_bool (struct JsonValue *list, size_t index, bool *val_p)
 Get bool from list.
bool json_list_get_int (struct JsonValue *list, size_t index, int64_t *val_p)
 Get int from list.
bool json_list_get_float (struct JsonValue *list, size_t index, double *val_p)
 Get float from list.
bool json_list_get_string (struct JsonValue *list, size_t index, const char **val_p, size_t *len_p)
 Get string from list.
bool json_list_get_list (struct JsonValue *list, size_t index, struct JsonValue **val_p)
 Get list value from list.
bool json_list_get_dict (struct JsonValue *list, size_t index, struct JsonValue **val_p)
 Get dict value from list.
Iterate over elements in list/dict.
bool json_dict_iter (struct JsonValue *dict, json_dict_iter_callback_f cb_func, void *cb_arg)
 Walk over dict elements.
bool json_list_iter (struct JsonValue *list, json_list_iter_callback_f cb_func, void *cb_arg)
 Walk over list elements.
Output JSON.
bool json_render (struct MBuf *dst, struct JsonValue *jv)
 Render JSON object as string.
Create new values.
struct JsonValuejson_new_null (struct JsonContext *ctx)
 Create NULL value.
struct JsonValuejson_new_bool (struct JsonContext *ctx, bool val)
 Create bool value.
struct JsonValuejson_new_int (struct JsonContext *ctx, int64_t val)
 Create int value.
struct JsonValuejson_new_float (struct JsonContext *ctx, double val)
 Create float value.
struct JsonValuejson_new_string (struct JsonContext *ctx, const char *val)
 Create string value.
struct JsonValuejson_new_dict (struct JsonContext *ctx)
 Create dict value.
struct JsonValuejson_new_list (struct JsonContext *ctx)
 Create list value.
Add values to containers.
bool json_list_append (struct JsonValue *list, struct JsonValue *elem)
 Add value to list.
bool json_list_append_null (struct JsonValue *list)
 Add null to list.
bool json_list_append_bool (struct JsonValue *list, bool val)
 Add bool to list.
bool json_list_append_int (struct JsonValue *list, int64_t val)
 Add int to list.
bool json_list_append_float (struct JsonValue *list, double val)
 Add float to list.
bool json_list_append_string (struct JsonValue *list, const char *val)
 Add string to list.
bool json_dict_put (struct JsonValue *dict, const char *key, struct JsonValue *val)
 Add element to dict.
bool json_dict_put_null (struct JsonValue *dict, const char *key)
 Add null to dict.
bool json_dict_put_bool (struct JsonValue *dict, const char *key, bool val)
 Add bool to dict.
bool json_dict_put_int (struct JsonValue *dict, const char *key, int64_t val)
 Add int to dict.
bool json_dict_put_float (struct JsonValue *dict, const char *key, double val)
 Add float to dict.
bool json_dict_put_string (struct JsonValue *dict, const char *key, const char *str)
 Add string to dict.

Detailed Description

Read and write JSON.

Features:

Optional features for JSON config files, off by default:


Typedef Documentation

typedef bool(* json_dict_iter_callback_f)(void *arg, struct JsonValue *key, struct JsonValue *val)

Callback for dict iterator.

typedef bool(* json_list_iter_callback_f)(void *arg, struct JsonValue *elem)

Callback for list iterator.


Enumeration Type Documentation

JSON value types.

Returned by json_value_type().

Enumerator:
JSON_NULL 

Null value.

JSON_BOOL 

Boolean value.

JSON_INT 

Integer value.

JSON_FLOAT 

Float value.

JSON_STRING 

String value.

JSON_LIST 

JSON list.

JSON_DICT 

JSON "object", which is key->value map.

Options for JSON parser.

Enumerator:
JSON_STRICT 

Default - do strict parsing.

No comments, no extra comma.

JSON_PARSE_RELAXED 

Allow comments, allow extra comma.

Useful for JSON in config files.

JSON_PARSE_IGNORE_ENCODING 

Do not validate UTF-8.

The default behavior is to validate UTF-8.


Function Documentation

struct JsonContext* json_new_context ( const void *  cx_mem,
size_t  initial_mem 
) [read]

Create allocation context.

void json_free_context ( struct JsonContext ctx)

Create allocation context.

const char* json_strerror ( struct JsonContext ctx)

Create allocation context.

struct JsonValue* json_parse ( struct JsonContext ctx,
const char *  src,
size_t  length 
) [read]

Parse JSON string.

void json_set_options ( struct JsonContext ctx,
unsigned int  options 
)

Set parsing options.

enum JsonValueType json_value_type ( struct JsonValue jv)
size_t json_value_size ( struct JsonValue jv)

Return element size.

For JSON strings, it's bytes in string, for list and dict it returns number of elements.

static bool json_value_is_null ( struct JsonValue jv) [inline, static]

Return true if value is null.

References JSON_NULL, and json_value_type().

static bool json_value_is_bool ( struct JsonValue jv) [inline, static]

Return true if value is boolean.

References JSON_BOOL, and json_value_type().

static bool json_value_is_int ( struct JsonValue jv) [inline, static]

Return true if value is int.

References JSON_INT, and json_value_type().

static bool json_value_is_float ( struct JsonValue jv) [inline, static]

Return true if value is float.

References JSON_FLOAT, and json_value_type().

static bool json_value_is_string ( struct JsonValue jv) [inline, static]

Return true if value is string.

References JSON_STRING, and json_value_type().

static bool json_value_is_list ( struct JsonValue jv) [inline, static]

Return true if value is list.

References JSON_LIST, and json_value_type().

static bool json_value_is_dict ( struct JsonValue jv) [inline, static]

Return true if value is dict.

References JSON_DICT, and json_value_type().

bool json_value_as_bool ( struct JsonValue jv,
bool *  dst_p 
)

Get bool value.

bool json_value_as_int ( struct JsonValue jv,
int64_t *  dst_p 
)

Get int value.

bool json_value_as_float ( struct JsonValue jv,
double *  dst_p 
)

Get double value.

bool json_value_as_string ( struct JsonValue jv,
const char **  dst_p,
size_t *  size_p 
)

Get string value.

bool json_dict_get_value ( struct JsonValue dict,
const char *  key,
struct JsonValue **  val_p 
)

Get key value from dict.

bool json_dict_is_null ( struct JsonValue jv,
const char *  key 
)

Return true if value is null or missing.

bool json_dict_get_bool ( struct JsonValue jv,
const char *  key,
bool *  dst_p 
)

Get boolean value from dict.

bool json_dict_get_int ( struct JsonValue jv,
const char *  key,
int64_t *  dst_p 
)

Get int value from dict.

bool json_dict_get_float ( struct JsonValue jv,
const char *  key,
double *  dst_p 
)

Get float value from dict.

bool json_dict_get_string ( struct JsonValue jv,
const char *  key,
const char **  dst_p,
size_t *  len_p 
)

Get string value from dict.

bool json_dict_get_dict ( struct JsonValue jv,
const char *  key,
struct JsonValue **  dst_p 
)

Get sub-dict from dict.

bool json_dict_get_list ( struct JsonValue jv,
const char *  key,
struct JsonValue **  dst_p 
)

Get list from dict.

bool json_dict_get_opt_bool ( struct JsonValue jv,
const char *  key,
bool *  dst_p 
)

Get optional bool from dict.

bool json_dict_get_opt_int ( struct JsonValue jv,
const char *  key,
int64_t *  dst_p 
)

Get optional int from dict.

bool json_dict_get_opt_float ( struct JsonValue jv,
const char *  key,
double *  dst_p 
)

Get optional float from dict.

bool json_dict_get_opt_string ( struct JsonValue jv,
const char *  key,
const char **  dst_p,
size_t *  len_p 
)

Get optional string from dict.

bool json_dict_get_opt_list ( struct JsonValue jv,
const char *  key,
struct JsonValue **  dst_p 
)

Get optional list from dict.

bool json_dict_get_opt_dict ( struct JsonValue jv,
const char *  key,
struct JsonValue **  dst_p 
)

Get optional dict from dict.

bool json_list_get_value ( struct JsonValue list,
size_t  index,
struct JsonValue **  val_p 
)

Get value from list.

bool json_list_is_null ( struct JsonValue list,
size_t  index 
)

Return true if value is null or missing.

bool json_list_get_bool ( struct JsonValue list,
size_t  index,
bool *  val_p 
)

Get bool from list.

bool json_list_get_int ( struct JsonValue list,
size_t  index,
int64_t *  val_p 
)

Get int from list.

bool json_list_get_float ( struct JsonValue list,
size_t  index,
double *  val_p 
)

Get float from list.

bool json_list_get_string ( struct JsonValue list,
size_t  index,
const char **  val_p,
size_t *  len_p 
)

Get string from list.

bool json_list_get_list ( struct JsonValue list,
size_t  index,
struct JsonValue **  val_p 
)

Get list value from list.

bool json_list_get_dict ( struct JsonValue list,
size_t  index,
struct JsonValue **  val_p 
)

Get dict value from list.

bool json_dict_iter ( struct JsonValue dict,
json_dict_iter_callback_f  cb_func,
void *  cb_arg 
)

Walk over dict elements.

bool json_list_iter ( struct JsonValue list,
json_list_iter_callback_f  cb_func,
void *  cb_arg 
)

Walk over list elements.

bool json_render ( struct MBuf dst,
struct JsonValue jv 
)

Render JSON object as string.

struct JsonValue* json_new_null ( struct JsonContext ctx) [read]

Create NULL value.

struct JsonValue* json_new_bool ( struct JsonContext ctx,
bool  val 
) [read]

Create bool value.

struct JsonValue* json_new_int ( struct JsonContext ctx,
int64_t  val 
) [read]

Create int value.

struct JsonValue* json_new_float ( struct JsonContext ctx,
double  val 
) [read]

Create float value.

struct JsonValue* json_new_string ( struct JsonContext ctx,
const char *  val 
) [read]

Create string value.

struct JsonValue* json_new_dict ( struct JsonContext ctx) [read]

Create dict value.

struct JsonValue* json_new_list ( struct JsonContext ctx) [read]

Create list value.

bool json_list_append ( struct JsonValue list,
struct JsonValue elem 
)

Add value to list.

bool json_list_append_null ( struct JsonValue list)

Add null to list.

bool json_list_append_bool ( struct JsonValue list,
bool  val 
)

Add bool to list.

bool json_list_append_int ( struct JsonValue list,
int64_t  val 
)

Add int to list.

bool json_list_append_float ( struct JsonValue list,
double  val 
)

Add float to list.

bool json_list_append_string ( struct JsonValue list,
const char *  val 
)

Add string to list.

bool json_dict_put ( struct JsonValue dict,
const char *  key,
struct JsonValue val 
)

Add element to dict.

bool json_dict_put_null ( struct JsonValue dict,
const char *  key 
)

Add null to dict.

bool json_dict_put_bool ( struct JsonValue dict,
const char *  key,
bool  val 
)

Add bool to dict.

bool json_dict_put_int ( struct JsonValue dict,
const char *  key,
int64_t  val 
)

Add int to dict.

bool json_dict_put_float ( struct JsonValue dict,
const char *  key,
double  val 
)

Add float to dict.

bool json_dict_put_string ( struct JsonValue dict,
const char *  key,
const char *  str 
)

Add string to dict.