mirror of
https://github.com/DaveGamble/cJSON
synced 2025-04-24 20:27:55 +00:00
Put format into internal_configuration
This commit is contained in:
parent
677f0cb1bb
commit
27977adc93
47
cJSON.c
47
cJSON.c
@ -118,6 +118,7 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned
|
||||
|
||||
typedef struct internal_configuration
|
||||
{
|
||||
cJSON_bool format;
|
||||
void *(*allocate)(size_t size);
|
||||
void (*deallocate)(void *pointer);
|
||||
void *(*reallocate)(void *pointer, size_t size);
|
||||
@ -143,7 +144,7 @@ static void *internal_realloc(void *pointer, size_t size)
|
||||
#define internal_realloc realloc
|
||||
#endif
|
||||
|
||||
static internal_configuration global_configuration = { internal_malloc, internal_free, internal_realloc };
|
||||
static internal_configuration global_configuration = { true, internal_malloc, internal_free, internal_realloc };
|
||||
|
||||
static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration)
|
||||
{
|
||||
@ -353,7 +354,6 @@ typedef struct
|
||||
size_t offset;
|
||||
size_t depth; /* current nesting depth (for formatted printing) */
|
||||
cJSON_bool noalloc;
|
||||
cJSON_bool format; /* is this print a formatted print */
|
||||
internal_configuration configuration;
|
||||
} printbuffer;
|
||||
|
||||
@ -994,7 +994,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
|
||||
/* Parse an object - create a new root, and populate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
cJSON *item = NULL;
|
||||
|
||||
/* reset error position */
|
||||
@ -1079,7 +1079,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
|
||||
|
||||
#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
|
||||
|
||||
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_configuration * const configuration)
|
||||
static unsigned char *print(const cJSON * const item, const internal_configuration * const configuration)
|
||||
{
|
||||
static const size_t default_buffer_size = 256;
|
||||
printbuffer buffer[1];
|
||||
@ -1090,7 +1090,6 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
|
||||
/* create buffer */
|
||||
buffer->buffer = (unsigned char*) configuration->allocate(default_buffer_size);
|
||||
buffer->length = default_buffer_size;
|
||||
buffer->format = format;
|
||||
buffer->configuration = *configuration;
|
||||
if (buffer->buffer == NULL)
|
||||
{
|
||||
@ -1148,17 +1147,19 @@ fail:
|
||||
/* Render a cJSON item/entity/structure to text. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
|
||||
{
|
||||
return (char*)print(item, true, &global_configuration);
|
||||
return (char*)print(item, &global_configuration);
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
|
||||
{
|
||||
return (char*)print(item, false, &global_configuration);
|
||||
internal_configuration configuration = global_configuration;
|
||||
configuration.format = false;
|
||||
return (char*)print(item, &configuration);
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format)
|
||||
{
|
||||
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
|
||||
if (prebuffer < 0)
|
||||
{
|
||||
@ -1174,8 +1175,8 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
|
||||
p.length = (size_t)prebuffer;
|
||||
p.offset = 0;
|
||||
p.noalloc = false;
|
||||
p.format = fmt;
|
||||
p.configuration = global_configuration;
|
||||
p.configuration.format = format;
|
||||
|
||||
if (!print_value(item, &p))
|
||||
{
|
||||
@ -1188,7 +1189,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
|
||||
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
|
||||
{
|
||||
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
|
||||
if ((length < 0) || (buffer == NULL))
|
||||
{
|
||||
@ -1199,8 +1200,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons
|
||||
p.length = (size_t)length;
|
||||
p.offset = 0;
|
||||
p.noalloc = true;
|
||||
p.format = format;
|
||||
p.configuration = global_configuration;
|
||||
p.configuration.format = format;
|
||||
|
||||
return print_value(item, &p);
|
||||
}
|
||||
@ -1485,14 +1486,14 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp
|
||||
update_offset(output_buffer);
|
||||
if (current_element->next)
|
||||
{
|
||||
length = (size_t) (output_buffer->format ? 2 : 1);
|
||||
length = (size_t) (output_buffer->configuration.format ? 2 : 1);
|
||||
output_pointer = ensure(output_buffer, length + 1);
|
||||
if (output_pointer == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*output_pointer++ = ',';
|
||||
if(output_buffer->format)
|
||||
if(output_buffer->configuration.format)
|
||||
{
|
||||
*output_pointer++ = ' ';
|
||||
}
|
||||
@ -1636,7 +1637,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
}
|
||||
|
||||
/* Compose the output: */
|
||||
length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
|
||||
length = (size_t) (output_buffer->configuration.format ? 2 : 1); /* fmt: {\n */
|
||||
output_pointer = ensure(output_buffer, length + 1);
|
||||
if (output_pointer == NULL)
|
||||
{
|
||||
@ -1645,7 +1646,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
|
||||
*output_pointer++ = '{';
|
||||
output_buffer->depth++;
|
||||
if (output_buffer->format)
|
||||
if (output_buffer->configuration.format)
|
||||
{
|
||||
*output_pointer++ = '\n';
|
||||
}
|
||||
@ -1653,7 +1654,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
|
||||
while (current_item)
|
||||
{
|
||||
if (output_buffer->format)
|
||||
if (output_buffer->configuration.format)
|
||||
{
|
||||
size_t i;
|
||||
output_pointer = ensure(output_buffer, output_buffer->depth);
|
||||
@ -1675,14 +1676,14 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
}
|
||||
update_offset(output_buffer);
|
||||
|
||||
length = (size_t) (output_buffer->format ? 2 : 1);
|
||||
length = (size_t) (output_buffer->configuration.format ? 2 : 1);
|
||||
output_pointer = ensure(output_buffer, length);
|
||||
if (output_pointer == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*output_pointer++ = ':';
|
||||
if (output_buffer->format)
|
||||
if (output_buffer->configuration.format)
|
||||
{
|
||||
*output_pointer++ = '\t';
|
||||
}
|
||||
@ -1696,7 +1697,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
update_offset(output_buffer);
|
||||
|
||||
/* print comma if not last */
|
||||
length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0));
|
||||
length = (size_t) ((output_buffer->configuration.format ? 1 : 0) + (current_item->next ? 1 : 0));
|
||||
output_pointer = ensure(output_buffer, length + 1);
|
||||
if (output_pointer == NULL)
|
||||
{
|
||||
@ -1707,7 +1708,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
*output_pointer++ = ',';
|
||||
}
|
||||
|
||||
if (output_buffer->format)
|
||||
if (output_buffer->configuration.format)
|
||||
{
|
||||
*output_pointer++ = '\n';
|
||||
}
|
||||
@ -1717,12 +1718,12 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
||||
current_item = current_item->next;
|
||||
}
|
||||
|
||||
output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
|
||||
output_pointer = ensure(output_buffer, output_buffer->configuration.format ? (output_buffer->depth + 1) : 2);
|
||||
if (output_pointer == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (output_buffer->format)
|
||||
if (output_buffer->configuration.format)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < (output_buffer->depth - 1); i++)
|
||||
|
2
cJSON.h
2
cJSON.h
@ -147,7 +147,7 @@ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
|
@ -418,7 +418,7 @@ static void *failing_realloc(void *pointer, size_t size)
|
||||
|
||||
static void ensure_should_fail_on_failed_realloc(void)
|
||||
{
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}};
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, {false, &malloc, &free, &failing_realloc}};
|
||||
buffer.buffer = (unsigned char*)malloc(100);
|
||||
TEST_ASSERT_NOT_NULL(buffer.buffer);
|
||||
|
||||
@ -428,7 +428,7 @@ static void ensure_should_fail_on_failed_realloc(void)
|
||||
static void skip_utf8_bom_should_skip_bom(void)
|
||||
{
|
||||
const unsigned char string[] = "\xEF\xBB\xBF{}";
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = string;
|
||||
buffer.length = sizeof(string);
|
||||
buffer.configuration = global_configuration;
|
||||
@ -440,7 +440,7 @@ static void skip_utf8_bom_should_skip_bom(void)
|
||||
static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
|
||||
{
|
||||
const unsigned char string[] = " \xEF\xBB\xBF{}";
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = string;
|
||||
buffer.length = sizeof(string);
|
||||
buffer.configuration = global_configuration;
|
||||
|
@ -44,7 +44,7 @@ static void assert_is_array(cJSON *array_item)
|
||||
|
||||
static void assert_not_array(const char *json)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*)json;
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.configuration = global_configuration;
|
||||
@ -55,7 +55,7 @@ static void assert_not_array(const char *json)
|
||||
|
||||
static void assert_parse_array(const char *json)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*)json;
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.configuration = global_configuration;
|
||||
|
@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item)
|
||||
|
||||
static void assert_parse_number(const char *string, int integer, double real)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
|
||||
|
@ -52,7 +52,7 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
|
||||
|
||||
static void assert_not_object(const char *json)
|
||||
{
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
parsebuffer.content = (const unsigned char*)json;
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.configuration = global_configuration;
|
||||
@ -64,7 +64,7 @@ static void assert_not_object(const char *json)
|
||||
|
||||
static void assert_parse_object(const char *json)
|
||||
{
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
parsebuffer.content = (const unsigned char*)json;
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.configuration = global_configuration;
|
||||
|
@ -45,7 +45,7 @@ static void assert_is_string(cJSON *string_item)
|
||||
|
||||
static void assert_parse_string(const char *string, const char *expected)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.configuration = global_configuration;
|
||||
@ -59,7 +59,7 @@ static void assert_parse_string(const char *string, const char *expected)
|
||||
|
||||
static void assert_not_parse_string(const char * const string)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*)string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.configuration = global_configuration;
|
||||
|
@ -43,7 +43,7 @@ static void assert_is_value(cJSON *value_item, int type)
|
||||
|
||||
static void assert_parse_value(const char *string, int type)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.content = (const unsigned char*) string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.configuration = global_configuration;
|
||||
|
@ -31,10 +31,10 @@ static void assert_print_array(const char * const expected, const char * const i
|
||||
|
||||
cJSON item[1];
|
||||
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
parsebuffer.length = strlen(input) + sizeof("");
|
||||
parsebuffer.configuration = global_configuration;
|
||||
@ -56,11 +56,11 @@ static void assert_print_array(const char * const expected, const char * const i
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array.");
|
||||
|
||||
unformatted_buffer.format = false;
|
||||
unformatted_buffer.configuration.format = false;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct.");
|
||||
|
||||
formatted_buffer.format = true;
|
||||
formatted_buffer.configuration.format = true;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct.");
|
||||
|
||||
|
@ -28,7 +28,7 @@ static void assert_print_number(const char *expected, double input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
cJSON item[1];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
|
@ -31,9 +31,9 @@ static void assert_print_object(const char * const expected, const char * const
|
||||
|
||||
cJSON item[1];
|
||||
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
|
||||
/* buffer for parsing */
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
@ -57,11 +57,11 @@ static void assert_print_object(const char * const expected, const char * const
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object.");
|
||||
|
||||
unformatted_buffer.format = false;
|
||||
unformatted_buffer.configuration.format = false;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct.");
|
||||
|
||||
formatted_buffer.format = true;
|
||||
formatted_buffer.configuration.format = true;
|
||||
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string.");
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct.");
|
||||
|
||||
|
@ -27,7 +27,7 @@
|
||||
static void assert_print_string(const char *expected, const char *input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
|
@ -32,13 +32,14 @@ static void assert_print_value(const char *input)
|
||||
{
|
||||
unsigned char printed[1024];
|
||||
cJSON item[1];
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
||||
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
|
||||
buffer.buffer = printed;
|
||||
buffer.length = sizeof(printed);
|
||||
buffer.offset = 0;
|
||||
buffer.noalloc = true;
|
||||
buffer.configuration = global_configuration;
|
||||
buffer.configuration.format = false;
|
||||
|
||||
parsebuffer.content = (const unsigned char*)input;
|
||||
parsebuffer.length = strlen(input) + sizeof("");
|
||||
|
Loading…
Reference in New Issue
Block a user