Add a checker to determine whether a JSON object is an array and a helper to steal objects from a JSON array. --- src/util/virjson.c | 29 +++++++++++++++++++++++++++++ src/util/virjson.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index af760a8..005406a 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -563,6 +563,13 @@ virJSONValueObjectGetValue(virJSONValuePtr object, } +bool +virJSONValueIsArray(virJSONValuePtr array) +{ + return array->type == VIR_JSON_TYPE_ARRAY; +} + + int virJSONValueArraySize(virJSONValuePtr array) { @@ -587,6 +594,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) { diff --git a/src/util/virjson.h b/src/util/virjson.h index db11396..9487729 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -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); -- 1.9.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list