diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2013-09-11 20:27:39 -0500 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2013-09-11 20:27:39 -0500 |
commit | 51993c28c2721b00340e1ddbd9bb133fdedf1bf4 (patch) | |
tree | e18192800a28eae1aaafb36648a3320d37e07ff5 /json_object.c | |
parent | b83e0f11826e86747885fc066fa5b06d50f60520 (diff) | |
download | json-c-51993c28c2721b00340e1ddbd9bb133fdedf1bf4.tar.gz |
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.
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/json_object.c b/json_object.c index 1b3fc76..f65ae52 100644 --- a/json_object.c +++ b/json_object.c @@ -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) |