fix merging two json arrays 48/201048/8
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Thu, 7 Mar 2019 14:27:20 +0000 (15:27 +0100)
committerMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Mon, 25 Mar 2019 13:53:06 +0000 (14:53 +0100)
Change-Id: I2456c3e39b68e0e8fd87cc8f5da48529bb60a188
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
src/util/json-config.c

index 415a0be4080a67ddda10a21ef25b66d0ef0044a2..186f3cc1d23c82864e9238ea0952177f1b7495be 100644 (file)
@@ -102,7 +102,21 @@ static int json_object_object_merge(json_object *obj1, char *key, json_object *o
                return -ENOMEM;
        }
 
-       /* if there is, but obj1 or obj2 is not json_type_object - replace it */
+       /* if both obj1 and obj2 are arrays move contents from
+        * obj1 to obj2 and replace obj1 with obj2 */
+       if (json_object_is_type(obj1_node, json_type_array) &&
+               json_object_is_type(obj2_node, json_type_array)) {
+               size_t obj2_size  = json_object_array_length(obj2_node);
+               for (size_t i = 0; i < json_object_array_length(obj1_node); i++) {
+                       json_object *o = json_object_array_get_idx(obj1_node, i);
+                       json_object_get(o);
+                       json_object_array_put_idx(obj2_node, obj2_size++, o);
+               }
+               /* obj1 will be deleted in next condition (it'll be true) */
+       }
+
+       /* if there is object with key in obj1 (obj2), but either obj1 or obj2
+        * is not json_type_object - replace it */
        if (!json_object_is_type(obj1_node, json_type_object) ||
                !json_object_is_type(obj2_node, json_type_object)) {