diff --git a/cJSON.c b/cJSON.c index 321e82c..a54d7f7 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2892,7 +2892,6 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const json, const cJSON_Allocators * const allocators, void *allocator_userdata) { internal_configuration *configuration = NULL; - cJSON *option = NULL; const cJSON_Allocators *local_allocators = &global_configuration.allocators; if (allocators != NULL) @@ -2927,18 +2926,6 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const return configuration; } - /* then overwrite with other options if they exist */ - - option = get_object_item(json, "allow_data_after_json", &global_configuration); - if (cJSON_IsTrue(option)) - { - configuration->allow_data_after_json = true; - } - else if (cJSON_IsFalse(option)) - { - configuration->allow_data_after_json = false; - } - return (cJSON_Configuration)configuration; fail: @@ -3032,6 +3019,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON return configuration; } +CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllowDataAfterJson(cJSON_Configuration configuration, cJSON_bool allow_data_after_json) +{ + if (configuration == NULL) + { + return NULL; + } + + ((internal_configuration*)configuration)->allow_data_after_json = allow_data_after_json; + return configuration; +} + static cJSON_bool compare(const cJSON * const a, const cJSON * const b, const internal_configuration * const configuration) { if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a)) diff --git a/cJSON.h b/cJSON.h index 6ad1759..21a6a43 100644 --- a/cJSON.h +++ b/cJSON.h @@ -187,6 +187,8 @@ typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Forma CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configuration configuration, cJSON_Format format); /* Change the case sensitivity */ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON_Configuration configuration, cJSON_bool case_sensitive); +/* Change if data is allowed after the JSON */ +CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllowDataAfterJson(cJSON_Configuration configuration, cJSON_bool allow_data_after_json); /* Supply malloc and free functions to cJSON globally */ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); diff --git a/tests/configuration_tests.c b/tests/configuration_tests.c index f2f4f3e..1587e8e 100644 --- a/tests/configuration_tests.c +++ b/tests/configuration_tests.c @@ -43,7 +43,7 @@ static void create_configuration_should_create_a_configuration(void) TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value."); TEST_ASSERT_TRUE_MESSAGE(configuration->format, "format has an incorrect value."); TEST_ASSERT_TRUE_MESSAGE(configuration->case_sensitive, "case_sensitive has an incorrect value."); - TEST_ASSERT_FALSE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value."); + TEST_ASSERT_TRUE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value."); TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Incorrect userdata"); TEST_ASSERT_TRUE_MESSAGE(global_allocate_wrapper == configuration->allocators.allocate, "Wrong malloc."); TEST_ASSERT_TRUE_MESSAGE(global_reallocate_wrapper == configuration->allocators.reallocate, "Wrong realloc."); @@ -197,6 +197,19 @@ static void configuration_change_case_sensitivity_should_change_case_sensitivity free(configuration); } +static void configuration_change_allow_data_after_json_should_change_allow_data_after_json(void) +{ + internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL); + TEST_ASSERT_NOT_NULL(configuration); + + configuration = (internal_configuration*)cJSON_ConfigurationChangeAllowDataAfterJson(configuration, false); + TEST_ASSERT_NOT_NULL(configuration); + + TEST_ASSERT_FALSE_MESSAGE(configuration->allow_data_after_json, "Didn't set allow_data_after_json property correctly."); + + free(configuration); +} + int main(void) { UNITY_BEGIN(); @@ -211,6 +224,7 @@ int main(void) RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes); RUN_TEST(configuration_change_format_should_change_format); RUN_TEST(configuration_change_case_sensitivity_should_change_case_sensitivity); + RUN_TEST(configuration_change_allow_data_after_json_should_change_allow_data_after_json); return UNITY_END(); }