[PATCH 05/12] util: json: add helper to iterate JSON object key=value pairs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This helper easies iterating all key=value pairs stored in a JSON
object. Usually we pick only certain known keys from a JSON object, but
this will allow to walk complete objects and have the callback act on
those.
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 33 +++++++++++++++++++++++++++++++++
 src/util/virjson.h       |  8 ++++++++
 3 files changed, 42 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 70c81a8..196b837 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1548,6 +1548,7 @@ virJSONValueObjectAppendNumberUlong;
 virJSONValueObjectAppendString;
 virJSONValueObjectCreate;
 virJSONValueObjectCreateVArgs;
+virJSONValueObjectForeachKeyValue;
 virJSONValueObjectGet;
 virJSONValueObjectGetBoolean;
 virJSONValueObjectGetKey;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 3e00650..ec1778d 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1198,6 +1198,39 @@ virJSONValueObjectIsNull(virJSONValuePtr object,
 }


+/**
+ * virJSONValueObjectForeachKeyValue:
+ * @object: JSON object to iterate
+ * @cb: callback to call on key-value pairs contained in the object
+ * @opaque: generic data for the callback
+ *
+ * Iterates all key=value pairs in @object. Iteration breaks if @cb returns
+ * negative value.
+ *
+ * Returns 0 if all elements were iterated, -2 if @cb returned negative value
+ * during iteration and -1 on generic errors.
+ */
+int
+virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
+                                  virJSONValueObjectIteratorFunc cb,
+                                  void *opaque)
+{
+    size_t i;
+
+    if (object->type != VIR_JSON_TYPE_OBJECT)
+        return -1;
+
+    for (i = 0; i < object->data.object.npairs; i++) {
+        virJSONObjectPairPtr elem = object->data.object.pairs + i;
+
+        if (cb(elem->key, elem->value, opaque) < 0)
+            return -2;
+    }
+
+    return 0;
+}
+
+
 #if WITH_YAJL
 static int
 virJSONParserInsertValue(virJSONParserPtr parser,
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 57010b0..9bb7461 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -157,4 +157,12 @@ virJSONValuePtr virJSONValueFromString(const char *jsonstring);
 char *virJSONValueToString(virJSONValuePtr object,
                            bool pretty);

+typedef int (*virJSONValueObjectIteratorFunc)(const char *key,
+                                              const virJSONValue *value,
+                                              void *opaque);
+
+int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
+                                      virJSONValueObjectIteratorFunc cb,
+                                      void *opaque);
+
 #endif /* __VIR_JSON_H_ */
-- 
2.2.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]