mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: json: make value object creator universal by supporting adding
To allow constructing of value objects stepwise explode the helper into separate steps and allow appending into existing value objects.
This commit is contained in:
parent
d793aeedf2
commit
f0363aa145
@ -1534,6 +1534,8 @@ virJSONValueNewNumberUlong;
|
|||||||
virJSONValueNewObject;
|
virJSONValueNewObject;
|
||||||
virJSONValueNewString;
|
virJSONValueNewString;
|
||||||
virJSONValueNewStringLen;
|
virJSONValueNewStringLen;
|
||||||
|
virJSONValueObjectAdd;
|
||||||
|
virJSONValueObjectAddVArgs;
|
||||||
virJSONValueObjectAppend;
|
virJSONValueObjectAppend;
|
||||||
virJSONValueObjectAppendBoolean;
|
virJSONValueObjectAppendBoolean;
|
||||||
virJSONValueObjectAppendNull;
|
virJSONValueObjectAppendNull;
|
||||||
|
@ -64,12 +64,11 @@ struct _virJSONParser {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virJSONValueObjectCreateVArgs:
|
* virJSONValueObjectAddVArgs:
|
||||||
* @obj: returns the created JSON object
|
* @obj: JSON object to add the values to
|
||||||
* @...: a key-value argument pairs, terminated by NULL
|
* @args: a key-value argument pairs, terminated by NULL
|
||||||
*
|
*
|
||||||
* Creates a JSON value object filled with key-value pairs supplied as variable
|
* Adds the key-value pairs supplied as variable argument list to @obj.
|
||||||
* argument list.
|
|
||||||
*
|
*
|
||||||
* Keys look like s:name the first letter is a type code:
|
* Keys look like s:name the first letter is a type code:
|
||||||
* Explanation of type codes:
|
* Explanation of type codes:
|
||||||
@ -104,22 +103,17 @@ struct _virJSONParser {
|
|||||||
* The value corresponds to the selected type.
|
* The value corresponds to the selected type.
|
||||||
*
|
*
|
||||||
* Returns -1 on error. 1 on success, if at least one key:pair was valid 0
|
* Returns -1 on error. 1 on success, if at least one key:pair was valid 0
|
||||||
* in case of no error but nothing was filled (@obj will be NULL).
|
* in case of no error but nothing was filled.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
virJSONValueObjectAddVArgs(virJSONValuePtr obj,
|
||||||
|
va_list args)
|
||||||
{
|
{
|
||||||
virJSONValuePtr jargs = NULL;
|
|
||||||
char type;
|
char type;
|
||||||
char *key;
|
char *key;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
*obj = NULL;
|
|
||||||
|
|
||||||
if (!(jargs = virJSONValueNewObject()))
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
while ((key = va_arg(args, char *)) != NULL) {
|
while ((key = va_arg(args, char *)) != NULL) {
|
||||||
|
|
||||||
if (strlen(key) < 3) {
|
if (strlen(key) < 3) {
|
||||||
@ -146,7 +140,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
key);
|
key);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
rc = virJSONValueObjectAppendString(jargs, key, val);
|
rc = virJSONValueObjectAppendString(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'z':
|
case 'z':
|
||||||
@ -165,7 +159,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
if (!val && (type == 'z' || type == 'y'))
|
if (!val && (type == 'z' || type == 'y'))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rc = virJSONValueObjectAppendNumberInt(jargs, key, val);
|
rc = virJSONValueObjectAppendNumberInt(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
@ -175,7 +169,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
if (!val && type == 'p')
|
if (!val && type == 'p')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rc = virJSONValueObjectAppendNumberUint(jargs, key, val);
|
rc = virJSONValueObjectAppendNumberUint(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'Z':
|
case 'Z':
|
||||||
@ -194,7 +188,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
if (!val && (type == 'Z' || type == 'Y'))
|
if (!val && (type == 'Z' || type == 'Y'))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rc = virJSONValueObjectAppendNumberLong(jargs, key, val);
|
rc = virJSONValueObjectAppendNumberLong(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'P':
|
case 'P':
|
||||||
@ -209,12 +203,12 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
if (!val && type == 'P')
|
if (!val && type == 'P')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rc = virJSONValueObjectAppendNumberLong(jargs, key, val);
|
rc = virJSONValueObjectAppendNumberLong(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'd': {
|
case 'd': {
|
||||||
double val = va_arg(args, double);
|
double val = va_arg(args, double);
|
||||||
rc = virJSONValueObjectAppendNumberDouble(jargs, key, val);
|
rc = virJSONValueObjectAppendNumberDouble(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'B':
|
case 'B':
|
||||||
@ -224,11 +218,11 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
if (!val && type == 'B')
|
if (!val && type == 'B')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rc = virJSONValueObjectAppendBoolean(jargs, key, val);
|
rc = virJSONValueObjectAppendBoolean(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'n': {
|
case 'n': {
|
||||||
rc = virJSONValueObjectAppendNull(jargs, key);
|
rc = virJSONValueObjectAppendNull(obj, key);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case 'A':
|
case 'A':
|
||||||
@ -245,7 +239,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = virJSONValueObjectAppend(jargs, key, val);
|
rc = virJSONValueObjectAppend(obj, key, val);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -259,17 +253,47 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* verify that we added at least one key-value pair */
|
/* verify that we added at least one key-value pair */
|
||||||
if (virJSONValueObjectKeysNumber(jargs) == 0) {
|
if (virJSONValueObjectKeysNumber(obj) == 0) {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
*obj = jargs;
|
|
||||||
jargs = NULL;
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
virJSONValueFree(jargs);
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virJSONValueObjectAdd(virJSONValuePtr obj, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
va_start(args, obj);
|
||||||
|
ret = virJSONValueObjectAddVArgs(obj, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virJSONValueObjectCreateVArgs(virJSONValuePtr *obj,
|
||||||
|
va_list args)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!(*obj = virJSONValueNewObject()))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/* free the object on error, or if no value objects were added */
|
||||||
|
if ((ret = virJSONValueObjectAddVArgs(*obj, args)) <= 0) {
|
||||||
|
virJSONValueFree(*obj);
|
||||||
|
*obj = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +85,11 @@ int virJSONValueObjectCreate(virJSONValuePtr *obj, ...)
|
|||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
|
||||||
int virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
int virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
||||||
ATTRIBUTE_NONNULL(1);
|
ATTRIBUTE_NONNULL(1);
|
||||||
|
int virJSONValueObjectAdd(virJSONValuePtr obj, ...)
|
||||||
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
|
||||||
|
int virJSONValueObjectAddVArgs(virJSONValuePtr obj, va_list args)
|
||||||
|
ATTRIBUTE_NONNULL(1);
|
||||||
|
|
||||||
|
|
||||||
virJSONValuePtr virJSONValueNewString(const char *data);
|
virJSONValuePtr virJSONValueNewString(const char *data);
|
||||||
virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t length);
|
virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t length);
|
||||||
|
Loading…
Reference in New Issue
Block a user