From eeaaaac63e6542623cd1f328fcb6f32d63ed6941 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 3 Feb 2018 02:48:14 +0100 Subject: [PATCH] cJSON_ConfigurationChangeCaseSensitivity --- cJSON.c | 21 +++++++++++---------- cJSON.h | 2 ++ tests/configuration_tests.c | 18 ++++++++++++++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cJSON.c b/cJSON.c index 25f391f..321e82c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2929,16 +2929,6 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const /* then overwrite with other options if they exist */ - option = get_object_item(json, "case_sensitive", &global_configuration); - if (cJSON_IsTrue(option)) - { - configuration->case_sensitive = true; - } - else if (cJSON_IsFalse(option)) - { - configuration->case_sensitive = false; - } - option = get_object_item(json, "allow_data_after_json", &global_configuration); if (cJSON_IsTrue(option)) { @@ -3031,6 +3021,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configur return configuration; } +CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON_Configuration configuration, cJSON_bool case_sensitive) +{ + if (configuration == NULL) + { + return NULL; + } + + ((internal_configuration*)configuration)->case_sensitive = case_sensitive; + 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 0f976b7..6ad1759 100644 --- a/cJSON.h +++ b/cJSON.h @@ -185,6 +185,8 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_C typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format; /* Change the format for printing */ 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); /* 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 3078132..f2f4f3e 100644 --- a/tests/configuration_tests.c +++ b/tests/configuration_tests.c @@ -34,7 +34,7 @@ static void create_configuration_should_create_a_configuration(void) internal_configuration *configuration = NULL; int userdata = 1; - json = cJSON_Parse("{\"case_sensitive\":false,\"allow_data_after_json\":false}"); + json = cJSON_Parse("{\"allow_data_after_json\":false}"); TEST_ASSERT_NOT_NULL(json); configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata); cJSON_Delete(json); @@ -42,7 +42,7 @@ static void create_configuration_should_create_a_configuration(void) TEST_ASSERT_NOT_NULL(configuration); 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_FALSE_MESSAGE(configuration->case_sensitive, "case_sensitive 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->userdata == &userdata, "Incorrect userdata"); TEST_ASSERT_TRUE_MESSAGE(global_allocate_wrapper == configuration->allocators.allocate, "Wrong malloc."); @@ -184,6 +184,19 @@ static void configuration_change_format_should_change_format(void) free(configuration); } +static void configuration_change_case_sensitivity_should_change_case_sensitivity(void) +{ + internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL); + TEST_ASSERT_NOT_NULL(configuration); + + configuration = (internal_configuration*)cJSON_ConfigurationChangeCaseSensitivity(configuration, false); + TEST_ASSERT_NOT_NULL(configuration); + + TEST_ASSERT_FALSE_MESSAGE(configuration->case_sensitive, "Didn't set the case sensitivity correctly."); + + free(configuration); +} + int main(void) { UNITY_BEGIN(); @@ -197,6 +210,7 @@ int main(void) RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size); 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); return UNITY_END(); }