mirror of
https://github.com/json-c/json-c.git
synced 2026-04-07 06:19:07 +08:00
Issue #539: use a internal-only serializer function in json_object_new_double_s() to avoid potential conflicts with user code that uses the json_object_userdata_to_json_string serializer. Also, document the serializer-resetting behavior of json_object_set_double().
This commit is contained in:
@@ -53,6 +53,7 @@ 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_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_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;
|
||||||
|
static json_object_to_json_string_fn _json_object_userdata_to_json_string;
|
||||||
|
|
||||||
|
|
||||||
/* ref count debugging */
|
/* ref count debugging */
|
||||||
@@ -921,11 +922,22 @@ struct json_object* json_object_new_double_s(double d, const char *ds)
|
|||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
json_object_set_serializer(jso, json_object_userdata_to_json_string,
|
json_object_set_serializer(jso, _json_object_userdata_to_json_string,
|
||||||
new_ds, json_object_free_userdata);
|
new_ds, json_object_free_userdata);
|
||||||
return jso;
|
return jso;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A wrapper around json_object_userdata_to_json_string() used only
|
||||||
|
* by json_object_new_double_s() just so json_object_set_double() can
|
||||||
|
* detect when it needs to reset the serializer to the default.
|
||||||
|
*/
|
||||||
|
static int _json_object_userdata_to_json_string(struct json_object *jso,
|
||||||
|
struct printbuf *pb, int level, int flags)
|
||||||
|
{
|
||||||
|
return json_object_userdata_to_json_string(jso, pb, level, flags);
|
||||||
|
}
|
||||||
|
|
||||||
int json_object_userdata_to_json_string(struct json_object *jso,
|
int json_object_userdata_to_json_string(struct json_object *jso,
|
||||||
struct printbuf *pb, int level, int flags)
|
struct printbuf *pb, int level, int flags)
|
||||||
{
|
{
|
||||||
@@ -999,7 +1011,7 @@ int json_object_set_double(struct json_object *jso,double new_value){
|
|||||||
if (!jso || jso->o_type!=json_type_double)
|
if (!jso || jso->o_type!=json_type_double)
|
||||||
return 0;
|
return 0;
|
||||||
jso->o.c_double=new_value;
|
jso->o.c_double=new_value;
|
||||||
if (jso->_to_json_string == &json_object_userdata_to_json_string)
|
if (jso->_to_json_string == &_json_object_userdata_to_json_string)
|
||||||
json_object_set_serializer(jso, NULL, NULL, NULL);
|
json_object_set_serializer(jso, NULL, NULL, NULL);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -1354,7 +1366,8 @@ static int json_object_copy_serializer_data(struct json_object *src, struct json
|
|||||||
if (!src->_userdata && !src->_user_delete)
|
if (!src->_userdata && !src->_user_delete)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (dst->_to_json_string == json_object_userdata_to_json_string)
|
if (dst->_to_json_string == json_object_userdata_to_json_string ||
|
||||||
|
dst->_to_json_string == _json_object_userdata_to_json_string)
|
||||||
{
|
{
|
||||||
dst->_userdata = strdup(src->_userdata);
|
dst->_userdata = strdup(src->_userdata);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -801,7 +801,8 @@ JSON_EXPORT struct json_object* json_object_new_double(double d);
|
|||||||
* The userdata field is used to store the string representation, so it
|
* The userdata field is used to store the string representation, so it
|
||||||
* can't be used for other data if this function is used.
|
* can't be used for other data if this function is used.
|
||||||
*
|
*
|
||||||
* An equivalent sequence of calls is:
|
* A roughly equivalent sequence of calls, with the difference being that
|
||||||
|
* the serialization function won't be reset by json_object_set_double(), is:
|
||||||
* @code
|
* @code
|
||||||
* jso = json_object_new_double(d);
|
* jso = json_object_new_double(d);
|
||||||
* json_object_set_serializer(jso, json_object_userdata_to_json_string,
|
* json_object_set_serializer(jso, json_object_userdata_to_json_string,
|
||||||
@@ -885,6 +886,9 @@ JSON_EXPORT double json_object_get_double(const struct json_object *obj);
|
|||||||
* if it is not without any further actions. If type of obj is json_type_double
|
* if it is not without any further actions. If type of obj is json_type_double
|
||||||
* the object value is changed to new_value
|
* the object value is changed to new_value
|
||||||
*
|
*
|
||||||
|
* If the object was created with json_object_new_double_s(), the serialization
|
||||||
|
* function is reset to the default and the cached serialized value is cleared.
|
||||||
|
*
|
||||||
* @param obj the json_object instance
|
* @param obj the json_object instance
|
||||||
* @param new_value the value to be set
|
* @param new_value the value to be set
|
||||||
* @returns 1 if value is set correctly, 0 otherwise
|
* @returns 1 if value is set correctly, 0 otherwise
|
||||||
|
|||||||
Reference in New Issue
Block a user