mirror of
https://github.com/json-c/json-c.git
synced 2026-03-28 09:29:06 +08:00
Make default double serializer ignore userdata again
The user might want to use the userdata for something different, so the serializer should ignore it by default. Explicitly setting the serializer to json_object_double_to_json_string will still make it interpret the userdata as a format string.
This commit is contained in:
@@ -54,6 +54,7 @@ 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_boolean_to_json_string;
|
||||
static json_object_to_json_string_fn json_object_double_to_json_string_default;
|
||||
static json_object_to_json_string_fn json_object_int_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;
|
||||
@@ -261,7 +262,7 @@ void json_object_set_serializer(json_object *jso,
|
||||
jso->_to_json_string = &json_object_boolean_to_json_string;
|
||||
break;
|
||||
case json_type_double:
|
||||
jso->_to_json_string = &json_object_double_to_json_string;
|
||||
jso->_to_json_string = &json_object_double_to_json_string_default;
|
||||
break;
|
||||
case json_type_int:
|
||||
jso->_to_json_string = &json_object_int_to_json_string;
|
||||
@@ -643,10 +644,11 @@ int64_t json_object_get_int64(const struct json_object *jso)
|
||||
|
||||
/* json_object_double */
|
||||
|
||||
int json_object_double_to_json_string(struct json_object* jso,
|
||||
struct printbuf *pb,
|
||||
int level,
|
||||
int flags)
|
||||
static int json_object_double_to_json_string_format(struct json_object* jso,
|
||||
struct printbuf *pb,
|
||||
int level,
|
||||
int flags,
|
||||
const char *format)
|
||||
{
|
||||
char buf[128], *p, *q;
|
||||
int size;
|
||||
@@ -663,7 +665,7 @@ int json_object_double_to_json_string(struct json_object* jso,
|
||||
size = snprintf(buf, sizeof(buf), "-Infinity");
|
||||
else
|
||||
size = snprintf(buf, sizeof(buf),
|
||||
jso->_userdata ? (const char*) jso->_userdata : "%.17g", jso->o.c_double);
|
||||
format ? format : "%.17g", jso->o.c_double);
|
||||
|
||||
p = strchr(buf, ',');
|
||||
if (p) {
|
||||
@@ -685,12 +687,30 @@ int json_object_double_to_json_string(struct json_object* jso,
|
||||
return size;
|
||||
}
|
||||
|
||||
static int json_object_double_to_json_string_default(struct json_object* jso,
|
||||
struct printbuf *pb,
|
||||
int level,
|
||||
int flags)
|
||||
{
|
||||
return json_object_double_to_json_string_format(jso, pb, level, flags,
|
||||
NULL);
|
||||
}
|
||||
|
||||
int json_object_double_to_json_string(struct json_object* jso,
|
||||
struct printbuf *pb,
|
||||
int level,
|
||||
int flags)
|
||||
{
|
||||
return json_object_double_to_json_string_format(jso, pb, level, flags,
|
||||
jso->_userdata);
|
||||
}
|
||||
|
||||
struct json_object* json_object_new_double(double d)
|
||||
{
|
||||
struct json_object *jso = json_object_new(json_type_double);
|
||||
if (!jso)
|
||||
return NULL;
|
||||
jso->_to_json_string = &json_object_double_to_json_string;
|
||||
jso->_to_json_string = &json_object_double_to_json_string_default;
|
||||
jso->o.c_double = d;
|
||||
return jso;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user