Skip to content
Snippets Groups Projects
Commit d503c81a authored by Suru Dissanaike's avatar Suru Dissanaike
Browse files

json-editor: WIP json_object_del_by_idx for json-c version 0.12.1

parent d25fc8c3
Branches
No related tags found
No related merge requests found
......@@ -292,13 +292,34 @@ out:
return 0;
}
struct json_object *json_object_e_del_by_idx(struct json_object *arr, int idx)
{
struct json_object *el, *new;
int i;
if (!json_object_is_type(arr, json_type_array))
return arr;
new = json_object_new_array();
for (i = 0; i < json_object_array_length(arr); i++) {
if (i == idx)
continue;
el = json_object_array_get_idx(arr, i);
json_object_array_add(new, el);
}
return new;
}
/**
* del a value key pair to src object
*
*/
static int del_val(struct json_object *src, char *key)
{
struct json_object *val;
struct json_object *val, *new, *new2;
int idx[32];
int len, rv = -1;
char parsed_key[32] = {0};
......@@ -310,6 +331,9 @@ static int del_val(struct json_object *src, char *key)
json_object_object_del(src, parsed_key);
rv = 0;
} else {
if (len > 1)
new = json_object_new_array();
/* iterate every index that was appended to the key */
for (int i = 0; i < len; i++) {
struct json_object *j_idx;
......@@ -328,14 +352,20 @@ static int del_val(struct json_object *src, char *key)
/* at final index, delete at specified idx */
else {
if (idx[i] > -1)
json_object_array_del_idx(val, idx[i], 1);
new2 = json_object_e_del_by_idx(val, idx[i]);
else
json_object_array_del_idx(val,
json_object_array_length(val) - 1, 1);
new2 = json_object_e_del_by_idx(val, idx[i]);
if (len > 1)
json_object_array_add(new, new2);
else
new = new2;
rv = 0;
}
}
json_object_object_del(src, parsed_key);
json_object_object_add(src, parsed_key, new);
}
out:
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment