Added a json_object_new_double_s() convenience function to allow an exact string representation of a double to be specified when creating the object and use it in json_tokener_parse_ex() so a re-serialized object more exactly matches the input.
Add json_object_free_userdata() and json_object_userdata_to_json_string() too.
This commit is contained in:
@@ -197,6 +197,25 @@ extern void json_object_set_serializer(json_object *jso,
|
||||
void *userdata,
|
||||
json_object_delete_fn *user_delete);
|
||||
|
||||
/**
|
||||
* Simply call free on the userdata pointer.
|
||||
* Can be used with json_object_set_serializer().
|
||||
*
|
||||
* @param jso unused
|
||||
* @param userdata the pointer that is passed to free().
|
||||
*/
|
||||
json_object_delete_fn json_object_free_userdata;
|
||||
|
||||
/**
|
||||
* Copy the jso->_userdata string over to pb as-is.
|
||||
* Can be used with json_object_set_serializer().
|
||||
*
|
||||
* @param jso The object whose _userdata is used.
|
||||
* @param pb The destination buffer.
|
||||
* @param level Ignored.
|
||||
* @param flags Ignored.
|
||||
*/
|
||||
json_object_to_json_string_fn json_object_userdata_to_json_string;
|
||||
|
||||
|
||||
/* object type methods */
|
||||
@@ -493,6 +512,29 @@ extern int64_t json_object_get_int64(struct json_object *obj);
|
||||
*/
|
||||
extern struct json_object* json_object_new_double(double d);
|
||||
|
||||
/**
|
||||
* Create a new json_object of type json_type_double, using
|
||||
* the exact serialized representation of the value.
|
||||
*
|
||||
* This allows for numbers that would otherwise get displayed
|
||||
* inefficiently (e.g. 12.3 => "12.300000000000001") to be
|
||||
* serialized with the more convenient form.
|
||||
*
|
||||
* Note: this is used by json_tokener_parse_ex() to allow for
|
||||
* an exact re-serialization of a parsed object.
|
||||
*
|
||||
* An equivalent sequence of calls is:
|
||||
* @code
|
||||
* jso = json_object_new_double(d);
|
||||
* json_object_set_serializer(d, json_object_userdata_to_json_string,
|
||||
* strdup(ds), json_object_free_userdata)
|
||||
* @endcode
|
||||
*
|
||||
* @param d the numeric value of the double.
|
||||
* @param ds the string representation of the double. This will be copied.
|
||||
*/
|
||||
extern struct json_object* json_object_new_double_s(double d, const char *ds);
|
||||
|
||||
/** 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.
|
||||
|
||||
Reference in New Issue
Block a user