Blame SOURCES/libvirt-util-json-Add-helper-to-return-string-or-number-properties-as-string.patch

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