|
|
c1c534 |
From c9083a7a541a2d7231633df10dba151853fd4bca Mon Sep 17 00:00:00 2001
|
|
|
c1c534 |
Message-Id: <c9083a7a541a2d7231633df10dba151853fd4bca@dist-git>
|
|
|
c1c534 |
From: Peter Krempa <pkrempa@redhat.com>
|
|
|
c1c534 |
Date: Wed, 31 Jan 2018 12:50:59 +0100
|
|
|
c1c534 |
Subject: [PATCH] util: json: Add helper to return string or number properties
|
|
|
c1c534 |
as string
|
|
|
c1c534 |
|
|
|
c1c534 |
The helper is useful in cases when the JSON we have to parse may contain
|
|
|
c1c534 |
one of the two due to historical reasons and the number value itself
|
|
|
c1c534 |
would be stored as a string.
|
|
|
c1c534 |
|
|
|
c1c534 |
(cherry picked from commit d3da8013cc11d6a10d4a4146bcbb0a7e34523fa5)
|
|
|
c1c534 |
|
|
|
c1c534 |
https://bugzilla.redhat.com/show_bug.cgi?id=1540290
|
|
|
c1c534 |
---
|
|
|
c1c534 |
src/util/virjson.c | 27 +++++++++++++++++++++++++++
|
|
|
c1c534 |
src/util/virjson.h | 1 +
|
|
|
c1c534 |
2 files changed, 28 insertions(+)
|
|
|
c1c534 |
|
|
|
c1c534 |
diff --git a/src/util/virjson.c b/src/util/virjson.c
|
|
|
c1c534 |
index 17b11f2b3d..14b68b8c93 100644
|
|
|
c1c534 |
--- a/src/util/virjson.c
|
|
|
c1c534 |
+++ b/src/util/virjson.c
|
|
|
c1c534 |
@@ -1208,6 +1208,33 @@ virJSONValueObjectGetString(virJSONValuePtr object,
|
|
|
c1c534 |
}
|
|
|
c1c534 |
|
|
|
c1c534 |
|
|
|
c1c534 |
+/**
|
|
|
c1c534 |
+ * virJSONValueObjectGetStringOrNumber:
|
|
|
c1c534 |
+ * @object: JSON value object
|
|
|
c1c534 |
+ * @key: name of the property in @object to get
|
|
|
c1c534 |
+ *
|
|
|
c1c534 |
+ * Gets a property named @key from the JSON object @object. The value may be
|
|
|
c1c534 |
+ * a number or a string and is returned as a string. In cases when the property
|
|
|
c1c534 |
+ * is not present or is not a string or number NULL is returned.
|
|
|
c1c534 |
+ */
|
|
|
c1c534 |
+const char *
|
|
|
c1c534 |
+virJSONValueObjectGetStringOrNumber(virJSONValuePtr object,
|
|
|
c1c534 |
+ const char *key)
|
|
|
c1c534 |
+{
|
|
|
c1c534 |
+ virJSONValuePtr val = virJSONValueObjectGet(object, key);
|
|
|
c1c534 |
+
|
|
|
c1c534 |
+ if (!val)
|
|
|
c1c534 |
+ return NULL;
|
|
|
c1c534 |
+
|
|
|
c1c534 |
+ if (val->type == VIR_JSON_TYPE_STRING)
|
|
|
c1c534 |
+ return val->data.string;
|
|
|
c1c534 |
+ else if (val->type == VIR_JSON_TYPE_NUMBER)
|
|
|
c1c534 |
+ return val->data.number;
|
|
|
c1c534 |
+
|
|
|
c1c534 |
+ return NULL;
|
|
|
c1c534 |
+}
|
|
|
c1c534 |
+
|
|
|
c1c534 |
+
|
|
|
c1c534 |
int
|
|
|
c1c534 |
virJSONValueObjectGetNumberInt(virJSONValuePtr object,
|
|
|
c1c534 |
const char *key,
|
|
|
c1c534 |
diff --git a/src/util/virjson.h b/src/util/virjson.h
|
|
|
c1c534 |
index e89a776ab5..b76a3c3472 100644
|
|
|
c1c534 |
--- a/src/util/virjson.h
|
|
|
c1c534 |
+++ b/src/util/virjson.h
|
|
|
c1c534 |
@@ -149,6 +149,7 @@ virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object,
|
|
|
c1c534 |
const char *key);
|
|
|
c1c534 |
|
|
|
c1c534 |
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
|
|
|
c1c534 |
+const char *virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, const char *key);
|
|
|
c1c534 |
int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);
|
|
|
c1c534 |
int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *key, unsigned int *value);
|
|
|
c1c534 |
int virJSONValueObjectGetNumberLong(virJSONValuePtr object, const char *key, long long *value);
|
|
|
c1c534 |
--
|
|
|
c1c534 |
2.16.1
|
|
|
c1c534 |
|