From 5dae561d334544a3038077fe347fbcd0420f9542 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Wed, 29 Nov 2017 23:35:38 -0500 Subject: [PATCH] In json_object_deep_copy(), copy over _userdata, at least for json_type_string's with the default serializer set, and provide a way for people using custom serializers to provide a custom shallow_copy method. --- json_object.c | 181 +++++++++++++++++++++++----------- json_object.h | 33 ++++++- tests/test_deep_copy.c | 72 +++++++++++--- tests/test_deep_copy.expected | 14 ++- 4 files changed, 223 insertions(+), 77 deletions(-) diff --git a/json_object.c b/json_object.c index 6cec62f..6457f74 100644 --- a/json_object.c +++ b/json_object.c @@ -1301,89 +1301,149 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2) static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst) { - /* FIXME: this is shared between copies ; maybe add a `_user_copy` cb here */ - dst->_userdata = src->_userdata; - dst->_to_json_string = src->_to_json_string; + if (!src->_userdata && !src->_user_delete) + return 0; + + if (dst->_to_json_string == json_object_userdata_to_json_string) + { + dst->_userdata = strdup(src->_userdata); + } + // else if ... other supported serializers ... + else + { + _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string); + return -1; + } dst->_user_delete = src->_user_delete; return 0; } -static int json_object_deep_copy_recursive(struct json_object *src, struct json_object **dst) + +/** + * The default shallow copy implementation. Simply creates a new object of the same + * type but does *not* copy over _userdata nor retain any custom serializer. + * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy + * implementation that is aware of how to copy them. + * + * This always returns -1 or 1. It will never return 2 since it does not copy the serializer. + */ +int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst) { - struct json_object *jso = NULL; - struct json_object_iter iter; - size_t i; + switch (src->o_type) { + case json_type_boolean: + *dst = json_object_new_boolean(src->o.c_boolean); + break; - if (!src || !dst) { + case json_type_double: + *dst = json_object_new_double(src->o.c_double); + break; + + case json_type_int: + *dst = json_object_new_int64(src->o.c_int64); + break; + + case json_type_string: + *dst = json_object_new_string(get_string_component(src)); + break; + + case json_type_object: + *dst = json_object_new_object(); + break; + + case json_type_array: + *dst = json_object_new_array(); + break; + + default: errno = EINVAL; return -1; } - switch (src->o_type) { - case json_type_boolean: - *dst = json_object_new_boolean(src->o.c_boolean); - break; - - case json_type_double: - *dst = json_object_new_double(src->o.c_double); - break; + if (!*dst) { + errno = ENOMEM; + return -1; + } + (*dst)->_to_json_string = src->_to_json_string; + // _userdata and _user_delete are copied later + return 1; +} - case json_type_int: - *dst = json_object_new_int64(src->o.c_int64); - break; +/* + * The actual guts of json_object_deep_copy(), with a few additional args + * needed so we can keep track of where we are within the object tree. + * + * Note: caller is responsible for freeing *dst if this fails and returns -1. + */ +static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, const char *key_in_parent, size_t index_in_parent, struct json_object **dst, json_c_shallow_copy_fn shallow_copy) +{ + struct json_object_iter iter; + size_t src_array_len, ii; - case json_type_string: - *dst = json_object_new_string(get_string_component(src)); - break; + int shallow_copy_rc = 0; + shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst); + /* -1=error, 1=object created ok, 2=userdata set */ + if (shallow_copy_rc < 1) + { + errno = EINVAL; + return -1; + } + assert(*dst != NULL); - case json_type_object: - *dst = json_object_new_object(); - if (!*dst) { - errno = ENOMEM; + switch (src->o_type) { + case json_type_object: + json_object_object_foreachC(src, iter) { + struct json_object *jso = NULL; + /* This handles the `json_type_null` case */ + if (!iter.val) + jso = NULL; + else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0) + { + json_object_put(jso); return -1; } - json_object_object_foreachC(src, iter) { - /* This handles the `json_type_null` case */ - if (!iter.val) - jso = NULL; - else if (json_object_deep_copy_recursive(iter.val, &jso) < 0) - return -1; - if (json_object_object_add(*dst, iter.key, jso) < 0) - return -1; + + if (json_object_object_add(*dst, iter.key, jso) < 0) + { + json_object_put(jso); + return -1; } - break; + } + break; - case json_type_array: - *dst = json_object_new_array(); - if (!*dst) { - errno = ENOMEM; + case json_type_array: + src_array_len = json_object_array_length(src); + for (ii = 0; ii < src_array_len; ii++) { + struct json_object *jso = NULL; + struct json_object *jso1 = json_object_array_get_idx(src, ii); + /* This handles the `json_type_null` case */ + if (!jso1) + jso = NULL; + else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0) + { + json_object_put(jso); return -1; } - for (i = 0; i < json_object_array_length(src); i++) { - struct json_object *jso1 = json_object_array_get_idx(src, i); - /* This handles the `json_type_null` case */ - if (!jso1) - jso = NULL; - else if (json_object_deep_copy_recursive(jso1, &jso) < 0) - return -1; - if (json_object_array_add(*dst, jso) < 0) - return -1; + + if (json_object_array_add(*dst, jso) < 0) + { + json_object_put(jso); + return -1; } - break; + } + break; - default: - errno = EINVAL; - return -1; - }; + default: + break; + /* else, nothing to do, shallow_copy already did. */ + } - /* errno should be set by the function that faulted */ - if (!*dst) - return -1; + if (shallow_copy_rc != 2) + return json_object_copy_serializer_data(src, *dst); - return json_object_copy_serializer_data(src, *dst); + return 0; } -int json_object_deep_copy(struct json_object *src, struct json_object **dst) +int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn shallow_copy) { int rc; @@ -1393,7 +1453,10 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst) return -1; } - rc = json_object_deep_copy_recursive(src, dst); + if (shallow_copy == NULL) + shallow_copy = json_c_shallow_copy_default; + + rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy); if (rc < 0) { json_object_put(*dst); *dst = NULL; diff --git a/json_object.h b/json_object.h index ff402ff..9105d4a 100644 --- a/json_object.h +++ b/json_object.h @@ -963,6 +963,36 @@ JSON_EXPORT int json_object_set_string_len(json_object* obj, const char* new_val JSON_EXPORT int json_object_equal(struct json_object *obj1, struct json_object *obj2); +/** + * Perform a shallow copy of src into *dst as part of an overall json_object_deep_copy(). + * + * If src is part of a containing object or array, parent will be non-NULL, + * and key or index will be provided. + * When shallow_copy is called *dst will be NULL, and must be non-NULL when it returns. + * src will never be NULL. + * + * If shallow_copy sets the serializer on an object, return 2 to indicate to + * json_object_deep_copy that it should not attempt to use the standard userdata + * copy function. + * + * @return On success 1 or 2, -1 on errors + */ +typedef int (json_c_shallow_copy_fn)(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst); + +/** + * The default shallow copy implementation for use with json_object_deep_copy(). + * This simply calls the appropriate json_object_new_() function and + * copies over the serializer function (_to_json_string internal field of + * the json_object structure) but not any _userdata or _user_delete values. + * + * If you're writing a custom shallow_copy function, perhaps because you're using + * your own custom serializer, you can call this first to create the new object + * before customizing it with json_object_set_serializer(). + * + * @return 1 on success, -1 on errors, but never 2. + */ +json_c_shallow_copy_fn json_c_shallow_copy_default; + /** * Copy the contents of the JSON object. * The destination object must be initialized to NULL, @@ -979,8 +1009,7 @@ JSON_EXPORT int json_object_equal(struct json_object *obj1, * or if the destination pointer is non-NULL */ -extern int json_object_deep_copy(struct json_object *src, struct json_object **dst); - +JSON_EXPORT int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn shallow_copy); #ifdef __cplusplus } #endif diff --git a/tests/test_deep_copy.c b/tests/test_deep_copy.c index a0c111d..7a6e63f 100644 --- a/tests/test_deep_copy.c +++ b/tests/test_deep_copy.c @@ -7,6 +7,7 @@ #include #include "json.h" +#include "printbuf.h" static void do_benchmark(json_object *src1); @@ -77,6 +78,31 @@ static const char *json_str3 = " }" "}}"; +json_object_to_json_string_fn my_custom_serializer; +int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags) +{ + sprintbuf(pb, "OTHER"); + return 0; +} + +json_c_shallow_copy_fn my_shallow_copy; +int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst) +{ + int rc; + rc = json_c_shallow_copy_default(src, parent, key, index, dst); + if (rc < 0) + return rc; + if (key != NULL && strcmp(key, "with_serializer") == 0) + { + printf("CALLED: my_shallow_copy on with_serializer object\n"); + void *userdata = json_object_get_userdata(src); + json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL); + return 2; + } + return rc; +} + + int main(int argc, char **argv) { struct json_object *src1, *src2, *src3; @@ -99,17 +125,17 @@ int main(int argc, char **argv) printf("PASSED - loaded input data\n"); /* do this 3 times to make sure overwriting it works */ - assert(0 == json_object_deep_copy(src1, &dst1)); - assert(0 == json_object_deep_copy(src2, &dst2)); - assert(0 == json_object_deep_copy(src3, &dst3)); + assert(0 == json_object_deep_copy(src1, &dst1, NULL)); + assert(0 == json_object_deep_copy(src2, &dst2, NULL)); + assert(0 == json_object_deep_copy(src3, &dst3, NULL)); printf("PASSED - all json_object_deep_copy() returned succesful\n"); - assert(-1 == json_object_deep_copy(src1, &dst1)); + assert(-1 == json_object_deep_copy(src1, &dst1, NULL)); assert(errno == EINVAL); - assert(-1 == json_object_deep_copy(src2, &dst2)); + assert(-1 == json_object_deep_copy(src2, &dst2, NULL)); assert(errno == EINVAL); - assert(-1 == json_object_deep_copy(src3, &dst3)); + assert(-1 == json_object_deep_copy(src3, &dst3, NULL)); assert(errno == EINVAL); printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n"); @@ -130,7 +156,7 @@ int main(int argc, char **argv) printf("PASSED - comparison of string output\n"); json_object_get(dst1); - assert(-1 == json_object_deep_copy(src1, &dst1)); + assert(-1 == json_object_deep_copy(src1, &dst1, NULL)); assert(errno == EINVAL); json_object_put(dst1); @@ -139,24 +165,44 @@ int main(int argc, char **argv) printf("\nPrinting JSON objects for visual inspection\n"); printf("------------------------------------------------\n"); printf(" JSON1\n"); - printf("%s", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)); + printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)); printf("------------------------------------------------\n"); printf("------------------------------------------------\n"); printf(" JSON2\n"); - printf("%s", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)); + printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)); printf("------------------------------------------------\n"); printf("------------------------------------------------\n"); printf(" JSON3\n"); printf("------------------------------------------------\n"); - printf("%s", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)); + printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)); printf("------------------------------------------------\n"); json_object_put(dst1); json_object_put(dst2); json_object_put(dst3); + printf("\nTesting deep_copy with a custom serializer set\n"); + json_object *with_serializer = json_object_new_string("notemitted"); + + json_object_set_serializer(with_serializer, my_custom_serializer, "dummy userdata", NULL); + json_object_object_add(src1, "with_serializer", with_serializer); + dst1 = NULL; + /* With a custom serializer in use, a custom shallow_copy function must also be used */ + assert(-1 == json_object_deep_copy(src1, &dst1, NULL)); + assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy)); + + json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer"); + assert(dest_with_serializer != NULL); + char *dst_userdata = json_object_get_userdata(dest_with_serializer); + assert(strcmp(dst_userdata, "dummy userdata") == 0); + + const char *special_output = json_object_to_json_string(dest_with_serializer); + assert(strcmp(special_output, "OTHER") == 0); + printf("\ndeep_copy with custom serializer worked OK.\n"); + json_object_put(dst1); + if (benchmark) { do_benchmark(src2); @@ -177,7 +223,7 @@ static void do_benchmark(json_object *src2) /** * The numbers that I got are: * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds - * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2)' took 29 seconds + * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds */ int iterations = 1000000; @@ -193,10 +239,10 @@ static void do_benchmark(json_object *src2) start = time(NULL); dst2 = NULL; for (ii = 0; ii < iterations; ii++) { - json_object_deep_copy(src2, &dst2); + json_object_deep_copy(src2, &dst2, NULL); json_object_put(dst2); dst2 = NULL; } - printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2)' took %d seconds\n", iterations, (int)(time(NULL) - start)); + printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d seconds\n", iterations, (int)(time(NULL) - start)); } diff --git a/tests/test_deep_copy.expected b/tests/test_deep_copy.expected index ce3358c..d009e94 100644 --- a/tests/test_deep_copy.expected +++ b/tests/test_deep_copy.expected @@ -32,7 +32,8 @@ Printing JSON objects for visual inspection } } } -}------------------------------------------------ +} +------------------------------------------------ ------------------------------------------------ JSON2 { @@ -112,7 +113,8 @@ Printing JSON objects for visual inspection } ] } -}------------------------------------------------ +} +------------------------------------------------ ------------------------------------------------ JSON3 ------------------------------------------------ @@ -137,4 +139,10 @@ Printing JSON objects for visual inspection ] } } -}------------------------------------------------ +} +------------------------------------------------ + +Testing deep_copy with a custom serializer set +CALLED: my_shallow_copy on with_serializer object + +deep_copy with custom serializer worked OK. -- 2.40.0