From d441fa05b3f012d5d9c1af1ff6886154aaf6eef8 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:05:11 +0100 Subject: [PATCH 1/6] print_value: return boolean instead of pointer --- cJSON.c | 21 +++++++++++++-------- tests/print_value.c | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cJSON.c b/cJSON.c index c780b77..675ce2f 100644 --- a/cJSON.c +++ b/cJSON.c @@ -813,7 +813,7 @@ static unsigned char *print_string(const cJSON * const item, printbuffer * const /* Predeclare these prototypes. */ static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const ep, const internal_hooks * const hooks); -static unsigned char *print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); +static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); static unsigned char *print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); @@ -893,7 +893,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i } /* print the value */ - if (print_value(item, 0, format, buffer, hooks) == NULL) + if (!print_value(item, 0, format, buffer, hooks)) { goto fail; } @@ -957,7 +957,12 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON p.offset = 0; p.noalloc = false; - return (char*)print_value(item, 0, fmt, &p, &global_hooks); + if (!print_value(item, 0, fmt, &p, &global_hooks)) + { + return NULL; + } + + return (char*)p.buffer; } CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt) @@ -973,7 +978,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i p.length = (size_t)len; p.offset = 0; p.noalloc = true; - return print_value(item, 0, fmt, &p, &global_hooks) != NULL; + return print_value(item, 0, fmt, &p, &global_hooks); } /* Parser core - when encountering text, process appropriately. */ @@ -1031,13 +1036,13 @@ static const unsigned char *parse_value(cJSON * const item, const unsigned char } /* Render a value to text. */ -static unsigned char *print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) { unsigned char *output = NULL; if ((item == NULL) || (output_buffer == NULL)) { - return NULL; + return false; } switch ((item->type) & 0xFF) @@ -1101,7 +1106,7 @@ static unsigned char *print_value(const cJSON * const item, const size_t depth, break; } - return output; + return output != NULL; } /* Build an array from input text. */ @@ -1210,7 +1215,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth, while (current_element != NULL) { - if (print_value(current_element, depth + 1, format, output_buffer, hooks) == NULL) + if (!print_value(current_element, depth + 1, format, output_buffer, hooks)) { return NULL; } diff --git a/tests/print_value.c b/tests/print_value.c index 41ee286..7e0f52f 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -43,7 +43,7 @@ static void assert_print_value(const char *input) TEST_ASSERT_NOT_NULL_MESSAGE(parse_value(item, (const unsigned char*)input, &error_pointer, &global_hooks), "Failed to parse value."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_value(item, 0, false, &buffer, &global_hooks), "Failed to print value."); + TEST_ASSERT_TRUE_MESSAGE(print_value(item, 0, false, &buffer, &global_hooks), "Failed to print value."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected."); reset(item); From bea1d102fd145c5c1401e5c04675760466186166 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:16:54 +0100 Subject: [PATCH 2/6] print_array: return boolean instead of pointer --- cJSON.c | 23 +++++++++-------------- tests/print_array.c | 4 ++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/cJSON.c b/cJSON.c index 675ce2f..69f5b31 100644 --- a/cJSON.c +++ b/cJSON.c @@ -815,7 +815,7 @@ static unsigned char *print_string(const cJSON * const item, printbuffer * const static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const ep, const internal_hooks * const hooks); static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); -static unsigned char *print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); +static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); static unsigned char *print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); @@ -1096,8 +1096,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons output = print_string(item, output_buffer, hooks); break; case cJSON_Array: - output = print_array(item, depth, format, output_buffer, hooks); - break; + return print_array(item, depth, format, output_buffer, hooks); case cJSON_Object: output = print_object(item, depth, format, output_buffer, hooks); break; @@ -1188,26 +1187,23 @@ fail: } /* Render an array to text */ -static unsigned char *print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) { - unsigned char *output = NULL; unsigned char *output_pointer = NULL; size_t length = 0; cJSON *current_element = item->child; - size_t output_offset = 0; if (output_buffer == NULL) { - return NULL; + return false; } /* Compose the output array. */ /* opening square bracket */ - output_offset = output_buffer->offset; output_pointer = ensure(output_buffer, 1, hooks); if (output_pointer == NULL) { - return NULL; + return false; } *output_pointer = '['; @@ -1217,7 +1213,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth, { if (!print_value(current_element, depth + 1, format, output_buffer, hooks)) { - return NULL; + return false; } update_offset(output_buffer); if (current_element->next) @@ -1226,7 +1222,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, length + 1, hooks); if (output_pointer == NULL) { - return NULL; + return false; } *output_pointer++ = ','; if(format) @@ -1242,13 +1238,12 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, 2, hooks); if (output_pointer == NULL) { - return NULL; + return false; } *output_pointer++ = ']'; *output_pointer = '\0'; - output = output_buffer->buffer + output_offset; - return output; + return true; } /* Build an object from the text. */ diff --git a/tests/print_array.c b/tests/print_array.c index e121c65..df32be6 100644 --- a/tests/print_array.c +++ b/tests/print_array.c @@ -50,10 +50,10 @@ static void assert_print_array(const char * const expected, const char * const i memset(item, 0, sizeof(item)); TEST_ASSERT_NOT_NULL_MESSAGE(parse_array(item, (const unsigned char*)input, &error_pointer, &global_hooks), "Failed to parse array."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_array(item, 0, false, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_array(item, 0, false, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_array(item, 0, true, &formatted_buffer, &global_hooks), "Failed to print formatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_array(item, 0, true, &formatted_buffer, &global_hooks), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct."); reset(item); From 748f4bfd4f7b58729201ec5606234780f082e717 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:21:34 +0100 Subject: [PATCH 3/6] print_object: return boolean instead of pointer --- cJSON.c | 29 ++++++++++++----------------- tests/print_object.c | 4 ++-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/cJSON.c b/cJSON.c index 69f5b31..d72b62b 100644 --- a/cJSON.c +++ b/cJSON.c @@ -817,7 +817,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); -static unsigned char *print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); +static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); /* Utility to jump whitespace and cr/lf */ static const unsigned char *skip_whitespace(const unsigned char *in) @@ -1098,8 +1098,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons case cJSON_Array: return print_array(item, depth, format, output_buffer, hooks); case cJSON_Object: - output = print_object(item, depth, format, output_buffer, hooks); - break; + return print_object(item, depth, format, output_buffer, hooks); default: output = NULL; break; @@ -1342,26 +1341,23 @@ fail: } /* Render an object to text. */ -static unsigned char *print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) { - unsigned char *output = NULL; unsigned char *output_pointer = NULL; size_t length = 0; - size_t output_offset = 0; cJSON *current_item = item->child; if (output_buffer == NULL) { - return NULL; + return false; } /* Compose the output: */ - output_offset = output_buffer->offset; length = format ? 2 : 1; /* fmt: {\n */ output_pointer = ensure(output_buffer, length + 1, hooks); if (output_pointer == NULL) { - return NULL; + return false; } *output_pointer++ = '{'; @@ -1379,7 +1375,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, depth + 1, hooks); if (output_pointer == NULL) { - return NULL; + return false; } for (i = 0; i < depth + 1; i++) { @@ -1391,7 +1387,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, /* print key */ if (print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks) == NULL) { - return NULL; + return false; } update_offset(output_buffer); @@ -1399,7 +1395,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, length, hooks); if (output_pointer == NULL) { - return NULL; + return false; } *output_pointer++ = ':'; if (format) @@ -1411,7 +1407,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, /* print value */ if (!print_value(current_item, depth + 1, format, output_buffer, hooks)) { - return NULL; + return false; } update_offset(output_buffer); @@ -1420,7 +1416,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, length + 1, hooks); if (output_pointer == NULL) { - return NULL; + return false; } if (current_item->next) { @@ -1440,7 +1436,7 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, output_pointer = ensure(output_buffer, format ? (depth + 2) : 2, hooks); if (output_pointer == NULL) { - return NULL; + return false; } if (format) { @@ -1452,9 +1448,8 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth, } *output_pointer++ = '}'; *output_pointer = '\0'; - output = (output_buffer->buffer) + output_offset; - return output; + return true; } /* Get Array size/item / object item. */ diff --git a/tests/print_object.c b/tests/print_object.c index b6dad6e..f7f5344 100644 --- a/tests/print_object.c +++ b/tests/print_object.c @@ -50,10 +50,10 @@ static void assert_print_object(const char * const expected, const char * const memset(item, 0, sizeof(item)); TEST_ASSERT_NOT_NULL_MESSAGE(parse_object(item, (const unsigned char*)input, &error_pointer, &global_hooks), "Failed to parse object."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_object(item, 0, false, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_object(item, 0, false, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_object(item, 0, true, &formatted_buffer, &global_hooks), "Failed to print formatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_object(item, 0, true, &formatted_buffer, &global_hooks), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct."); reset(item); From 1749de02f8e115482ebdd60cf8fc1709a8b16c00 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:26:58 +0100 Subject: [PATCH 4/6] print_number: return boolean instead of pointer --- cJSON.c | 17 ++++++++--------- tests/print_number.c | 7 +++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/cJSON.c b/cJSON.c index d72b62b..2f8ab5f 100644 --- a/cJSON.c +++ b/cJSON.c @@ -327,14 +327,14 @@ static void update_offset(printbuffer * const buffer) } /* Removes trailing zeroes from the end of a printed number */ -static unsigned char *trim_trailing_zeroes(printbuffer * const buffer) +static cJSON_bool trim_trailing_zeroes(printbuffer * const buffer) { size_t offset = 0; unsigned char *content = NULL; if ((buffer == NULL) || (buffer->buffer == NULL) || (buffer->offset < 1)) { - return NULL; + return false; } offset = buffer->offset - 1; @@ -354,11 +354,11 @@ static unsigned char *trim_trailing_zeroes(printbuffer * const buffer) buffer->offset = offset; - return content + offset; + return true; } /* Render the number nicely from the given item into a string. */ -static unsigned char *print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) { unsigned char *output_pointer = NULL; double d = item->valuedouble; @@ -367,7 +367,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const if (output_buffer == NULL) { - return NULL; + return false; } /* This is a nice tradeoff. */ @@ -399,7 +399,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const /* sprintf failed */ if (length < 0) { - return NULL; + return false; } output_buffer->offset += (size_t)length; @@ -409,7 +409,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const return trim_trailing_zeroes(output_buffer); } - return output_buffer->buffer; + return true; } /* parse 4 digit hexadecimal number */ @@ -1069,8 +1069,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons } break; case cJSON_Number: - output = print_number(item, output_buffer, hooks); - break; + return print_number(item, output_buffer, hooks); case cJSON_Raw: { size_t raw_length = 0; diff --git a/tests/print_number.c b/tests/print_number.c index ad25b74..b478049 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -37,7 +37,7 @@ static void assert_print_number(const char *expected, double input) memset(item, 0, sizeof(item)); cJSON_SetNumberValue(item, input); - TEST_ASSERT_NOT_NULL_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number."); + TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected."); } @@ -91,14 +91,13 @@ static void trim_trailing_zeroes_should_trim_trailing_zeroes(void) { printbuffer buffer; unsigned char number[100]; - unsigned char *pointer = NULL; buffer.length = sizeof(number); buffer.buffer = number; strcpy((char*)number, "10.00"); buffer.offset = sizeof("10.00") - 1; - pointer = trim_trailing_zeroes(&buffer); - TEST_ASSERT_EQUAL_UINT8('\0', *pointer); + TEST_ASSERT_TRUE(trim_trailing_zeroes(&buffer)); + TEST_ASSERT_EQUAL_UINT8('\0', buffer.buffer[buffer.offset]); TEST_ASSERT_EQUAL_STRING("10", number); TEST_ASSERT_EQUAL_UINT(sizeof("10") - 1, buffer.offset); } From 5ea4fad263a696046336a6c667df8c3aa37d0029 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:32:53 +0100 Subject: [PATCH 5/6] print_string: return boolean instead of pointer --- cJSON.c | 20 ++++++++++---------- tests/print_string.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cJSON.c b/cJSON.c index 2f8ab5f..8cf8fcc 100644 --- a/cJSON.c +++ b/cJSON.c @@ -694,7 +694,7 @@ fail: } /* Render the cstring provided to an escaped version that can be printed. */ -static unsigned char *print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL; @@ -705,7 +705,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb if (output_buffer == NULL) { - return NULL; + return false; } /* empty string */ @@ -714,11 +714,11 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb output = ensure(output_buffer, sizeof("\"\""), hooks); if (output == NULL) { - return NULL; + return false; } strcpy((char*)output, "\"\""); - return output; + return true; } /* set "flag" to 1 if something needs to be escaped */ @@ -740,7 +740,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb output = ensure(output_buffer, output_length + sizeof("\"\""), hooks); if (output == NULL) { - return NULL; + return false; } /* no characters have to be escaped */ @@ -751,7 +751,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb output[output_length + 1] = '\"'; output[output_length + 2] = '\0'; - return output; + return true; } output[0] = '\"'; @@ -802,11 +802,11 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb output[output_length + 1] = '\"'; output[output_length + 2] = '\0'; - return output; + return true; } /* Invoke print_string_ptr (which is useful) on an item. */ -static unsigned char *print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks) +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks) { return print_string_ptr((unsigned char*)item->valuestring, p, hooks); } @@ -1092,7 +1092,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons break; } case cJSON_String: - output = print_string(item, output_buffer, hooks); + return print_string(item, output_buffer, hooks); break; case cJSON_Array: return print_array(item, depth, format, output_buffer, hooks); @@ -1384,7 +1384,7 @@ static cJSON_bool print_object(const cJSON * const item, const size_t depth, con } /* print key */ - if (print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks) == NULL) + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks)) { return false; } diff --git a/tests/print_string.c b/tests/print_string.c index 9521434..2e38eaa 100644 --- a/tests/print_string.c +++ b/tests/print_string.c @@ -33,7 +33,7 @@ static void assert_print_string(const char *expected, const char *input) buffer.offset = 0; buffer.noalloc = true; - TEST_ASSERT_NOT_NULL_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string."); + TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected."); } From 0bb1843925172e70a3dab0af9ec2bf2ce62f3a54 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:40:02 +0100 Subject: [PATCH 6/6] print_value: return as soon as possible --- cJSON.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/cJSON.c b/cJSON.c index 8cf8fcc..7b411fc 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1049,27 +1049,34 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons { case cJSON_NULL: output = ensure(output_buffer, 5, hooks); - if (output != NULL) + if (output == NULL) { - strcpy((char*)output, "null"); + return false; } - break; + strcpy((char*)output, "null"); + return true; + case cJSON_False: output = ensure(output_buffer, 6, hooks); - if (output != NULL) + if (output == NULL) { - strcpy((char*)output, "false"); + return false; } - break; + strcpy((char*)output, "false"); + return true; + case cJSON_True: output = ensure(output_buffer, 5, hooks); - if (output != NULL) + if (output == NULL) { - strcpy((char*)output, "true"); + return false; } - break; + strcpy((char*)output, "true"); + return true; + case cJSON_Number: return print_number(item, output_buffer, hooks); + case cJSON_Raw: { size_t raw_length = 0; @@ -1079,31 +1086,31 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons { hooks->deallocate(output_buffer->buffer); } - output = NULL; - break; + return false; } raw_length = strlen(item->valuestring) + sizeof('\0'); output = ensure(output_buffer, raw_length, hooks); - if (output != NULL) + if (output == NULL) { - memcpy(output, item->valuestring, raw_length); + return false; } - break; + memcpy(output, item->valuestring, raw_length); + return true; } + case cJSON_String: return print_string(item, output_buffer, hooks); - break; + case cJSON_Array: return print_array(item, depth, format, output_buffer, hooks); + case cJSON_Object: return print_object(item, depth, format, output_buffer, hooks); - default: - output = NULL; - break; - } - return output != NULL; + default: + return false; + } } /* Build an array from input text. */