[json-glib] Don't loose decimal in whole-double -> string conversion
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] Don't loose decimal in whole-double -> string conversion
- Date: Mon, 13 Mar 2017 11:35:33 +0000 (UTC)
commit b9007d48d0a3f3d819d727d825fa589fa9cc9557
Author: djcb <djcb djcbsoftware nl>
Date: Tue Aug 18 16:18:02 2015 +0300
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
json-glib/json-generator.c | 5 +++++
json-glib/tests/generator.c | 22 ++++++++++++++++++++++
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]