]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add functions for interating over json object
authorDmitry Guryanov <dguryanov@parallels.com>
Wed, 2 May 2012 18:32:37 +0000 (22:32 +0400)
committerEric Blake <eblake@redhat.com>
Thu, 3 May 2012 15:07:25 +0000 (09:07 -0600)
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>
AUTHORS
src/libvirt_private.syms
src/util/json.c
src/util/json.h

diff --git a/AUTHORS b/AUTHORS
index 935a4d90a861ffd2354fe5a2b45c6ce6d883bfb6..31e3772a86ce876afd0311c0a16f1223f1efa62e 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -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....]
 
index d4038b21f2ae387ff430d2a16d50b5233a15617a..88f8a217af346d7ab212f1e2331b644884470ebd 100644 (file)
@@ -671,14 +671,17 @@ virJSONValueObjectAppendNumberUlong;
 virJSONValueObjectAppendString;
 virJSONValueObjectGet;
 virJSONValueObjectGetBoolean;
+virJSONValueObjectGetKey;
 virJSONValueObjectGetNumberDouble;
 virJSONValueObjectGetNumberInt;
 virJSONValueObjectGetNumberLong;
 virJSONValueObjectGetNumberUint;
 virJSONValueObjectGetNumberUlong;
 virJSONValueObjectGetString;
+virJSONValueObjectGetValue;
 virJSONValueObjectHasKey;
 virJSONValueObjectIsNull;
+virJSONValueObjectKeysNumber;
 virJSONValueToString;
 
 
index 3258c3f386d4461217aea3dd04b41aafa4c5a3ad..e7dc2729d83e55f55401844b5ca85ad3763e5c63 100644 (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)
index 686a8fb4b2ef105f120ec22eecc399c45e738800..436405f4c2c8a897e7d81ad5319d39218d66b1cb 100644 (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);