Browse Source

cJSON_ConfigurationChangeCaseSensitivity

Max Bruckner 7 years ago
parent
commit
eeaaaac63e
3 changed files with 29 additions and 12 deletions
  1. 11 10
      cJSON.c
  2. 2 0
      cJSON.h
  3. 16 2
      tests/configuration_tests.c

+ 11 - 10
cJSON.c

@@ -2929,16 +2929,6 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const
 
 
     /* then overwrite with other options if they exist */
     /* 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);
     option = get_object_item(json, "allow_data_after_json", &global_configuration);
     if (cJSON_IsTrue(option))
     if (cJSON_IsTrue(option))
     {
     {
@@ -3031,6 +3021,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configur
     return configuration;
     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)
 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))
     if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a))

+ 2 - 0
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;
 typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format;
 /* Change the format for printing */
 /* Change the format for printing */
 CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configuration configuration, cJSON_Format format);
 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 */
 /* Supply malloc and free functions to cJSON globally */
 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);

+ 16 - 2
tests/configuration_tests.c

@@ -34,7 +34,7 @@ static void create_configuration_should_create_a_configuration(void)
     internal_configuration *configuration = NULL;
     internal_configuration *configuration = NULL;
     int userdata = 1;
     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);
     TEST_ASSERT_NOT_NULL(json);
     configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata);
     configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata);
     cJSON_Delete(json);
     cJSON_Delete(json);
@@ -42,7 +42,7 @@ static void create_configuration_should_create_a_configuration(void)
     TEST_ASSERT_NOT_NULL(configuration);
     TEST_ASSERT_NOT_NULL(configuration);
     TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
     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->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_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(configuration->userdata == &userdata, "Incorrect userdata");
     TEST_ASSERT_TRUE_MESSAGE(global_allocate_wrapper == configuration->allocators.allocate, "Wrong malloc.");
     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);
     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)
 int main(void)
 {
 {
     UNITY_BEGIN();
     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_change_buffer_size);
     RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
     RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
     RUN_TEST(configuration_change_format_should_change_format);
     RUN_TEST(configuration_change_format_should_change_format);
+    RUN_TEST(configuration_change_case_sensitivity_should_change_case_sensitivity);
 
 
     return UNITY_END();
     return UNITY_END();
 }
 }