[libvirt] [PATCH 6/9] util: json: Improve handling and docs for adding JSON objects

Peter Krempa pkrempa at redhat.com
Tue Oct 14 07:29:10 UTC 2014


The JSON structure constructor has an option to add JSON arrays to the
constructed object. The description is inaccurate as it can add any json
object even a dict. Change the docs to cover this option and add
possibility to specify NULL object to be added. These will be skipped as
we have with other specifiers.
---
 src/util/virjson.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 0dfeaed..f490f2c 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -97,8 +97,9 @@ struct _virJSONParser {
  *
  * d: double precision floating point number
  * n: json null value
- * a: json array
  *
+ * a: json object, must be non-NULL
+ * A: json object, omitted if NULL
  * The value corresponds to the selected type.
  *
  * Returns -1 on error. 1 on success, if at least one key:pair was valid 0
@@ -229,8 +230,20 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
             rc = virJSONValueObjectAppendNull(jargs, key);
         }   break;

+        case 'A':
         case 'a': {
             virJSONValuePtr val = va_arg(args, virJSONValuePtr);
+
+            if (!val) {
+                if (type == 'A')
+                    continue;
+
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("argument key '%s' must not have null value"),
+                               key);
+                goto cleanup;
+            }
+
             rc = virJSONValueObjectAppend(jargs, key, val);
         }   break;

-- 
2.1.0




More information about the libvir-list mailing list