aboutsummaryrefslogtreecommitdiff
path: root/test/suites
diff options
context:
space:
mode:
authorHenrique Cabral <hcabral@veniam.com>2019-01-11 12:50:37 +0000
committerallen <leloucharcher@163.com>2019-10-11 11:20:25 +0800
commitcb4727c4a931a471f7d2f1594b162a71349b4dfe (patch)
tree8c0edea0c052a6d06bf5c3fb8db07d1e53512351 /test/suites
parent672b6df4743dbf76e059c640ad3fe45f7c795594 (diff)
downloadjansson-cb4727c4a931a471f7d2f1594b162a71349b4dfe.zip
jansson-cb4727c4a931a471f7d2f1594b162a71349b4dfe.tar.gz
jansson-cb4727c4a931a471f7d2f1594b162a71349b4dfe.tar.bz2
Add json_object_update_recursive()
Support merging values nested within objects. For instance, merging: { "foo": 1, "bar": { "baz": 2 } } with { "bar": { "baz": 3 } } results in { "foo": 1, "bar": { "baz": 3 } } instead of overwriting the value for the bar key.
Diffstat (limited to 'test/suites')
-rw-r--r--test/suites/api/test_object.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/suites/api/test_object.c b/test/suites/api/test_object.c
index 521ca81..ac10936 100644
--- a/test/suites/api/test_object.c
+++ b/test/suites/api/test_object.c
@@ -207,6 +207,57 @@ static void test_conditional_updates()
json_decref(other);
}
+static void test_recursive_updates()
+{
+ json_t *invalid, *object, *other;
+
+ invalid = json_integer(42);
+
+ object = json_pack("{sis{si}}", "foo", 1, "bar", "baz", 2);
+ other = json_pack("{sisisi}", "foo", 3, "bar", 4, "baz", 5);
+
+ if(!json_object_update_recursive(invalid, other))
+ fail("json_object_update_recursive accepted non-object argument");
+
+ json_decref(invalid);
+
+ if(json_object_update_recursive(object, other))
+ fail("json_object_update_recursive failed");
+
+ if(json_object_size(object) != 3)
+ fail("invalid size after update");
+
+ if(json_integer_value(json_object_get(object, "foo")) != 3)
+ fail("json_object_update_recursive failed to update existing key");
+
+ if(json_integer_value(json_object_get(object, "bar")) != 4)
+ fail("json_object_update_recursive failed to overwrite object");
+
+ if(json_integer_value(json_object_get(object, "baz")) != 5)
+ fail("json_object_update_recursive didn't add new item");
+
+ json_decref(object);
+ json_decref(other);
+
+ object = json_pack("{sis{si}}", "foo", 1, "bar", "baz", 2);
+ other = json_pack("{s{si}}", "bar", "baz", 3);
+
+ if(json_object_update_recursive(object, other))
+ fail("json_object_update_recursive failed");
+
+ if(json_object_size(object) != 2)
+ fail("invalid size after update");
+
+ if(!json_object_get(object, "foo"))
+ fail("json_object_update_recursive removed existing key");
+
+ if(json_integer_value(json_object_get(json_object_get(object, "bar"), "baz")) != 3)
+ fail("json_object_update_recursive failed to update nested value");
+
+ json_decref(object);
+ json_decref(other);
+}
+
static void test_circular()
{
json_t *object1, *object2;
@@ -667,6 +718,7 @@ static void run_tests()
test_update();
test_set_many_keys();
test_conditional_updates();
+ test_recursive_updates();
test_circular();
test_set_nocheck();
test_iterators();