No functionality change (well, the index is now a size_t instead of int),
the function will be used later.
Signed-off-by: Miloslav Trmač <mitr(a)redhat.com>
---
src/util/json.c | 35 ++++++++++++++++++++++++++---------
src/util/json.h | 12 ++++++++++++
2 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/src/util/json.c b/src/util/json.c
index 1accde9..9b0a6dd 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -1105,15 +1105,8 @@ static int virJSONValueToStringOne(virJSONValuePtr object,
case VIR_JSON_TYPE_OBJECT:
if (yajl_gen_map_open(g) != yajl_gen_status_ok)
return -1;
- for (i = 0; i < object->data.object.npairs ; i++) {
- if (yajl_gen_string(g,
- (unsigned char *)object->data.object.pairs[i].key,
- strlen(object->data.object.pairs[i].key))
- != yajl_gen_status_ok)
- return -1;
- if (virJSONValueToStringOne(object->data.object.pairs[i].value, g) <
0)
- return -1;
- }
+ if (virJSONStringGeneratorAddProperties(g, &object->data.object) != 0)
+ return -1;
if (yajl_gen_map_close(g) != yajl_gen_status_ok)
return -1;
break;
@@ -1157,6 +1150,30 @@ static int virJSONValueToStringOne(virJSONValuePtr object,
return 0;
}
+/**
+ * virJSONStringGeneratorAddProperties:
+ * @gen: the destination generator
+ * @object: a JSON object
+ *
+ * Add properties contained in @object into the string generated by @gen.
+ * Note that this adds each property into the "current" object, it does not
+ * add a subobject.
+ */
+int virJSONStringGeneratorAddProperties(virJSONStringGeneratorPtr gen,
+ virJSONObjectPtr object)
+{
+ size_t i;
+
+ for (i = 0; i < object->npairs ; i++) {
+ if (yajl_gen_string(gen, (unsigned char *)object->pairs[i].key,
+ strlen(object->pairs[i].key)) != yajl_gen_status_ok)
+ return -1;
+ if (virJSONValueToStringOne(object->pairs[i].value, gen) < 0)
+ return -1;
+ }
+ return 0;
+}
+
char *virJSONValueToString(virJSONValuePtr object,
bool pretty)
{
diff --git a/src/util/json.h b/src/util/json.h
index e2cfe6a..390e68d 100644
--- a/src/util/json.h
+++ b/src/util/json.h
@@ -26,6 +26,9 @@
# include "internal.h"
+# if HAVE_YAJL
+# include <yajl/yajl_gen.h>
+# endif
typedef enum {
VIR_JSON_TYPE_OBJECT,
@@ -77,6 +80,12 @@ struct _virJSONValue {
} data;
};
+# if HAVE_YAJL
+typedef yajl_gen virJSONStringGeneratorPtr;
+# else
+typedef void *virJSONStringGeneratorPtr;
+# endif
+
void virJSONValueFree(virJSONValuePtr value);
virJSONValuePtr virJSONValueNewString(const char *data);
@@ -142,6 +151,9 @@ void virJSONStaticObjectAppendNumberInt(virJSONObjectPtr object,
char *buf, size_t buf_size,
int value);
+int virJSONStringGeneratorAddProperties(virJSONStringGeneratorPtr gen,
+ virJSONObjectPtr object);
+
virJSONValuePtr virJSONValueFromString(const char *jsonstring);
char *virJSONValueToString(virJSONValuePtr object,
bool pretty);
--
1.7.11.4