]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: json: Recursively deflatten objects virJSONValueObjectDeflatten
authorPeter Krempa <pkrempa@redhat.com>
Mon, 26 Jun 2017 17:37:18 +0000 (19:37 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 11 Jul 2017 12:20:05 +0000 (14:20 +0200)
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
tests/virjsondata/deflatten-nested-out.json

index 665278b0aa89d1eb970576cb0dedd3c989a002ef..17b11f2b3dd3408647a5836cc5af9f2f8fdc6777 100644 (file)
@@ -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)) {
index acdcd1fc8ce8f351fa31a2b07f5836b0fa5cce43..f23ed8fd56b6315bba87f46d06b0f2eafb31c369 100644 (file)
@@ -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"
+        }
       }
     }
   }