From cf06a8e29eb419860c512afb4d3ac379a1c2b20a Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Mon, 6 Oct 2014 10:49:11 -0700 Subject: [PATCH 1/6] json-builder.h Changed include to use quotes instead of angle brackets. This allows the include to be found in the local include path. --- json-builder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-builder.h b/json-builder.h index 50a5668..eea5ba0 100644 --- a/json-builder.h +++ b/json-builder.h @@ -34,7 +34,7 @@ /* Requires json.h from json-parser * https://github.com/udp/json-parser */ -#include <json.h> +#include "json.h" #ifdef __cplusplus extern "C" From c312e792e21aab1c4a7c98375e80560f82f3ce4e Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Wed, 8 Oct 2014 15:33:56 -0700 Subject: [PATCH 2/6] Change %g to %f to scanf doubles when building JSON doc. The Python json module does not like scientific notation when loading a string using the json.loads() method. --- json-builder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-builder.c b/json-builder.c index b01d6ae..4fdbba4 100644 --- a/json-builder.c +++ b/json-builder.c @@ -664,7 +664,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) case json_double: - total += snprintf (NULL, 0, "%g", value->u.dbl); + total += snprintf (NULL, 0, "%f", value->u.dbl); if (value->u.dbl - floor (value->u.dbl) < 0.001) total += 2; From 49b2f2def770ce2a3e0153143229c837f735d4b1 Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Thu, 9 Oct 2014 13:57:44 -0700 Subject: [PATCH 3/6] json-builder.c json-builder.h Added the json_serialize_opt_no_scientific_notation option. This changes the serialization code to use %f instead of %g when it's set. --- .gitignore | 1 + json-builder.c | 36 +++++++++++++++++++++++++++--------- json-builder.h | 1 + 3 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ffd740 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +nbproject diff --git a/json-builder.c b/json-builder.c index 4fdbba4..ff050bb 100644 --- a/json-builder.c +++ b/json-builder.c @@ -98,13 +98,17 @@ const int f_spaces_around_brackets = (1 << 0); const int f_spaces_after_commas = (1 << 1); const int f_spaces_after_colons = (1 << 2); const int f_tabs = (1 << 3); +const int f_no_scientific_notation = (1 << 4); int get_serialize_flags (json_serialize_opts opts) { int flags = 0; + if (opts.opts & json_serialize_opt_no_scientific_notation) + flags |= f_no_scientific_notation; + if (opts.mode == json_serialize_mode_packed) - return 0; + return flags; if (opts.mode == json_serialize_mode_multiline) { @@ -123,6 +127,7 @@ int get_serialize_flags (json_serialize_opts opts) if (! (opts.opts & json_serialize_opt_no_space_after_colon)) flags |= f_spaces_after_colons; + return flags; } @@ -543,7 +548,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) size_t depth = 0; size_t indents = 0; int flags; - int bracket_size, comma_size, colon_size; + int bracket_size, comma_size, colon_size, no_scientific_notation; flags = get_serialize_flags (opts); @@ -552,7 +557,9 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) bracket_size = flags & f_spaces_around_brackets ? 2 : 1; comma_size = flags & f_spaces_after_commas ? 2 : 1; colon_size = flags & f_spaces_after_colons ? 2 : 1; + no_scientific_notation = flags & f_no_scientific_notation ? 1 : 0; + while (value) { json_int_t integer; @@ -663,11 +670,16 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) break; case json_double: - - total += snprintf (NULL, 0, "%f", value->u.dbl); - - if (value->u.dbl - floor (value->u.dbl) < 0.001) - total += 2; + if (no_scientific_notation) + { + total += snprintf (NULL, 0, "%f", value->u.dbl); + } + else + { + total += snprintf (NULL, 0, "%g", value->u.dbl); + } + if (value->u.dbl - floor (value->u.dbl) < 0.001) + total += 2; break; @@ -878,8 +890,14 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts case json_double: ptr = buf; - - buf += sprintf (buf, "%g", value->u.dbl); + if (flags & f_no_scientific_notation) + { + buf += sprintf (buf, "%f", value->u.dbl); + } + else + { + buf += sprintf (buf, "%g", value->u.dbl); + } if ((dot = strchr (ptr, ','))) { diff --git a/json-builder.h b/json-builder.h index eea5ba0..003ff3d 100644 --- a/json-builder.h +++ b/json-builder.h @@ -121,6 +121,7 @@ json_value * json_null_new (); #define json_serialize_opt_no_space_after_comma (1 << 3) #define json_serialize_opt_no_space_after_colon (1 << 4) #define json_serialize_opt_use_tabs (1 << 5) +#define json_serialize_opt_no_scientific_notation (1 << 6) typedef struct json_serialize_opts { From 7d9c5d28795032a3093c81a5cf23a5689b36107e Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Fri, 10 Oct 2014 15:56:50 -0700 Subject: [PATCH 4/6] Revert "json-builder.c" This reverts commit 49b2f2def770ce2a3e0153143229c837f735d4b1. --- .gitignore | 1 - json-builder.c | 36 +++++++++--------------------------- json-builder.h | 1 - 3 files changed, 9 insertions(+), 29 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 1ffd740..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -nbproject diff --git a/json-builder.c b/json-builder.c index ff050bb..4fdbba4 100644 --- a/json-builder.c +++ b/json-builder.c @@ -98,17 +98,13 @@ const int f_spaces_around_brackets = (1 << 0); const int f_spaces_after_commas = (1 << 1); const int f_spaces_after_colons = (1 << 2); const int f_tabs = (1 << 3); -const int f_no_scientific_notation = (1 << 4); int get_serialize_flags (json_serialize_opts opts) { int flags = 0; - if (opts.opts & json_serialize_opt_no_scientific_notation) - flags |= f_no_scientific_notation; - if (opts.mode == json_serialize_mode_packed) - return flags; + return 0; if (opts.mode == json_serialize_mode_multiline) { @@ -127,7 +123,6 @@ int get_serialize_flags (json_serialize_opts opts) if (! (opts.opts & json_serialize_opt_no_space_after_colon)) flags |= f_spaces_after_colons; - return flags; } @@ -548,7 +543,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) size_t depth = 0; size_t indents = 0; int flags; - int bracket_size, comma_size, colon_size, no_scientific_notation; + int bracket_size, comma_size, colon_size; flags = get_serialize_flags (opts); @@ -557,9 +552,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) bracket_size = flags & f_spaces_around_brackets ? 2 : 1; comma_size = flags & f_spaces_after_commas ? 2 : 1; colon_size = flags & f_spaces_after_colons ? 2 : 1; - no_scientific_notation = flags & f_no_scientific_notation ? 1 : 0; - while (value) { json_int_t integer; @@ -670,16 +663,11 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) break; case json_double: - if (no_scientific_notation) - { - total += snprintf (NULL, 0, "%f", value->u.dbl); - } - else - { - total += snprintf (NULL, 0, "%g", value->u.dbl); - } - if (value->u.dbl - floor (value->u.dbl) < 0.001) - total += 2; + + total += snprintf (NULL, 0, "%f", value->u.dbl); + + if (value->u.dbl - floor (value->u.dbl) < 0.001) + total += 2; break; @@ -890,14 +878,8 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts case json_double: ptr = buf; - if (flags & f_no_scientific_notation) - { - buf += sprintf (buf, "%f", value->u.dbl); - } - else - { - buf += sprintf (buf, "%g", value->u.dbl); - } + + buf += sprintf (buf, "%g", value->u.dbl); if ((dot = strchr (ptr, ','))) { diff --git a/json-builder.h b/json-builder.h index 003ff3d..eea5ba0 100644 --- a/json-builder.h +++ b/json-builder.h @@ -121,7 +121,6 @@ json_value * json_null_new (); #define json_serialize_opt_no_space_after_comma (1 << 3) #define json_serialize_opt_no_space_after_colon (1 << 4) #define json_serialize_opt_use_tabs (1 << 5) -#define json_serialize_opt_no_scientific_notation (1 << 6) typedef struct json_serialize_opts { From 0b7120948311b763645a9a85c2bbcc1622349616 Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Fri, 10 Oct 2014 15:57:26 -0700 Subject: [PATCH 5/6] Revert "Change %g to %f to scanf doubles when building JSON doc." This reverts commit c312e792e21aab1c4a7c98375e80560f82f3ce4e. --- json-builder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-builder.c b/json-builder.c index 4fdbba4..b01d6ae 100644 --- a/json-builder.c +++ b/json-builder.c @@ -664,7 +664,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts) case json_double: - total += snprintf (NULL, 0, "%f", value->u.dbl); + total += snprintf (NULL, 0, "%g", value->u.dbl); if (value->u.dbl - floor (value->u.dbl) < 0.001) total += 2; From 776639f267d03406b66ca246c9bd7ffb02bd1075 Mon Sep 17 00:00:00 2001 From: Chad Beaulac <cabeaulac@gmail.com> Date: Fri, 10 Oct 2014 16:07:59 -0700 Subject: [PATCH 6/6] Ignore NetBeans project and revert two commits --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ffd740 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +nbproject