From 287737f413e89cdc0070849865fb42b9f8afcf09 Mon Sep 17 00:00:00 2001 From: Dmitry Guryanov Date: Wed, 2 May 2012 22:32:37 +0400 Subject: [PATCH] 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 --- AUTHORS | 1 + src/libvirt_private.syms | 3 +++ src/util/json.c | 30 ++++++++++++++++++++++++++++++ src/util/json.h | 4 ++++ 4 files changed, 38 insertions(+) diff --git a/AUTHORS b/AUTHORS index 935a4d90a8..31e3772a86 100644 --- a/AUTHORS +++ b/AUTHORS @@ -235,6 +235,7 @@ Patches have also been contributed by: Ryan Woodsmall Wido den Hollander Eugen Feller + Dmitry Guryanov [....send patches to get your name here....] diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d4038b21f2..88f8a217af 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -671,14 +671,17 @@ virJSONValueObjectAppendNumberUlong; virJSONValueObjectAppendString; virJSONValueObjectGet; virJSONValueObjectGetBoolean; +virJSONValueObjectGetKey; virJSONValueObjectGetNumberDouble; virJSONValueObjectGetNumberInt; virJSONValueObjectGetNumberLong; virJSONValueObjectGetNumberUint; virJSONValueObjectGetNumberUlong; virJSONValueObjectGetString; +virJSONValueObjectGetValue; virJSONValueObjectHasKey; virJSONValueObjectIsNull; +virJSONValueObjectKeysNumber; virJSONValueToString; diff --git a/src/util/json.c b/src/util/json.c index 3258c3f386..e7dc2729d8 100644 --- a/src/util/json.c +++ b/src/util/json.c @@ -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) diff --git a/src/util/json.h b/src/util/json.h index 686a8fb4b2..436405f4c2 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -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); -- GitLab