cJSON_ConfigurationChangeFormat

This commit is contained in:
Max Bruckner 2018-02-03 02:32:17 +01:00
parent 1a8f732749
commit 78b5bed9a0
3 changed files with 49 additions and 12 deletions

34
cJSON.c
View File

@ -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, "format", &global_configuration);
if (cJSON_IsTrue(option))
{
configuration->format = true;
}
else if (cJSON_IsFalse(option))
{
configuration->format = false;
}
option = get_object_item(json, "case_sensitive", &global_configuration);
if (cJSON_IsTrue(option))
{
@ -3017,6 +3007,30 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_C
return configuration;
}
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configuration configuration, cJSON_Format format)
{
if (configuration == NULL)
{
return NULL;
}
switch (format)
{
case CJSON_FORMAT_MINIFIED:
((internal_configuration*)configuration)->format = false;
break;
case CJSON_FORMAT_DEFAULT:
((internal_configuration*)configuration)->format = true;
break;
default:
return NULL;
}
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))

View File

@ -182,6 +182,9 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Config
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end);
/* Set how many bytes should be initially allocated for printing */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size);
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);
/* Supply malloc and free functions to cJSON globally */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);

View File

@ -34,14 +34,14 @@ static void create_configuration_should_create_a_configuration(void)
internal_configuration *configuration = NULL;
int userdata = 1;
json = cJSON_Parse("{\"format\":false,\"case_sensitive\":false,\"allow_data_after_json\":false}");
json = cJSON_Parse("{\"case_sensitive\":false,\"allow_data_after_json\":false}");
TEST_ASSERT_NOT_NULL(json);
configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata);
cJSON_Delete(json);
json = NULL;
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
TEST_ASSERT_FALSE_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_FALSE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value.");
TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Incorrect userdata");
@ -165,6 +165,25 @@ static void configuration_change_prebuffer_size_should_not_allow_empty_sizes(voi
free(configuration);
}
static void configuration_change_format_should_change_format(void)
{
internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
TEST_ASSERT_NOT_NULL(configuration);
configuration = (internal_configuration*)cJSON_ConfigurationChangeFormat(configuration, CJSON_FORMAT_MINIFIED);
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_FALSE_MESSAGE(configuration->format, "Failed to set CJSON_FORMAT_MINIFIED.");
configuration = (internal_configuration*)cJSON_ConfigurationChangeFormat(configuration, CJSON_FORMAT_DEFAULT);
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_TRUE_MESSAGE(configuration->format, "Failed to set CJSON_FORMAT_DEFAULT.");
TEST_ASSERT_NULL_MESSAGE(cJSON_ConfigurationChangeFormat(configuration, (cJSON_Format)3), "Failed to detect invalid format.");
free(configuration);
}
int main(void)
{
UNITY_BEGIN();
@ -177,6 +196,7 @@ int main(void)
RUN_TEST(configuration_change_parse_end_should_change_parse_end);
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);
return UNITY_END();
}