69 lines
2.4 KiB
Diff
69 lines
2.4 KiB
Diff
|
|
From 485f2a02c79da8a7b31e972f0c652f06094bcfb9 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Eric Haszlakiewicz <erh+git@nimenees.com>
|
||
|
|
Date: Tue, 28 May 2019 02:44:22 +0000
|
||
|
|
Subject: [PATCH 2/5] Issue #486: append a missing ".0" to negative double
|
||
|
|
values too.
|
||
|
|
|
||
|
|
---
|
||
|
|
json_object.c | 11 ++++++++---
|
||
|
|
tests/test_double_serializer.c | 4 ++++
|
||
|
|
tests/test_double_serializer.expected | 1 +
|
||
|
|
3 files changed, 13 insertions(+), 3 deletions(-)
|
||
|
|
|
||
|
|
diff --git a/json_object.c b/json_object.c
|
||
|
|
index 344af51..026dab3 100644
|
||
|
|
--- a/json_object.c
|
||
|
|
+++ b/json_object.c
|
||
|
|
@@ -810,6 +810,7 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
|
||
|
|
{
|
||
|
|
const char *std_format = "%.17g";
|
||
|
|
int format_drops_decimals = 0;
|
||
|
|
+ int looks_numeric = 0;
|
||
|
|
|
||
|
|
if (!format)
|
||
|
|
{
|
||
|
|
@@ -837,11 +838,15 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
|
||
|
|
if (format == std_format || strstr(format, ".0f") == NULL)
|
||
|
|
format_drops_decimals = 1;
|
||
|
|
|
||
|
|
+ looks_numeric = /* Looks like *some* kind of number */
|
||
|
|
+ isdigit((unsigned char)buf[0]) ||
|
||
|
|
+ (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
|
||
|
|
+
|
||
|
|
if (size < (int)sizeof(buf) - 2 &&
|
||
|
|
- isdigit((unsigned char)buf[0]) && /* Looks like *some* kind of number */
|
||
|
|
- !p && /* Has no decimal point */
|
||
|
|
+ looks_numeric &&
|
||
|
|
+ !p && /* Has no decimal point */
|
||
|
|
strchr(buf, 'e') == NULL && /* Not scientific notation */
|
||
|
|
- format_drops_decimals)
|
||
|
|
+ format_drops_decimals)
|
||
|
|
{
|
||
|
|
// Ensure it looks like a float, even if snprintf didn't,
|
||
|
|
// unless a custom format is set to omit the decimal.
|
||
|
|
diff --git a/tests/test_double_serializer.c b/tests/test_double_serializer.c
|
||
|
|
index 0f7a60e..21612c8 100644
|
||
|
|
--- a/tests/test_double_serializer.c
|
||
|
|
+++ b/tests/test_double_serializer.c
|
||
|
|
@@ -74,4 +74,8 @@ int main()
|
||
|
|
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||
|
|
|
||
|
|
json_object_put(obj);
|
||
|
|
+
|
||
|
|
+ obj = json_object_new_double(-12.0);
|
||
|
|
+ printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
|
||
|
|
+
|
||
|
|
}
|
||
|
|
diff --git a/tests/test_double_serializer.expected b/tests/test_double_serializer.expected
|
||
|
|
index 98eea1e..d3aef72 100644
|
||
|
|
--- a/tests/test_double_serializer.expected
|
||
|
|
+++ b/tests/test_double_serializer.expected
|
||
|
|
@@ -16,3 +16,4 @@ obj(12.0).to_string(default format)=12.0
|
||
|
|
obj(12.0).to_string(%.0f)=12
|
||
|
|
obj(12.0).to_string(%.0g)=1e+01
|
||
|
|
obj(12.0).to_string(%.1g)=12.0
|
||
|
|
+obj(-12.0).to_string(default format)=-12.0
|
||
|
|
--
|
||
|
|
1.8.3.1
|
||
|
|
|