void json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
{
- lh_table_delete(jso->o.c_object, key);
- lh_table_insert(jso->o.c_object, strdup(key), val);
+ // We lookup the entry and replace the value, rather than just deleting
+ // and re-adding it, so the existing key remains valid.
+ json_object *existing_value = NULL;
+ struct lh_entry *existing_entry;
+ existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
+ if (!existing_entry)
+ {
+ lh_table_insert(jso->o.c_object, strdup(key), val);
+ return;
+ }
+ existing_value = (void *)existing_entry->v;
+ if (existing_value)
+ json_object_put(existing_value);
+ existing_entry->v = val;
}
struct json_object* json_object_object_get(struct json_object* jso, const char *key)
check_PROGRAMS = test1 test1Formatted
check_PROGRAMS += test2 test2Formatted
check_PROGRAMS += test4
+check_PROGRAMS += testReplaceExisting
check_PROGRAMS += test_parse_int64
check_PROGRAMS += test_null
check_PROGRAMS += test_cast
test4_LDADD = $(LIBJSON_LA)
+testReplaceExisting_LDADD = $(LIBJSON_LA)
+
test_parse_int64_LDADD = $(LIBJSON_LA)
test_null_LDADD = $(LIBJSON_LA)
test_parse_LDADD = $(LIBJSON_LA)
-TESTS = test1.test test2.test test4.test parse_int64.test test_null.test test_cast.test test_parse.test
+TESTS = test1.test test2.test test4.test testReplaceExisting.test parse_int64.test test_null.test test_cast.test test_parse.test
TESTS+= test_printbuf.test
check_PROGRAMS+=test_printbuf
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "json.h"
+
+int main(int argc, char **argv)
+{
+ MC_SET_DEBUG(1);
+
+ /*
+ * Check that replacing an existing object keeps the key valid,
+ * and that it keeps the order the same.
+ */
+ json_object *my_object = json_object_new_object();
+ json_object_object_add(my_object, "foo1", json_object_new_string("bar1"));
+ json_object_object_add(my_object, "foo2", json_object_new_string("bar2"));
+ json_object_object_add(my_object, "foo3", json_object_new_string("bar3"));
+ const char *original_key = NULL;
+ int orig_count = 0;
+ json_object_object_foreach(my_object, key, val)
+ {
+ printf("Key at index %d is [%s]\n", orig_count, key);
+ orig_count++;
+ if (strcmp(key, "foo2") != 0)
+ continue;
+ printf("replacing value for key [%s]\n", key);
+ original_key = key;
+ json_object_object_add(my_object, key, json_object_new_string("zzz"));
+ }
+
+ printf("==== second loop starting ====\n");
+
+ int new_count = 0;
+ int retval = 0;
+ json_object_object_foreach(my_object, key2, val2)
+ {
+ printf("Key at index %d is [%s]\n", new_count, key2);
+ new_count++;
+ if (strcmp(key2, "foo2") != 0)
+ continue;
+ printf("pointer for key [%s] does %smatch\n", key2,
+ (key2 == original_key) ? "" : "NOT ");
+ if (key2 != original_key)
+ retval = 1;
+ }
+ if (new_count != orig_count)
+ {
+ printf("mismatch between original count (%d) and new count (%d)\n",
+ orig_count, new_count);
+ retval = 1;
+ }
+
+ return 0;
+}
--- /dev/null
+Key at index 0 is [foo1]
+Key at index 1 is [foo2]
+replacing value for key [foo2]
+Key at index 2 is [foo3]
+==== second loop starting ====
+Key at index 0 is [foo1]
+Key at index 1 is [foo2]
+pointer for key [foo2] does match
+Key at index 2 is [foo3]
--- /dev/null
+#!/bin/sh
+
+# Common definitions
+if test -z "$srcdir"; then
+ srcdir="${0%/*}"
+ test "$srcdir" = "$0" && srcdir=.
+ test -z "$srcdir" && srcdir=.
+fi
+. "$srcdir/test-defs.sh"
+
+run_output_test testReplaceExisting
+exit $?