mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: json: Introduce helper to prepend string into a virJSONValueObject
Libvirt treats the JSON objects as lists thus the values appear in the order they were added. To avoid too much changes introduce a helper which allows to prepend a string which will allow to keep certain outputs in order. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
13f59e6f40
commit
12a330b50c
@ -2244,6 +2244,7 @@ virJSONValueObjectGetValue;
|
|||||||
virJSONValueObjectHasKey;
|
virJSONValueObjectHasKey;
|
||||||
virJSONValueObjectIsNull;
|
virJSONValueObjectIsNull;
|
||||||
virJSONValueObjectKeysNumber;
|
virJSONValueObjectKeysNumber;
|
||||||
|
virJSONValueObjectPrependString;
|
||||||
virJSONValueObjectRemoveKey;
|
virJSONValueObjectRemoveKey;
|
||||||
virJSONValueObjectStealArray;
|
virJSONValueObjectStealArray;
|
||||||
virJSONValueObjectStealObject;
|
virJSONValueObjectStealObject;
|
||||||
|
@ -606,10 +606,11 @@ virJSONValueNewObject(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
static int
|
||||||
virJSONValueObjectAppend(virJSONValuePtr object,
|
virJSONValueObjectInsert(virJSONValuePtr object,
|
||||||
const char *key,
|
const char *key,
|
||||||
virJSONValuePtr value)
|
virJSONValuePtr value,
|
||||||
|
bool prepend)
|
||||||
{
|
{
|
||||||
virJSONObjectPair pair = { NULL, value };
|
virJSONObjectPair pair = { NULL, value };
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
@ -628,27 +629,60 @@ virJSONValueObjectAppend(virJSONValuePtr object,
|
|||||||
if (VIR_STRDUP(pair.key, key) < 0)
|
if (VIR_STRDUP(pair.key, key) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ret = VIR_APPEND_ELEMENT(object->data.object.pairs,
|
if (prepend) {
|
||||||
object->data.object.npairs, pair);
|
ret = VIR_INSERT_ELEMENT(object->data.object.pairs, 0,
|
||||||
|
object->data.object.npairs, pair);
|
||||||
|
} else {
|
||||||
|
ret = VIR_APPEND_ELEMENT(object->data.object.pairs,
|
||||||
|
object->data.object.npairs, pair);
|
||||||
|
}
|
||||||
|
|
||||||
VIR_FREE(pair.key);
|
VIR_FREE(pair.key);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virJSONValueObjectAppend(virJSONValuePtr object,
|
||||||
|
const char *key,
|
||||||
|
virJSONValuePtr value)
|
||||||
|
{
|
||||||
|
return virJSONValueObjectInsert(object, key, value, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
virJSONValueObjectInsertString(virJSONValuePtr object,
|
||||||
|
const char *key,
|
||||||
|
const char *value,
|
||||||
|
bool prepend)
|
||||||
|
{
|
||||||
|
virJSONValuePtr jvalue = virJSONValueNewString(value);
|
||||||
|
if (!jvalue)
|
||||||
|
return -1;
|
||||||
|
if (virJSONValueObjectInsert(object, key, jvalue, prepend) < 0) {
|
||||||
|
virJSONValueFree(jvalue);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
virJSONValueObjectAppendString(virJSONValuePtr object,
|
virJSONValueObjectAppendString(virJSONValuePtr object,
|
||||||
const char *key,
|
const char *key,
|
||||||
const char *value)
|
const char *value)
|
||||||
{
|
{
|
||||||
virJSONValuePtr jvalue = virJSONValueNewString(value);
|
return virJSONValueObjectInsertString(object, key, value, false);
|
||||||
if (!jvalue)
|
}
|
||||||
return -1;
|
|
||||||
if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
|
|
||||||
virJSONValueFree(jvalue);
|
int
|
||||||
return -1;
|
virJSONValueObjectPrependString(virJSONValuePtr object,
|
||||||
}
|
const char *key,
|
||||||
return 0;
|
const char *value)
|
||||||
|
{
|
||||||
|
return virJSONValueObjectInsertString(object, key, value, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -126,6 +126,7 @@ int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *
|
|||||||
int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key);
|
int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key);
|
||||||
|
|
||||||
int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value);
|
int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value);
|
||||||
|
int virJSONValueObjectPrependString(virJSONValuePtr object, const char *key, const char *value);
|
||||||
int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number);
|
int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number);
|
||||||
int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number);
|
int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number);
|
||||||
int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char *key, long long number);
|
int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char *key, long long number);
|
||||||
|
Loading…
Reference in New Issue
Block a user