From fa268b50177c85776023e369ae4fb954bd076f6a Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Tue, 24 Jan 2012 21:03:36 +0200 Subject: [PATCH] Add json_object_update_{existing,missing} Closes #37. --- doc/apiref.rst | 15 ++++++++++++ src/jansson.h | 2 ++ src/value.c | 32 ++++++++++++++++++++++++++ test/suites/api/check-exports | 5 ++-- test/suites/api/test_object.c | 43 +++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) diff --git a/doc/apiref.rst b/doc/apiref.rst index 3820b3f..f65dc5d 100644 --- a/doc/apiref.rst +++ b/doc/apiref.rst @@ -564,6 +564,21 @@ Unicode string and the value is any JSON value. Update *object* with the key-value pairs from *other*, overwriting existing keys. Returns 0 on success or -1 on error. +.. function:: int json_object_update_existing(json_t *object, json_t *other) + + Like :func:`json_object_update()`, but only the values of existing + keys are updated. No new keys are created. Returns 0 on success or + -1 on error. + + .. versionadded:: 2.3 + +.. function:: int json_object_update_missing(json_t *object, json_t *other) + + Like :func:`json_object_update()`, but only new keys are created. + The value of any existing key is not changed. Returns 0 on success + or -1 on error. + + .. versionadded:: 2.3 The following macro can be used to iterate through all key-value pairs in an object. diff --git a/src/jansson.h b/src/jansson.h index 274dbad..55c618d 100644 --- a/src/jansson.h +++ b/src/jansson.h @@ -130,6 +130,8 @@ int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); int json_object_del(json_t *object, const char *key); int json_object_clear(json_t *object); int json_object_update(json_t *object, json_t *other); +int json_object_update_existing(json_t *object, json_t *other); +int json_object_update_missing(json_t *object, json_t *other); void *json_object_iter(json_t *object); void *json_object_iter_at(json_t *object, const char *key); void *json_object_key_to_iter(const char *key); diff --git a/src/value.c b/src/value.c index eadf596..2018c4d 100644 --- a/src/value.c +++ b/src/value.c @@ -149,6 +149,38 @@ int json_object_update(json_t *object, json_t *other) return 0; } +int json_object_update_existing(json_t *object, json_t *other) +{ + const char *key; + json_t *value; + + if(!json_is_object(object) || !json_is_object(other)) + return -1; + + json_object_foreach(other, key, value) { + if(json_object_get(object, key)) + json_object_set_nocheck(object, key, value); + } + + return 0; +} + +int json_object_update_missing(json_t *object, json_t *other) +{ + const char *key; + json_t *value; + + if(!json_is_object(object) || !json_is_object(other)) + return -1; + + json_object_foreach(other, key, value) { + if(!json_object_get(object, key)) + json_object_set_nocheck(object, key, value); + } + + return 0; +} + void *json_object_iter(json_t *json) { json_object_t *object; diff --git a/test/suites/api/check-exports b/test/suites/api/check-exports index 0af0dd0..a7c87a9 100755 --- a/test/suites/api/check-exports +++ b/test/suites/api/check-exports @@ -1,7 +1,6 @@ #!/bin/sh -# This tests checks that the libjansson.so exports the correct -# symbols. +# This test checks that libjansson.so exports the correct symbols. # The list of symbols that the shared object should export sort >$test_log/exports <