json_dumper: add support to convert dots in underscores.
authorDario Lombardo <lomato@gmail.com>
Mon, 10 Dec 2018 12:42:17 +0000 (13:42 +0100)
committerPeter Wu <peter@lekensteyn.nl>
Mon, 10 Dec 2018 19:58:25 +0000 (19:58 +0000)
This is needed for ek json compatibility.

Change-Id: I75c74a1dc7996f3f4c17071508655ae6e3c6b94c
Reviewed-on: https://code.wireshark.org/review/30993
Petri-Dish: Dario Lombardo <lomato@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
wsutil/json_dumper.c
wsutil/json_dumper.h

index 7ae9a0674aa939a6c7def8bcc76ddd1c0657b577..6a0568ce7e2347e38df28e55b0b9004f6f7e372a 100644 (file)
@@ -37,7 +37,7 @@ enum json_dumper_change {
 };
 
 static void
-json_puts_string(FILE *fp, const char *str)
+json_puts_string(FILE *fp, const char *str, gboolean dot_to_underscore)
 {
     if (!str) {
         fputs("null", fp);
@@ -58,7 +58,10 @@ json_puts_string(FILE *fp, const char *str)
             if (str[i] == '\\' || str[i] == '"') {
                 fputc('\\', fp);
             }
-            fputc(str[i], fp);
+            if (dot_to_underscore && str[i] == '.')
+                fputc('_', fp);
+            else
+                fputc(str[i], fp);
         }
     }
     fputc('"', fp);
@@ -200,7 +203,7 @@ json_dumper_set_member_name(json_dumper *dumper, const char *name)
     }
 
     prepare_token(dumper);
-    json_puts_string(dumper->output_file, name);
+    json_puts_string(dumper->output_file, name, dumper->flags & JSON_DUMPER_DOT_TO_UNDERSCORE);
     fputc(':', dumper->output_file);
     if ((dumper->flags & JSON_DUMPER_FLAGS_PRETTY_PRINT)) {
         fputc(' ', dumper->output_file);
@@ -256,7 +259,7 @@ json_dumper_value_string(json_dumper *dumper, const char *value)
     }
 
     prepare_token(dumper);
-    json_puts_string(dumper->output_file, value);
+    json_puts_string(dumper->output_file, value, FALSE);
 
     dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_VALUE;
 }
index 98ce99a3e73d64bbb0bcf494f4c5d99b996ce844..68e1777d4677523120b797b6db898de4bd0d7b24 100644 (file)
@@ -49,6 +49,7 @@ extern "C" {
 typedef struct json_dumper {
     FILE   *output_file;    /**< Output file, must be set. */
 #define JSON_DUMPER_FLAGS_PRETTY_PRINT  (1 << 0)    /* Enable pretty printing. */
+#define JSON_DUMPER_DOT_TO_UNDERSCORE   (1 << 1)    /* Convert dots to underscores in keys */
     int     flags;
     /* for internal use, initialize with zeroes. */
     int     current_depth;