1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-22 14:50:27 +03:00

util: json: add virJSONValueObjectGetStringArray convenience

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>
This commit is contained in:
Marc-André Lureau 2020-11-20 22:09:43 +04:00 committed by Michal Privoznik
parent c6fcb75f77
commit b3dad96972
3 changed files with 39 additions and 0 deletions

View File

@ -2409,6 +2409,7 @@ virJSONValueObjectGetNumberUint;
virJSONValueObjectGetNumberUlong;
virJSONValueObjectGetObject;
virJSONValueObjectGetString;
virJSONValueObjectGetStringArray;
virJSONValueObjectGetValue;
virJSONValueObjectHasKey;
virJSONValueObjectIsNull;

View 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:

View 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);