util: add functions for interating over json object

Add function virJSONValueObjectKeysNumber, virJSONValueObjectGetKey
and virJSONValueObjectGetValue, which allow you to iterate over all
fields of json object: you can get number of fields and then get
name and value, stored in field with that name by index.

Signed-off-by: Dmitry Guryanov <dguryanov@parallels.com>
This commit is contained in:
Dmitry Guryanov 2012-05-02 22:32:37 +04:00 committed by Eric Blake
parent 0d631e9182
commit 287737f413
4 changed files with 38 additions and 0 deletions

View File

@ -235,6 +235,7 @@ Patches have also been contributed by:
Ryan Woodsmall <rwoodsmall@gmail.com>
Wido den Hollander <wido@widodh.nl>
Eugen Feller <eugen.feller@inria.fr>
Dmitry Guryanov <dguryanov@parallels.com>
[....send patches to get your name here....]

View File

@ -671,14 +671,17 @@ virJSONValueObjectAppendNumberUlong;
virJSONValueObjectAppendString;
virJSONValueObjectGet;
virJSONValueObjectGetBoolean;
virJSONValueObjectGetKey;
virJSONValueObjectGetNumberDouble;
virJSONValueObjectGetNumberInt;
virJSONValueObjectGetNumberLong;
virJSONValueObjectGetNumberUint;
virJSONValueObjectGetNumberUlong;
virJSONValueObjectGetString;
virJSONValueObjectGetValue;
virJSONValueObjectHasKey;
virJSONValueObjectIsNull;
virJSONValueObjectKeysNumber;
virJSONValueToString;

View File

@ -431,6 +431,36 @@ virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key)
return NULL;
}
int virJSONValueObjectKeysNumber(virJSONValuePtr object)
{
if (object->type != VIR_JSON_TYPE_OBJECT)
return -1;
return object->data.object.npairs;
}
const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n)
{
if (object->type != VIR_JSON_TYPE_OBJECT)
return NULL;
if (n >= object->data.object.npairs)
return NULL;
return object->data.object.pairs[n].key;
}
virJSONValuePtr virJSONValueObjectGetValue(virJSONValuePtr object, unsigned int n)
{
if (object->type != VIR_JSON_TYPE_OBJECT)
return NULL;
if (n >= object->data.object.npairs)
return NULL;
return object->data.object.pairs[n].value;
}
int virJSONValueArraySize(virJSONValuePtr array)
{
if (array->type != VIR_JSON_TYPE_ARRAY)

View File

@ -100,6 +100,10 @@ virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
int virJSONValueArraySize(virJSONValuePtr object);
virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
int virJSONValueObjectKeysNumber(virJSONValuePtr object);
const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n);
virJSONValuePtr virJSONValueObjectGetValue(virJSONValuePtr object, unsigned int n);
const char *virJSONValueGetString(virJSONValuePtr object);
int virJSONValueGetNumberInt(virJSONValuePtr object, int *value);
int virJSONValueGetNumberUint(virJSONValuePtr object, unsigned int *value);