@@ -55,7 +55,6 @@ static struct json_object* json_object_new(enum json_type o_type); | |||||
static json_object_to_json_string_fn json_object_object_to_json_string; | static json_object_to_json_string_fn json_object_object_to_json_string; | ||||
static json_object_to_json_string_fn json_object_boolean_to_json_string; | static json_object_to_json_string_fn json_object_boolean_to_json_string; | ||||
static json_object_to_json_string_fn json_object_int_to_json_string; | static json_object_to_json_string_fn json_object_int_to_json_string; | ||||
static json_object_to_json_string_fn json_object_double_to_json_string; | |||||
static json_object_to_json_string_fn json_object_string_to_json_string; | static json_object_to_json_string_fn json_object_string_to_json_string; | ||||
static json_object_to_json_string_fn json_object_array_to_json_string; | static json_object_to_json_string_fn json_object_array_to_json_string; | ||||
@@ -644,10 +643,10 @@ int64_t json_object_get_int64(const struct json_object *jso) | |||||
/* json_object_double */ | /* json_object_double */ | ||||
static int json_object_double_to_json_string(struct json_object* jso, | |||||
struct printbuf *pb, | |||||
int level, | |||||
int flags) | |||||
int json_object_double_to_json_string(struct json_object* jso, | |||||
struct printbuf *pb, | |||||
int level, | |||||
int flags) | |||||
{ | { | ||||
char buf[128], *p, *q; | char buf[128], *p, *q; | ||||
int size; | int size; | ||||
@@ -663,7 +662,8 @@ static int json_object_double_to_json_string(struct json_object* jso, | |||||
else | else | ||||
size = snprintf(buf, sizeof(buf), "-Infinity"); | size = snprintf(buf, sizeof(buf), "-Infinity"); | ||||
else | else | ||||
size = snprintf(buf, sizeof(buf), "%.17g", jso->o.c_double); | |||||
size = snprintf(buf, sizeof(buf), | |||||
jso->_userdata ? (const char*) jso->_userdata : "%.17g", jso->o.c_double); | |||||
p = strchr(buf, ','); | p = strchr(buf, ','); | ||||
if (p) { | if (p) { | ||||
@@ -614,6 +614,9 @@ extern int64_t json_object_get_int64(const struct json_object *obj); | |||||
/* double type methods */ | /* double type methods */ | ||||
/** Create a new empty json_object of type json_type_double | /** Create a new empty json_object of type json_type_double | ||||
* | |||||
* @see json_object_double_to_json_string() for how to set a custom format string. | |||||
* | |||||
* @param d the double | * @param d the double | ||||
* @returns a json_object of type json_type_double | * @returns a json_object of type json_type_double | ||||
*/ | */ | ||||
@@ -642,6 +645,31 @@ extern struct json_object* json_object_new_double(double d); | |||||
*/ | */ | ||||
extern struct json_object* json_object_new_double_s(double d, const char *ds); | extern struct json_object* json_object_new_double_s(double d, const char *ds); | ||||
/** Serialize a json_object of type json_type_double to a string. | |||||
* | |||||
* This function isn't meant to be called directly. Instead, you can set a | |||||
* custom format string for the serialization of this double using the | |||||
* following call (where "%.17g" actually is the default): | |||||
* | |||||
* @code | |||||
* jso = json_object_new_double(d); | |||||
* json_object_set_serializer(jso, json_object_double_to_json_string, | |||||
* "%.17g", NULL); | |||||
* @endcode | |||||
* | |||||
* @see printf(3) man page for format strings | |||||
* | |||||
* @param jso The json_type_double object that is serialized. | |||||
* @param pb The destination buffer. | |||||
* @param level Ignored. | |||||
* @param flags Ignored. | |||||
*/ | |||||
extern int json_object_double_to_json_string(struct json_object* jso, | |||||
struct printbuf *pb, | |||||
int level, | |||||
int flags); | |||||
/** Get the double floating point value of a json_object | /** Get the double floating point value of a json_object | ||||
* | * | ||||
* The type is coerced to a double if the passed object is not a double. | * The type is coerced to a double if the passed object is not a double. | ||||