Add a function to disconnect a JSON object from the parse tree

This commit is contained in:
Eric Fischer 2015-10-23 14:58:50 -07:00
parent dfcdd03b19
commit c6dfae26cb
2 changed files with 12 additions and 1 deletions

View File

@ -568,11 +568,19 @@ void json_free(json_object *o) {
free(o->string); free(o->string);
} }
json_disconnect(o);
free(o);
}
void json_disconnect(json_object *o) {
// Expunge references to this as an array element // Expunge references to this as an array element
// or a hash key or value. // or a hash key or value.
if (o->parent != NULL) { if (o->parent != NULL) {
if (o->parent->type == JSON_ARRAY) { if (o->parent->type == JSON_ARRAY) {
int i;
for (i = 0; i < o->parent->length; i++) { for (i = 0; i < o->parent->length; i++) {
if (o->parent->array[i] == o) { if (o->parent->array[i] == o) {
break; break;
@ -586,6 +594,8 @@ void json_free(json_object *o) {
} }
if (o->parent->type == JSON_HASH) { if (o->parent->type == JSON_HASH) {
int i;
for (i = 0; i < o->parent->length; i++) { for (i = 0; i < o->parent->length; i++) {
if (o->parent->keys[i] == o) { if (o->parent->keys[i] == o) {
o->parent->keys[i] = fabricate_object(o->parent, JSON_NULL); o->parent->keys[i] = fabricate_object(o->parent, JSON_NULL);
@ -612,5 +622,5 @@ void json_free(json_object *o) {
} }
} }
free(o); o->parent = NULL;
} }

View File

@ -59,5 +59,6 @@ json_object *json_read_tree(json_pull *j);
json_object *json_read(json_pull *j); json_object *json_read(json_pull *j);
json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state); json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state);
void json_free(json_object *j); void json_free(json_object *j);
void json_disconnect(json_object *j);
json_object *json_hash_get(json_object *o, const char *s); json_object *json_hash_get(json_object *o, const char *s);