util: json: Add helpers for manipulating json arrays

Add a checker to determine whether a JSON object is an array and a
helper to steal objects from a JSON array.
This commit is contained in:
Peter Krempa 2014-06-16 15:48:11 +02:00
parent 5429a3b648
commit 5133b50372
2 changed files with 31 additions and 0 deletions

View File

@ -564,6 +564,13 @@ virJSONValueObjectGetValue(virJSONValuePtr object,
}
bool
virJSONValueIsArray(virJSONValuePtr array)
{
return array->type == VIR_JSON_TYPE_ARRAY;
}
int
virJSONValueArraySize(virJSONValuePtr array)
{
@ -588,6 +595,28 @@ virJSONValueArrayGet(virJSONValuePtr array,
}
virJSONValuePtr
virJSONValueArraySteal(virJSONValuePtr array,
unsigned int element)
{
virJSONValuePtr ret = NULL;
if (array->type != VIR_JSON_TYPE_ARRAY)
return NULL;
if (element >= array->data.array.nvalues)
return NULL;
ret = array->data.array.values[element];
VIR_DELETE_ELEMENT(array->data.array.values,
element,
array->data.array.nvalues);
return ret;
}
const char *
virJSONValueGetString(virJSONValuePtr string)
{

View File

@ -97,8 +97,10 @@ int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key);
virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
bool virJSONValueIsArray(virJSONValuePtr array);
int virJSONValueArraySize(virJSONValuePtr object);
virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
virJSONValuePtr virJSONValueArraySteal(virJSONValuePtr object, unsigned int element);
int virJSONValueObjectKeysNumber(virJSONValuePtr object);
const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n);