From 32d149c8f60a5bdad4f678013a83d052ffd8ad43 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Thu, 13 Dec 2012 11:46:04 +0100 Subject: [PATCH] probably worth an option for this --- json_object.c | 2 +- json_object.h | 4 ++++ tests/test_locale.c | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/json_object.c b/json_object.c index b078017..9161b00 100644 --- a/json_object.c +++ b/json_object.c @@ -562,7 +562,7 @@ static int json_object_double_to_json_string(struct json_object* jso, } else { p = strchr(buf, '.'); } - if (p) { + if (p && (flags & JSON_C_TO_STRING_NOZERO)) { /* last useful digit, always keep 1 zero */ p++; for (q=p ; *q ; q++) { diff --git a/json_object.h b/json_object.h index 2621112..1f70461 100644 --- a/json_object.h +++ b/json_object.h @@ -42,6 +42,10 @@ extern "C" { * for an example of the format. */ #define JSON_C_TO_STRING_PRETTY (1<<1) +/** + * A flag to drop trailing zero for float values + */ +#define JSON_C_TO_STRING_NOZERO (1<<2) #undef FALSE #define FALSE ((json_bool)0) diff --git a/tests/test_locale.c b/tests/test_locale.c index 6926a5d..da070cf 100644 --- a/tests/test_locale.c +++ b/tests/test_locale.c @@ -23,8 +23,9 @@ int main(int argc, char **argv) MC_SET_DEBUG(1); - new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0]"); + new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); + printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO)); json_object_put(new_obj); } -- 2.40.0