X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fdump.c;h=bc06dfd3934a6c790d1862380bf819f8a89d511f;hb=047a1417fb4f09f995eb2671ee52253f7f1e1e76;hp=328e93b17314924aee50f43da0f0608094892c11;hpb=19a606d7361e73ff2f5e0b240c2adb07b35e4273;p=jansson.git diff --git a/src/dump.c b/src/dump.c index 328e93b..bc06dfd 100644 --- a/src/dump.c +++ b/src/dump.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -191,10 +190,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); }