From 51f2ad73cc0811469910d9c78c8bb72b6e0e72f4 Mon Sep 17 00:00:00 2001 From: Marcel Meulemans Date: Wed, 26 Jul 2017 22:54:24 +0200 Subject: [PATCH 1/4] A few small fixes for strict compiler settings. Especially the fix in the header is important because it forces one to tone down compile strictness if you want to include this header. Compiled with gcc and added at least -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes. --- json-builder.c | 6 +++--- json-builder.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/json-builder.c b/json-builder.c index 949977a..c51b58c 100644 --- a/json-builder.c +++ b/json-builder.c @@ -39,7 +39,7 @@ #define snprintf _snprintf #endif -const static json_serialize_opts default_opts = +static const json_serialize_opts default_opts = { json_serialize_mode_single_line, 0, @@ -99,7 +99,7 @@ const int f_spaces_after_commas = (1 << 1); const int f_spaces_after_colons = (1 << 2); const int f_tabs = (1 << 3); -int get_serialize_flags (json_serialize_opts opts) +static int get_serialize_flags (json_serialize_opts opts) { int flags = 0; @@ -357,7 +357,7 @@ json_value * json_boolean_new (int b) return value; } -json_value * json_null_new () +json_value * json_null_new (void) { json_value * value = (json_value *) calloc (1, sizeof (json_builder_value)); diff --git a/json-builder.h b/json-builder.h index 50a5668..29a36ff 100644 --- a/json-builder.h +++ b/json-builder.h @@ -107,7 +107,7 @@ json_value * json_string_new_nocopy (unsigned int length, json_char *); json_value * json_integer_new (json_int_t); json_value * json_double_new (double); json_value * json_boolean_new (int); -json_value * json_null_new (); +json_value * json_null_new (void); /*** Serializing From d668e8ed27603f1ab12494bf278ff4931d033cb7 Mon Sep 17 00:00:00 2001 From: Marcel Meulemans Date: Tue, 26 Sep 2017 14:52:23 +0200 Subject: [PATCH 2/4] Fix failing double tests The suffix ".0" was being added to every serialize double leading to numbers like 1e-28.0 and 4e-06.0 (instead of 1e-28 and 4e-06). --- json-builder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-builder.c b/json-builder.c index c51b58c..60e4c24 100644 --- a/json-builder.c +++ b/json-builder.c @@ -885,7 +885,7 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts { *dot = '.'; } - else if (!strchr (ptr, '.')) + else if (!strchr (ptr, '.') && !strchr (ptr, 'e')) { *buf ++ = '.'; *buf ++ = '0'; From d6eb388571c60686cb981b68fe87e3f4a7dafbac Mon Sep 17 00:00:00 2001 From: Marcel Meulemans Date: Tue, 26 Sep 2017 14:55:15 +0200 Subject: [PATCH 3/4] Remove dependency on libm Just to save a few bytes when compiling for embedded systems. --- json-builder.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/json-builder.c b/json-builder.c index 60e4c24..9afec57 100644 --- a/json-builder.c +++ b/json-builder.c @@ -33,12 +33,15 @@ #include #include #include -#include #ifdef _MSC_VER #define snprintf _snprintf #endif +static double floor(double d) { + return d - ((int)d % 1); +} + static const json_serialize_opts default_opts = { json_serialize_mode_single_line, From 02d099ca6e51aa4084c90adead3a2168b1fc1b5d Mon Sep 17 00:00:00 2001 From: Marcel Meulemans Date: Tue, 26 Sep 2017 14:57:21 +0200 Subject: [PATCH 4/4] Fail test if the measure size were to be less than the serialized size Doesn't fail currently, but good to check for! --- test/main.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/main.cc b/test/main.cc index 70e2857..cd25c97 100644 --- a/test/main.cc +++ b/test/main.cc @@ -127,11 +127,17 @@ void test_buf (char * buffer, size_t size, int * num_failed) char * buf = (char *) malloc (measured); json_serialize (buf, value); - printf ("serialized len: %d\n", (int) strlen (buf) + 1); + size_t serialized = (int) strlen (buf) + 1; + printf ("serialized len: %d\n", serialized); printf ("serialized:\n%s\n", buf); - if (! (value2 = json_parse_ex (&settings, buf, strlen(buf), error))) + if (serialized > measured) + { + printf ("Serialized more than measured\n"); + ++ *num_failed; + } + else if (! (value2 = json_parse_ex (&settings, buf, strlen(buf), error))) { printf ("Failed to re-parse: %s\n", error); ++ *num_failed;