aboutsummaryrefslogtreecommitdiff
path: root/src/dump.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dump.c')
-rw-r--r--src/dump.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/dump.c b/src/dump.c
index 328e93b..ba70f8d 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -191,10 +191,25 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
char buffer[MAX_REAL_STR_LENGTH];
int size;
- size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%0.17f", json_real_value(json));
+ size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
+ json_real_value(json));
if(size >= MAX_REAL_STR_LENGTH)
return -1;
+ /* Make sure there's a dot or 'e' in the output. Otherwise
+ a real is converted to an integer when decoding */
+ if(strchr(buffer, '.') == NULL &&
+ strchr(buffer, 'e') == NULL)
+ {
+ if(size + 2 >= MAX_REAL_STR_LENGTH) {
+ /* No space to append ".0" */
+ return -1;
+ }
+ buffer[size] = '.';
+ buffer[size + 1] = '0';
+ size += 2;
+ }
+
return dump(buffer, size, data);
}