From c9f39ce27c78f293477bec6812bcb4964f8fcda8 Mon Sep 17 00:00:00 2001 From: Andrea Censi <andrea@cds.caltech.edu> Date: Sat, 25 Jul 2009 05:00:44 +0000 Subject: [PATCH] added option for specifying format to JSON double formatting --- sm/apps/json2matlab.c | 2 +- sm/lib/json-c/json_object.c | 10 +++++++++- sm/lib/json-c/json_object.h | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sm/apps/json2matlab.c b/sm/apps/json2matlab.c index 05d109c..e24941e 100644 --- a/sm/apps/json2matlab.c +++ b/sm/apps/json2matlab.c @@ -128,7 +128,7 @@ void jo_write_as_matlab(JO jo, FILE*out) { fprintf(out, "%d", json_object_get_int(jo)); return; - case json_type_double: + case json_type_double: fprintf(out, "%lg", json_object_get_double(jo)); return; diff --git a/sm/lib/json-c/json_object.c b/sm/lib/json-c/json_object.c index 937c4c2..b32740e 100644 --- a/sm/lib/json-c/json_object.c +++ b/sm/lib/json-c/json_object.c @@ -357,15 +357,23 @@ int json_object_get_int(struct json_object *this) } } +const char *float_format = "%e"; + +void json_set_float_format(const char*f) { + float_format = f; +} /* json_object_double */ static int json_object_double_to_json_string(struct json_object* this, struct printbuf *pb) { +#define AC_BETTER_PRECISION #ifdef AC_BETTER_PRECISION +//#warning json: Using better precision in printing floats if( ((int) this->o.c_double) != this->o.c_double) - return sprintbuf(pb, "%25.18Lg", this->o.c_double); +// return sprintbuf(pb, "%g", this->o.c_double); + return sprintbuf(pb, float_format, this->o.c_double); else return sprintbuf(pb, "%d.0", (int) this->o.c_double); #else diff --git a/sm/lib/json-c/json_object.h b/sm/lib/json-c/json_object.h index fe9c9fa..1d589ca 100644 --- a/sm/lib/json-c/json_object.h +++ b/sm/lib/json-c/json_object.h @@ -46,6 +46,9 @@ enum json_type { /* reference counting functions */ + + + /** * Increment the reference count of json_object * @param obj the json_object instance @@ -307,4 +310,6 @@ extern struct json_object* json_object_new_string_len(const char *s, int len); */ extern char* json_object_get_string(struct json_object *obj); +extern void json_set_float_format(const char*f); + #endif -- GitLab