Skip to content

Commit

Permalink
Add removing value functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Nov 29, 2022
1 parent a879ed0 commit 376cead
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,24 @@ const json_value_t *json_object_append(json_object_t *obj,
return &memb->value;
}

json_value_t *json_object_remove(const json_value_t *val,
json_object_t *obj)
{
json_member_t *memb = list_entry(val, json_member_t, value);

val = (json_value_t *)malloc(sizeof (json_value_t));
if (!val)
return NULL;

list_del(&memb->list);
rb_erase(&memb->rb, &obj->root);
obj->size--;

__move_json_value(&memb->value, (json_value_t *)val);
free(memb);
return (json_value_t *)val;
}

int json_array_size(const json_array_t *arr)
{
return arr->size;
Expand Down Expand Up @@ -896,3 +914,20 @@ const json_value_t *json_array_append(json_array_t *arr,
return &elem->value;
}

json_value_t *json_array_remove(const json_value_t *val,
json_array_t *arr)
{
json_element_t *elem = list_entry(val, json_element_t, value);

val = (json_value_t *)malloc(sizeof (json_value_t));
if (!val)
return NULL;

list_del(&elem->list);
arr->size--;

__move_json_value(&elem->value, (json_value_t *)val);
free(elem);
return (json_value_t *)val;
}

4 changes: 4 additions & 0 deletions json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ const json_value_t *json_object_next_value(const json_value_t *prev,
const json_value_t *json_object_append(json_object_t *obj,
const char *name,
int type, ...);
json_value_t *json_object_remove(const json_value_t *val,
json_object_t *obj);

int json_array_size(const json_array_t *arr);
const json_value_t *json_array_next_value(const json_value_t *val,
const json_array_t *arr);
const json_value_t *json_array_append(json_array_t *arry,
int type, ...);
json_value_t *json_array_remove(const json_value_t *val,
json_array_t *arr);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 376cead

Please sign in to comment.