[PATCH] tfjson: Make $(format-json) have less unneccessary whitespace.
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@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
Hi, This doesn't compile with a stock json-c installation as printbuf.h and json_object_private.h aren't installed headers. I have json-c 0.7, do the newer ones contain this? On Fri, 2011-10-28 at 15:39 +0200, Gergely Nagy wrote:
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@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);
-- Bazsi
On Sat, 2011-10-29 at 13:21 +0200, Balazs Scheidler wrote:
Hi,
This doesn't compile with a stock json-c installation as printbuf.h and json_object_private.h aren't installed headers.
I have json-c 0.7, do the newer ones contain this?
I've updated the required minimum version of json-c and applied this along with jsonparser. thanks. -- Bazsi
Balazs Scheidler <bazsi@balabit.hu> writes:
On Sat, 2011-10-29 at 13:21 +0200, Balazs Scheidler wrote:
Hi,
This doesn't compile with a stock json-c installation as printbuf.h and json_object_private.h aren't installed headers.
I have json-c 0.7, do the newer ones contain this?
I've updated the required minimum version of json-c and applied this along with jsonparser.
Oops, yeah, it needs a newer json-c. Forgot to update that part, sorry! -- |8]
participants (2)
-
Balazs Scheidler
-
Gergely Nagy