[PATCH 2/9] util: add functions for interating over json object | |
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] | |
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@xxxxxxxxxxxxx>
---
src/util/json.c | 30 ++++++++++++++++++++++++++++++
src/util/json.h | 4 ++++
2 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/src/util/json.c b/src/util/json.c
index a85f580..9109e06 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -428,6 +428,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)
diff --git a/src/util/json.h b/src/util/json.h
index 4572654..2677ffc 100644
--- a/src/util/json.h
+++ b/src/util/json.h
@@ -99,6 +99,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);
--
1.7.1
--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list
[Virt Tools] [Libvirt Users] [Fedora Users] [Fedora Legacy] [Fedora Maintainers] [Fedora Desktop] [Fedora SELinux] [Big List of Linux Books] [Yosemite News] [Yosemite Photos] [KDE Users] [Fedora Tools]