[libvirt] [PATCH 7/9] util: json: Recursively deflatten objects virJSONValueObjectDeflatten

Peter Krempa posted 9 patches 8 years, 7 months ago
[libvirt] [PATCH 7/9] util: json: Recursively deflatten objects virJSONValueObjectDeflatten
Posted by Peter Krempa 8 years, 7 months ago
If a value of the first level object contains more objects needing
deflattening which would be wrapped in an actual object the function
would not recurse into them.

By this simple addition we can fully deflatten the objects.
---
 src/util/virjson.c                          |  8 +++++++-
 tests/virjsondata/deflatten-nested-out.json | 32 +++++++++++++++++++----------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 635b78e3a..0c7beafab 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1981,7 +1981,13 @@ virJSONValueObjectDeflattenWorker(const char *key,

     /* non-nested keys only need to be copied */
     if (!strchr(key, '.')) {
-        if (!(newval = virJSONValueCopy(value)))
+
+        if (virJSONValueIsObject(value))
+            newval = virJSONValueObjectDeflatten(value);
+        else
+            newval = virJSONValueCopy(value);
+
+        if (!newval)
             return -1;

         if (virJSONValueObjectHasKey(retobj, key)) {
diff --git a/tests/virjsondata/deflatten-nested-out.json b/tests/virjsondata/deflatten-nested-out.json
index acdcd1fc8..f23ed8fd5 100644
--- a/tests/virjsondata/deflatten-nested-out.json
+++ b/tests/virjsondata/deflatten-nested-out.json
@@ -1,17 +1,27 @@
 {
   "file": {
     "nest": {
-      "even.objects": "can",
-      "even.contain": "some",
-      "even.more": {
-        "deeply.nested": "objects",
-        "deeply.needing": "deflattening",
-        "deeply.some.even": "more",
-        "deeply.some.than": {
-          "others.thought.was": "even",
-          "others.thought.completely": "necessary"
-        },
-        "perhaps": "flat value"
+      "even": {
+        "objects": "can",
+        "contain": "some",
+        "more": {
+          "deeply": {
+            "nested": "objects",
+            "needing": "deflattening",
+            "some": {
+              "even": "more",
+              "than": {
+                "others": {
+                  "thought": {
+                    "was": "even",
+                    "completely": "necessary"
+                  }
+                }
+              }
+            }
+          },
+          "perhaps": "flat value"
+        }
       }
     }
   }
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 7/9] util: json: Recursively deflatten objects virJSONValueObjectDeflatten
Posted by John Ferlan 8 years, 7 months ago

On 06/27/2017 08:46 AM, Peter Krempa wrote:
> If a value of the first level object contains more objects needing
> deflattening which would be wrapped in an actual object the function
> would not recurse into them.
> 
> By this simple addition we can fully deflatten the objects.
> ---
>  src/util/virjson.c                          |  8 +++++++-
>  tests/virjsondata/deflatten-nested-out.json | 32 +++++++++++++++++++----------
>  2 files changed, 28 insertions(+), 12 deletions(-)
> 

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list