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:
Eric Haszlakiewicz
2013-09-11 20:27:39 -05:00
parent b83e0f1182
commit 51993c28c2
6 changed files with 83 additions and 11 deletions

View File

@@ -601,11 +601,36 @@ static int json_object_double_to_json_string(struct json_object* jso,
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->o.c_double = d;
return jso;
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->o.c_double = d;
return jso;
}
struct json_object* json_object_new_double_s(double d, const char *ds)
{
struct json_object *jso = json_object_new_double(d);
if (!jso)
return NULL;
json_object_set_serializer(jso, json_object_userdata_to_json_string,
strdup(ds), json_object_free_userdata);
return jso;
}
int json_object_userdata_to_json_string(struct json_object *jso,
struct printbuf *pb, int level, int flags)
{
int userdata_len = strlen(jso->_userdata);
printbuf_memappend(pb, jso->_userdata, userdata_len);
return userdata_len;
}
void json_object_free_userdata(struct json_object *jso, void *userdata)
{
free(userdata);
}
double json_object_get_double(struct json_object *jso)