text
stringlengths
0
14.1k
bool json_value_as_string(struct JsonValue *jv, const char **dst_p, size_t *size_p)
{
if (!has_type(jv, JSON_STRING))
return false;
*dst_p = get_cstring(jv);
if (size_p)
*size_p = jv->u.v_size;
return true;
}
/*
* Load value from dict.
*/
static int dict_getter(struct JsonValue *dict,
const char *key, unsigned int klen,
struct JsonValue **val_p,
enum JsonValueType req_type, bool req_value)
{
struct JsonValue *val, *kjv;
struct CBTree *tree;
tree = get_dict_tree(dict);
if (!tree)
return false;
kjv = cbtree_lookup(tree, key, klen);
if (!kjv) {
if (req_value)
return false;
*val_p = NULL;
return true;
}
val = get_next(kjv);
if (!req_value && json_value_is_null(val)) {
*val_p = NULL;
return true;
}
if (!has_type(val, req_type))
return false;
*val_p = val;
return true;
}
bool json_dict_get_value(struct JsonValue *dict, const char *key, struct JsonValue **val_p)
{
struct CBTree *tree;
struct JsonValue *kjv;
size_t klen;
tree = get_dict_tree(dict);
if (!tree)
return false;
klen = strlen(key);
kjv = cbtree_lookup(tree, key, klen);
if (!kjv)
return false;
*val_p = get_next(kjv);
return true;
}
bool json_dict_is_null(struct JsonValue *dict, const char *key)
{
struct JsonValue *val;
if (!json_dict_get_value(dict, key, &val))
return true;
return has_type(val, JSON_NULL);
}
bool json_dict_get_bool(struct JsonValue *dict, const char *key, bool *dst_p)
{
struct JsonValue *val;
if (!dict_getter(dict, key, strlen(key), &val, JSON_BOOL, true))
return false;
return json_value_as_bool(val, dst_p);
}
bool json_dict_get_int(struct JsonValue *dict, const char *key, int64_t *dst_p)
{
struct JsonValue *val;
if (!dict_getter(dict, key, strlen(key), &val, JSON_INT, true))
return false;
return json_value_as_int(val, dst_p);
}
bool json_dict_get_float(struct JsonValue *dict, const char *key, double *dst_p)
{
struct JsonValue *val;
if (!dict_getter(dict, key, strlen(key), &val, JSON_FLOAT, true))
return false;
return json_value_as_float(val, dst_p);
}
bool json_dict_get_string(struct JsonValue *dict, const char *key, const char **dst_p, size_t *len_p)
{
struct JsonValue *val;