diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-09-02 15:21:56 -0500 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-09-02 15:21:56 -0500 |
commit | 38f421a2e7f4453bdc7f206681dbcbfb1b00de4c (patch) | |
tree | 0b1772e2dc05f8efb749d8f8fcb83dca7d8169d6 /json_object.c | |
parent | f74e8f8f9b1314293623c293e10fc838b89c7ddb (diff) | |
download | json-c-38f421a2e7f4453bdc7f206681dbcbfb1b00de4c.tar.gz |
Add a json_set_serializer() function to allow the string output of a json_object to be customized.
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/json_object.c b/json_object.c index 8dd13b0..d11efc5 100644 --- a/json_object.c +++ b/json_object.c @@ -46,6 +46,13 @@ const char *json_hex_chars = "0123456789abcdefABCDEF"; static void json_object_generic_delete(struct json_object* jso); 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_int_to_json_string; +static json_object_to_json_string_fn json_object_double_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; + /* ref count debugging */ @@ -134,10 +141,16 @@ extern struct json_object* json_object_get(struct json_object *jso) extern void json_object_put(struct json_object *jso) { - if(jso) { - jso->_ref_count--; - if(!jso->_ref_count) jso->_delete(jso); - } + if(jso) + { + jso->_ref_count--; + if(!jso->_ref_count) + { + if (jso->_user_delete) + jso->_user_delete(jso, jso->_userdata); + jso->_delete(jso); + } + } } @@ -187,6 +200,57 @@ enum json_type json_object_get_type(struct json_object *jso) return jso->o_type; } +/* set a custom conversion to string */ + +void json_object_set_serializer(json_object *jso, + json_object_to_json_string_fn to_string_func, + void *userdata, + json_object_delete_fn *user_delete) +{ + // First, clean up any previously existing user info + if (jso->_user_delete) + { + jso->_user_delete(jso, jso->_userdata); + } + jso->_userdata = NULL; + jso->_user_delete = NULL; + + if (to_string_func == NULL) + { + // Reset to the standard serialization function + switch(jso->o_type) + { + case json_type_null: + jso->_to_json_string = NULL; + break; + case json_type_boolean: + 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; + break; + case json_type_int: + jso->_to_json_string = &json_object_int_to_json_string; + break; + case json_type_object: + jso->_to_json_string = &json_object_object_to_json_string; + break; + case json_type_array: + jso->_to_json_string = &json_object_array_to_json_string; + break; + case json_type_string: + jso->_to_json_string = &json_object_string_to_json_string; + break; + } + return; + } + + jso->_to_json_string = to_string_func; + jso->_userdata = userdata; + jso->_user_delete = user_delete; +} + + /* extended conversion to string */ const char* json_object_to_json_string_ext(struct json_object *jso, int flags) |