From c6dfae26cb4bce87d37d0547918e8d97c9bd915b Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 23 Oct 2015 14:58:50 -0700 Subject: [PATCH] Add a function to disconnect a JSON object from the parse tree --- jsonpull.c | 12 +++++++++++- jsonpull.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/jsonpull.c b/jsonpull.c index fc85b92d..212ca2c9 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -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; } diff --git a/jsonpull.h b/jsonpull.h index f5ccbeeb..645f47c2 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -59,5 +59,6 @@ json_object *json_read_tree(json_pull *j); json_object *json_read(json_pull *j); json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state); void json_free(json_object *j); +void json_disconnect(json_object *j); json_object *json_hash_get(json_object *o, const char *s);