Add a JSON API that allows creating a JSON data structure within
variables preallocated on the stack, without allocating memory in the
heap.
Signed-off-by: Miloslav Trmač <mitr(a)redhat.com>
---
src/util/json.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/util/json.h | 11 ++++++++
2 files changed, 93 insertions(+)
diff --git a/src/util/json.c b/src/util/json.c
index 0507244..8529b5f 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -23,6 +23,8 @@
#include <config.h>
+#include <stdio.h>
+
#include "json.h"
#include "memory.h"
#include "virterror_internal.h"
@@ -659,6 +661,86 @@ int virJSONValueObjectIsNull(virJSONValuePtr object, const char
*key)
}
+/**
+ * virJSONStaticObjectInitialize:
+ * @object: destination object
+ * @pairs_buf: a buffer for storing property pairs.
+ *
+ * Initialize a pre-allocated object so that virJSONStaticObjectAppend* can
+ * be called on it.
+ *
+ * The caller is responsible for sizing @pairs_buf appropriately, one entry
+ * per virJSONStaticObjectAppend* call.
+ */
+void virJSONStaticObjectInitialize(virJSONObjectPtr object,
+ virJSONObjectPairPtr pairs_buf)
+{
+ object->npairs = 0;
+ object->pairs = pairs_buf;
+}
+
+/**
+ * virJSONStaticObjectAppendString:
+ * @object: destination object
+ * @key: property key
+ * @value_dest: a preallocated object for storing the JSON value
+ * @value: value of the property
+ *
+ * Add a string value to @object. Does not make any copies, so it is not
+ * necessary to free anything, but the caller is responsible for keeping
+ * @key, @value_dest and @value valid until it stops using @object.
+ */
+void virJSONStaticObjectAppendString(virJSONObjectPtr object, const char *key,
+ virJSONValuePtr value_dest,
+ const char *value)
+{
+ virJSONObjectPairPtr pair;
+
+ value_dest->type = VIR_JSON_TYPE_STRING;
+ value_dest->protect = 0;
+ value_dest->data.string = (char *)value;
+
+ pair = object->pairs + object->npairs;
+ pair->key = (char *)key;
+ pair->value = value_dest;
+ object->npairs++;
+}
+
+/**
+ * virJSONStaticObjectAppendNumberInt:
+ * @object: destination object
+ * @key: property key
+ * @value_dest: a preallocated object for storing the JSON value
+ * @buf: a preallocated buffer for storing the string representation of @value
+ * @buf_size: size of @buf
+ * @value: value of the property
+ *
+ * Add an integer value to @object. Does not make any copies, so it is not
+ * necessary to free anything, but the caller is responsible for keeping
+ * @key, @value_dest, @buf and @value valid until it stops using @object.
+ *
+ * @buf_size should be at least INT_BUFSIZE_BOUND(@value).
+ */
+void virJSONStaticObjectAppendNumberInt(virJSONObjectPtr object,
+ const char *key,
+ virJSONValuePtr value_dest,
+ char *buf, size_t buf_size,
+ int value)
+{
+ virJSONObjectPairPtr pair;
+
+ snprintf(buf, buf_size, "%d", value);
+ value_dest->type = VIR_JSON_TYPE_NUMBER;
+ value_dest->protect = 0;
+ value_dest->data.number = buf;
+
+ pair = object->pairs + object->npairs;
+ pair->key = (char *)key;
+ pair->value = value_dest;
+ object->npairs++;
+}
+
+
#if HAVE_YAJL
static int virJSONParserInsertValue(virJSONParserPtr parser,
virJSONValuePtr value)
diff --git a/src/util/json.h b/src/util/json.h
index bdba3dd..e2cfe6a 100644
--- a/src/util/json.h
+++ b/src/util/json.h
@@ -131,6 +131,17 @@ int virJSONValueObjectAppendNumberDouble(virJSONValuePtr object,
const char *key
int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int
boolean);
int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key);
+void virJSONStaticObjectInitialize(virJSONObjectPtr object,
+ virJSONObjectPairPtr pairs_buf);
+void virJSONStaticObjectAppendString(virJSONObjectPtr object, const char *key,
+ virJSONValuePtr value_dest,
+ const char *value);
+void virJSONStaticObjectAppendNumberInt(virJSONObjectPtr object,
+ const char *key,
+ virJSONValuePtr value_dest,
+ char *buf, size_t buf_size,
+ int value);
+
virJSONValuePtr virJSONValueFromString(const char *jsonstring);
char *virJSONValueToString(virJSONValuePtr object,
bool pretty);
--
1.7.11.4