cJSON_ConfigurationChangeAllowDataAfterJson

This commit is contained in:
Max Bruckner 2018-02-03 02:55:06 +01:00
parent 7e4ac634c6
commit d664199036
3 changed files with 28 additions and 14 deletions

24
cJSON.c
View File

@ -2893,7 +2893,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) CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const json, const cJSON_Allocators * const allocators, void *allocator_userdata)
{ {
internal_configuration *configuration = NULL; internal_configuration *configuration = NULL;
cJSON *option = NULL;
const cJSON_Allocators *local_allocators = &global_configuration.allocators; const cJSON_Allocators *local_allocators = &global_configuration.allocators;
if (allocators != NULL) if (allocators != NULL)
@ -2928,18 +2927,6 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const
return configuration; 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; return (cJSON_Configuration)configuration;
fail: fail:
@ -3033,6 +3020,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON
return configuration; 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) 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))

View File

@ -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); CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configuration configuration, cJSON_Format format);
/* Change the case sensitivity */ /* Change the case sensitivity */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON_Configuration configuration, cJSON_bool case_sensitive); 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 */ /* Supply malloc and free functions to cJSON globally */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);

View File

@ -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_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_TRUE_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->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.");
TEST_ASSERT_TRUE_MESSAGE(global_reallocate_wrapper == configuration->allocators.reallocate, "Wrong realloc."); 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); 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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
@ -211,6 +224,7 @@ int main(void)
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); 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(); return UNITY_END();
} }