/*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016 IoT.bzh
author: José Bollo <jose.bollo@iot.bzh>
#include <errno.h>
-#include <json.h>
+#include <json-c/json.h>
#include "utils-json.h"
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;
}
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;
+}
+