diff options
author | djcb <djcb@djcbsoftware.nl> | 2015-08-18 16:18:02 +0300 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2017-03-13 09:20:45 +0000 |
commit | b9007d48d0a3f3d819d727d825fa589fa9cc9557 (patch) | |
tree | 032de0364912a52287c26593b50e334ef91e43e7 /json-glib | |
parent | 41dbbd6fd7b45c850e2942c2259f2bb23bfe52ef (diff) | |
download | json-glib-b9007d48d0a3f3d819d727d825fa589fa9cc9557.tar.gz |
Don't loose decimal in whole-double -> string conversion
When converting json to its string representation, whole-doubles (such
as 1.0) would be converted into strings without decimals ("1"). That can
be inconvenient e.g. when converting from/to GVariants.
To avoid this, append '.0' to the string representation for doubles if
they lost their decimals in the conversion.
Also add / update unit tests for this.
https://bugzilla.gnome.org/show_bug.cgi?id=753763
Diffstat (limited to 'json-glib')
-rw-r--r-- | json-glib/json-generator.c | 5 | ||||
-rw-r--r-- | json-glib/tests/generator.c | 22 | ||||
-rw-r--r-- | json-glib/tests/gvariant.c | 4 |
3 files changed, 29 insertions, 2 deletions
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c index 42681f6..879f3be 100644 --- a/json-glib/json-generator.c +++ b/json-glib/json-generator.c @@ -342,6 +342,11 @@ dump_value (JsonGenerator *generator, g_string_append (buffer, g_ascii_dtostr (buf, sizeof (buf), json_value_get_double (value))); + /* ensure doubles don't become ints */ + if (g_strstr_len (buf, G_ASCII_DTOSTR_BUF_SIZE, ".") == NULL) + { + g_string_append (buffer, ".0"); + } } break; diff --git a/json-glib/tests/generator.c b/json-glib/tests/generator.c index 79b6887..e7dabff 100644 --- a/json-glib/tests/generator.c +++ b/json-glib/tests/generator.c @@ -335,6 +335,27 @@ test_decimal_separator (void) json_node_free (node); } + +static void +test_double_stays_double (void) +{ + gchar *str; + JsonNode *node = json_node_new (JSON_NODE_VALUE); + JsonGenerator *generator = json_generator_new (); + + json_node_set_double (node, 1.0); + + json_generator_set_root (generator, node); + + str = json_generator_to_data (generator, NULL); + g_assert_cmpstr (str, ==, "1.0"); + + g_free (str); + g_object_unref (generator); + json_node_free (node); +} + + static void test_pretty (void) { @@ -427,6 +448,7 @@ main (int argc, g_test_add_func ("/generator/simple-object", test_simple_object); g_test_add_func ("/generator/nested-object", test_nested_object); g_test_add_func ("/generator/decimal-separator", test_decimal_separator); + g_test_add_func ("/generator/double-stays-double", test_double_stays_double); g_test_add_func ("/generator/pretty", test_pretty); for (i = 0; i < G_N_ELEMENTS (string_fixtures); i++) diff --git a/json-glib/tests/gvariant.c b/json-glib/tests/gvariant.c index 701997d..e88b1b9 100644 --- a/json-glib/tests/gvariant.c +++ b/json-glib/tests/gvariant.c @@ -45,7 +45,7 @@ static const TestCase two_way_test_cases[] = { "/double", "(d)", "(1.23,)", "[1.23]" }, /* double */ - { "/double-whole", "(d)", "(123.0,)", "[123]" }, + { "/double-whole", "(d)", "(123.0,)", "[123.0]" }, /* string */ { "/string", "(s)", "('hello world!',)", "[\"hello world!\"]" }, @@ -158,7 +158,7 @@ static const TestCase json_to_gvariant_test_cases[] = { "/string-to-int64", "(x)", "(int64 -666999666999,)", "[\"-666999666999\"]" }, { "/string-to-uint64", "(t)", "(uint64 1999999999999999,)", "[\"1999999999999999\"]" }, { "/string-to-double", "(d)", "(1.23,)", "[\"1.23\"]" }, - { "/string-to-double-whole", "(d)", "(123.0,)", "[\"123\"]" }, + { "/string-to-double-whole", "(d)", "(123.0,)", "[\"123.0\"]" }, }; static void |