X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Futils-json.c;h=74df9a949bf015237a372b28a6efa1ad928a70f8;hb=fd61febe4d3275e02d15440d6632327aa69ce636;hp=3af2a4cff7683744335ca4d65b1ba5657a87ae48;hpb=6b3c3b1916b0af0494041c12a3c86819e441105e;p=src%2Fapp-framework-main.git diff --git a/src/utils-json.c b/src/utils-json.c index 3af2a4c..74df9a9 100644 --- a/src/utils-json.c +++ b/src/utils-json.c @@ -1,5 +1,5 @@ /* - Copyright 2015 IoT.bzh + Copyright 2015, 2016 IoT.bzh author: José Bollo @@ -18,7 +18,7 @@ #include -#include +#include #include "utils-json.h" @@ -95,7 +95,10 @@ int j_integer_at(struct json_object *obj, const char *key, int defval) int j_add(struct json_object *obj, const char *key, struct json_object *val) { - json_object_object_add(obj, key, val); + if (key) + json_object_object_add(obj, key, val); + else + json_object_array_add(obj, val); return 1; } @@ -117,3 +120,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; +} +