]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: json: add virJSONValueObjectGetStringArray convenience
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Fri, 20 Nov 2020 18:09:43 +0000 (22:09 +0400)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 1 Dec 2020 10:23:37 +0000 (11:23 +0100)
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Han Han <hhan@redhat.com>
src/libvirt_private.syms
src/util/virjson.c
src/util/virjson.h

index f581676227692e0dbd2d22921e0d5339c4802b0a..581857deb964aaee2d499c689e40fe86ac167fa0 100644 (file)
@@ -2409,6 +2409,7 @@ virJSONValueObjectGetNumberUint;
 virJSONValueObjectGetNumberUlong;
 virJSONValueObjectGetObject;
 virJSONValueObjectGetString;
+virJSONValueObjectGetStringArray;
 virJSONValueObjectGetValue;
 virJSONValueObjectHasKey;
 virJSONValueObjectIsNull;
index 3fa9a9ca24f64776b742e423dda6ce0225b0b6c0..d4719237327637581b130da528065de96689f2b2 100644 (file)
@@ -1463,6 +1463,43 @@ virJSONValueObjectIsNull(virJSONValuePtr object,
     return virJSONValueIsNull(val);
 }
 
+char **
+virJSONValueObjectGetStringArray(virJSONValuePtr object, const char *key)
+{
+    g_auto(GStrv) ret = NULL;
+    virJSONValuePtr data;
+    size_t n;
+    size_t i;
+
+    data = virJSONValueObjectGetArray(object, key);
+    if (!data)
+        return NULL;
+
+    n = virJSONValueArraySize(data);
+    ret = g_new0(char *, n + 1);
+    for (i = 0; i < n; i++) {
+        virJSONValuePtr child = virJSONValueArrayGet(data, i);
+        const char *tmp;
+
+        if (!child) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("%s array element is missing item %zu"),
+                           key, i);
+            return NULL;
+        }
+
+        if (!(tmp = virJSONValueGetString(child))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("%s array element does not contain a string"),
+                           key);
+            return NULL;
+        }
+
+        ret[i] = g_strdup(tmp);
+    }
+
+    return g_steal_pointer(&ret);
+}
 
 /**
  * virJSONValueObjectForeachKeyValue:
index 588c977650f1f3fe9b7d0d1119e1cd8d4400d314..4dc7ed1a2d52045eb17bb771a8f1d0c27df56a6a 100644 (file)
@@ -117,6 +117,7 @@ virJSONValuePtr virJSONValueObjectStealObject(virJSONValuePtr object,
                                               const char *key);
 
 const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
+char **virJSONValueObjectGetStringArray(virJSONValuePtr object, const char *key);
 const char *virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, const char *key);
 int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);
 int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *key, unsigned int *value);