util: json: Add option to skip adding a JSON object if it is NULL

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.
This commit is contained in:
Peter Krempa 2014-10-15 09:41:49 +02:00
parent 81069ae3f5
commit ddf52d01f8

View 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);