Merge branch '1.1'
[jansson.git] / src / dump.c
index 328e93b..ba70f8d 100644 (file)
@@ -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);
         }