From 740c64f92c24da30a8636b836a21e91fcba70463 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Wed, 2 Mar 2016 17:44:16 +0100 Subject: [PATCH] utils-json: adds function for new array/object MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I14168684cdfcae7ec0689eb45ab09aff82d66d22 Signed-off-by: José Bollo --- src/utils-json.c | 20 ++++++++++++++++++++ src/utils-json.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/utils-json.c b/src/utils-json.c index 3af2a4c..eb1f90b 100644 --- a/src/utils-json.c +++ b/src/utils-json.c @@ -117,3 +117,23 @@ int j_add_integer(struct json_object *obj, const char *key, int val) return str ? j_add(obj, key, str) : (errno = ENOMEM, 0); } +struct json_object *j_add_new_array(struct json_object *obj, const char *key) +{ + struct json_object *result = json_object_new_array(); + if (result != NULL && !j_add(obj, key, result)) { + json_object_put(result); + result = NULL; + } + return result; +} + +struct json_object *j_add_new_object(struct json_object *obj, const char *key) +{ + struct json_object *result = json_object_new_object(); + if (result != NULL && !j_add(obj, key, result)) { + json_object_put(result); + result = NULL; + } + return result; +} + diff --git a/src/utils-json.h b/src/utils-json.h index 4b86fa9..9d495f8 100644 --- a/src/utils-json.h +++ b/src/utils-json.h @@ -61,4 +61,6 @@ extern int j_add(struct json_object *obj, const char *key, struct json_object *v extern int j_add_string(struct json_object *obj, const char *key, const char *val); extern int j_add_boolean(struct json_object *obj, const char *key, int val); extern int j_add_integer(struct json_object *obj, const char *key, int val); +extern struct json_object *j_add_new_array(struct json_object *obj, const char *key); +extern struct json_object *j_add_new_object(struct json_object *obj, const char *key); -- 2.16.6