[syslog-ng] [PATCH] tfjson: Make $(format-json) have less unneccessary whitespace.
Gergely Nagy
algernon at balabit.hu
Fri Oct 28 15:39:15 CEST 2011
When compiling with jsonc, use a custom formatter, so that the
generated JSON does not have superflous whitespace between elements,
and the only whitespace in the JSON is in the content parts.
Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
modules/tfjson/tfjson.c | 32 ++++++++++++++++++++++++++++++++
1 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/modules/tfjson/tfjson.c b/modules/tfjson/tfjson.c
index 65daadd..eba2147 100644
--- a/modules/tfjson/tfjson.c
+++ b/modules/tfjson/tfjson.c
@@ -28,7 +28,9 @@
#include "config.h"
#ifdef HAVE_JSON_C
+#include <printbuf.h>
#include <json.h>
+#include <json_object_private.h>
#endif
#ifdef HAVE_JSON_GLIB
@@ -54,6 +56,35 @@ tf_json_prepare(LogTemplateFunction *self, LogTemplate *parent,
}
#if HAVE_JSON_C
+static int
+tf_json_object_to_string (struct json_object *jso,
+ struct printbuf *pb)
+{
+ int i = 0;
+ struct json_object_iter iter;
+ sprintbuf (pb, "{");
+
+ json_object_object_foreachC (jso, iter)
+ {
+ gchar *esc;
+
+ if (i)
+ sprintbuf (pb, ",");
+ sprintbuf (pb, "\"");
+ esc = g_strescape (iter.key, NULL);
+ sprintbuf (pb, esc);
+ g_free (esc);
+ sprintbuf (pb, "\":");
+ if (iter.val == NULL)
+ sprintbuf (pb, "null");
+ else
+ iter.val->_to_json_string (iter.val, pb);
+ i++;
+ }
+
+ return sprintbuf(pb, "}");
+}
+
static gboolean
tf_json_foreach (const gchar *name, const gchar *value, gpointer user_data)
{
@@ -72,6 +103,7 @@ tf_json_append(GString *result, ValuePairs *vp, LogMessage *msg)
struct json_object *json;
json = json_object_new_object();
+ json->_to_json_string = tf_json_object_to_string;
value_pairs_foreach(vp, tf_json_foreach, msg, 0, json);
--
1.7.7.1
More information about the syslog-ng
mailing list