aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-08 15:54:31 +0200
committerJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-11 10:08:19 +0200
commit826731fde22a34cfbd64035a3f20554a6b50a5cf (patch)
tree311bd9422071c9de482d50045f48f604e793ffe7
parent547374849a76225b1a6a0dd8bb84ae27a601f19d (diff)
Add function to add one json array to another one
Add a function to add one json array to another one. The json array to add can be inserted at any place of the json array to complete using 'idx' parameter. Also, correct minor mistake in 'wrap_json_object_add' description. BUG-AGL: SPEC-2867 Change-Id: If194878527e8e283644f02f52e97d08d006fc220 Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r--wrap-json.c42
-rw-r--r--wrap-json.h2
2 files changed, 43 insertions, 1 deletions
diff --git a/wrap-json.c b/wrap-json.c
index 27940be..0e0c31c 100644
--- a/wrap-json.c
+++ b/wrap-json.c
@@ -1031,7 +1031,7 @@ struct json_object *wrap_json_clone_deep(struct json_object *object)
* Adds the items of the object 'added' to the object 'dest'.
*
* @param dest the object to complete this object is modified
- * @added the object containing fields to add
+ * @param added the object containing fields to add
*
* @return the destination object 'dest'
*
@@ -1054,6 +1054,46 @@ struct json_object *wrap_json_object_add(struct json_object *dest, struct json_o
}
/**
+ * Insert content of the array 'added' at index 'idx' of 'dest' array.
+ *
+ * @param dest the array to complete, this array is modified
+ * @param added the array containing content to add
+ * @param idx the index where the 'added' array content will be inserted into
+ * 'dest' array. To insert 'added' array at the end of 'dest' array,
+ * you can set 'idx' to:
+ * - a negative value.
+ * - a valued bigger than 'dest' array length.
+ *
+ * @return the destination array 'dest'
+ *
+ * @example wrap_json_array_insert(["a","b",5],["X","Y",0.92], 1) -> ["a","X","Y",0.92,"b",5]
+ */
+struct json_object *wrap_json_array_insert_array(struct json_object *dest, struct json_object *added, int idx)
+{
+ int i, nd, na;
+ if (!json_object_is_type(dest, json_type_array) || !json_object_is_type(added, json_type_array))
+ return dest;
+ nd = (int) json_object_array_length(dest);
+ na = (int) json_object_array_length(added);
+ i = nd + na;
+ if (idx < 0 || idx > nd)
+ idx = nd;
+ while (i > idx + na) {
+ i--;
+ json_object_array_put_idx(dest,
+ i,
+ json_object_get(json_object_array_get_idx(dest, i - na)));
+ }
+ while (i > idx) {
+ i--;
+ json_object_array_put_idx(dest,
+ i,
+ json_object_get(json_object_array_get_idx(added, i - idx)));
+ }
+ return dest;
+}
+
+/**
* Sort the 'array' and returns it. Sorting is done accordingly to the
* order given by the function 'wrap_json_cmp'. If the paramater isn't
* an array, nothing is done and the parameter is returned unchanged.
diff --git a/wrap-json.h b/wrap-json.h
index f43659a..6415119 100644
--- a/wrap-json.h
+++ b/wrap-json.h
@@ -51,6 +51,8 @@ extern struct json_object *wrap_json_clone_depth(struct json_object *object, int
extern struct json_object *wrap_json_object_add(struct json_object *dest, struct json_object *added);
+extern struct json_object *wrap_json_array_insert_array(struct json_object *dest, struct json_object *added, int idx);
+
extern struct json_object *wrap_json_sort(struct json_object *array);
extern struct json_object *wrap_json_keys(struct json_object *object);
extern int wrap_json_cmp(struct json_object *x, struct json_object *y);