diff --git a/cJSON.c b/cJSON.c index 72bd852..442c3ac 100644 --- a/cJSON.c +++ b/cJSON.c @@ -3023,6 +3023,17 @@ CJSON_PUBLIC(cJSON_Context) cJSON_AllowDataAfterJson(cJSON_Context context, cJSO return context; } +CJSON_PUBLIC(cJSON_Context) cJSON_MakeDuplicateRecursive(cJSON_Context context, cJSON_bool recursive) +{ + if (context == NULL) + { + return NULL; + } + + ((internal_context*)context)->duplicate_recursive = recursive; + return context; +} + static cJSON_bool compare(const cJSON * const a, const cJSON * const b, const internal_context * const context) { if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a)) diff --git a/cJSON.h b/cJSON.h index 675594a..094785d 100644 --- a/cJSON.h +++ b/cJSON.h @@ -174,6 +174,8 @@ CJSON_PUBLIC(cJSON_Context) cJSON_SetFormat(cJSON_Context context, cJSON_Format CJSON_PUBLIC(cJSON_Context) cJSON_MakeCaseSensitive(cJSON_Context context, cJSON_bool case_sensitive); /* Change if data is allowed after the JSON */ CJSON_PUBLIC(cJSON_Context) cJSON_AllowDataAfterJson(cJSON_Context context, cJSON_bool allow_data_after_json); +/* Change if cJSON_Duplicate copies recursively */ +CJSON_PUBLIC(cJSON_Context) cJSON_MakeDuplicateRecursive(cJSON_Context context, cJSON_bool recursive); /* Supply malloc and free functions to cJSON globally */ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); diff --git a/tests/context_tests.c b/tests/context_tests.c index 0e2aeae..cbc3d6a 100644 --- a/tests/context_tests.c +++ b/tests/context_tests.c @@ -241,6 +241,18 @@ static void allow_data_after_json_should_change_allow_data_after_json(void) free(context); } +static void make_duplicate_recursive_should_make_duplicate_recursive(void) +{ + internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL); + TEST_ASSERT_NOT_NULL(context); + + context = (internal_context*)cJSON_MakeDuplicateRecursive(context, false); + TEST_ASSERT_NOT_NULL(context); + TEST_ASSERT_FALSE_MESSAGE(context->duplicate_recursive, "Duplicating is not set correctly."); + + free(context); +} + int main(void) { UNITY_BEGIN(); @@ -260,6 +272,7 @@ int main(void) RUN_TEST(set_format_should_set_format); RUN_TEST(make_case_sensitive_should_change_case_sensitivity); RUN_TEST(allow_data_after_json_should_change_allow_data_after_json); + RUN_TEST(make_duplicate_recursive_should_make_duplicate_recursive); return UNITY_END(); }