text
stringlengths 0
14.1k
|
---|
tree = get_dict_tree(dict); |
if (!tree) |
return err_false(ctx, ""Expect dict""); |
if (json_value_size(key) > JSON_MAX_KEY) |
return err_false(ctx, ""Too large key""); |
dict->u.v_size++; |
if (!cbtree_insert(tree, key)) |
return err_false(ctx, ""Key insertion failed""); |
return true; |
} |
/* create basic value struct, link to stuctures */ |
static struct JsonValue *mk_value(struct JsonContext *ctx, enum JsonValueType type, size_t extra, bool attach) |
{ |
struct JsonValue *val; |
struct JsonContainer *col = NULL; |
if (!ctx) |
return NULL; |
val = cx_alloc(ctx->pool, sizeof(struct JsonValue) + extra); |
if (!val) |
return err_null(ctx, ""No memory""); |
if ((uintptr_t)val & TYPE_MASK) |
return err_null(ctx, ""Unaligned pointer""); |
/* initial value */ |
val->v_next_and_type = type; |
val->u.v_int = 0; |
if (type == JSON_DICT || type == JSON_LIST) { |
col = get_container(val); |
col->c_ctx = ctx; |
col->c_parent = NULL; |
if (type == JSON_DICT) { |
col->u.c_dict = cbtree_create(get_key_data_cb, NULL, val, ctx->pool); |
if (!col->u.c_dict) |
return err_null(ctx, ""No memory""); |
} else { |
memset(&col->u.c_list, 0, sizeof(col->u.c_list)); |
} |
} |
/* independent JsonValue? */ |
if (!attach) { |
set_next(val, UNATTACHED); |
return val; |
} |
/* attach to parent */ |
if (col) |
col->c_parent = ctx->parent; |
/* attach to previous value */ |
if (has_type(ctx->parent, JSON_DICT)) { |
if (ctx->cur_key) { |
set_next(ctx->cur_key, val); |
ctx->cur_key = NULL; |
} else { |
ctx->cur_key = val; |
} |
} else if (has_type(ctx->parent, JSON_LIST)) { |
real_list_append(ctx->parent, val); |
} else if (!ctx->top) { |
ctx->top = val; |
} else { |
return err_null(ctx, ""Only one top element is allowed""); |
} |
return val; |
} |
static void prepare_array(struct JsonValue *list) |
{ |
struct JsonContainer *c; |
struct JsonValue *val; |
struct ValueList *vlist; |
size_t i; |
vlist = get_list_vlist(list); |
if (vlist->array) |
return; |
c = get_container(list); |
vlist->array = cx_alloc(c->c_ctx->pool, list->u.v_size * sizeof(struct JsonValue *)); |
if (!vlist->array) |
return; |
val = vlist->first; |
for (i = 0; i < list->u.v_size && val; i++) { |
vlist->array[i] = val; |
val = get_next(val); |
} |
} |
/* |
* Parsing code starts |
*/ |
Subsets and Splits