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

This commit is contained in:
Eric Fischer
2015-12-18 12:05:22 -08:00
parent dfcdd03b19
commit c6dfae26cb
2 changed files with 12 additions and 1 deletions
+11 -1
View File
@@ -568,11 +568,19 @@ void json_free(json_object *o) {
free(o->string);
}
json_disconnect(o);
free(o);
}
void json_disconnect(json_object *o) {
// Expunge references to this as an array element
// or a hash key or value.
if (o->parent != NULL) {
if (o->parent->type == JSON_ARRAY) {
int i;
for (i = 0; i < o->parent->length; i++) {
if (o->parent->array[i] == o) {
break;
@@ -586,6 +594,8 @@ void json_free(json_object *o) {
}
if (o->parent->type == JSON_HASH) {
int i;
for (i = 0; i < o->parent->length; i++) {
if (o->parent->keys[i] == o) {
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;
}