]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: json: Add option to skip adding a JSON object if it is NULL
authorPeter Krempa <pkrempa@redhat.com>
Wed, 15 Oct 2014 07:41:49 +0000 (09:41 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 15 Oct 2014 08:27:50 +0000 (10:27 +0200)
Add a new option specifier that will optionally add a JSON key=value pair
containing a nested object if the added object isn't NULL.

src/util/virjson.c

index 81833f73a7a39e29ea5bcdef079fca4bbb33d321..3ffa19f7db6feb387057e6a1021d64015fd8865f 100644 (file)
@@ -99,6 +99,7 @@ struct _virJSONParser {
  * n: json null value
  *
  * a: json object, must be non-NULL
+ * A: json object, omitted if NULL
  *
  * The value corresponds to the selected type.
  *
@@ -230,10 +231,14 @@ 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);