mirror of
https://github.com/DaveGamble/cJSON
synced 2025-02-26 08:40:44 +00:00
parse_functions: Return booleans instead of pointers
This commit is contained in:
parent
c9739c59fd
commit
87a204ed0b
67
cJSON.c
67
cJSON.c
@ -202,7 +202,7 @@ typedef struct
|
||||
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
|
||||
|
||||
/* Parse the input text to generate a number, and populate the result into item. */
|
||||
static const unsigned char *parse_number(cJSON * const item, parse_buffer * const input_buffer)
|
||||
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
|
||||
{
|
||||
double number = 0;
|
||||
unsigned char *after_end = NULL;
|
||||
@ -212,7 +212,7 @@ static const unsigned char *parse_number(cJSON * const item, parse_buffer * cons
|
||||
|
||||
if ((input_buffer == NULL) || (input_buffer->content == NULL))
|
||||
{
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* copy the number into a temporary buffer and replace '.' with the decimal point
|
||||
@ -253,7 +253,7 @@ loop_end:
|
||||
number = strtod((const char*)number_c_string, (char**)&after_end);
|
||||
if (number_c_string == after_end)
|
||||
{
|
||||
return NULL; /* parse_error */
|
||||
return false; /* parse_error */
|
||||
}
|
||||
|
||||
item->valuedouble = number;
|
||||
@ -275,7 +275,7 @@ loop_end:
|
||||
item->type = cJSON_Number;
|
||||
|
||||
input_buffer->offset += (size_t)(after_end - number_c_string);
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
|
||||
@ -662,7 +662,7 @@ fail:
|
||||
}
|
||||
|
||||
/* Parse the input text into an unescaped cinput, and populate item. */
|
||||
static const unsigned char *parse_string(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
{
|
||||
const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
|
||||
const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
|
||||
@ -776,7 +776,7 @@ static const unsigned char *parse_string(cJSON * const item, parse_buffer * cons
|
||||
input_buffer->offset = (size_t) (input_end - input_buffer->content);
|
||||
input_buffer->offset++;
|
||||
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if (output != NULL)
|
||||
@ -784,7 +784,7 @@ fail:
|
||||
hooks->deallocate(output);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Render the cstring provided to an escaped version that can be printed. */
|
||||
@ -916,11 +916,11 @@ static cJSON_bool print_string(const cJSON * const item, printbuffer * const p,
|
||||
}
|
||||
|
||||
/* Predeclare these prototypes. */
|
||||
static const unsigned char *parse_value(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const ep, const internal_hooks * const hooks);
|
||||
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, 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, parse_buffer * const input_buffer, const unsigned char ** const ep, const internal_hooks * const hooks);
|
||||
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, 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, parse_buffer * const input_buffer, const unsigned char ** const ep, const internal_hooks * const hooks);
|
||||
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const ep, 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 */
|
||||
@ -980,13 +980,14 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
|
||||
buffer.length = strlen((const char*)value) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
end = parse_value(item, buffer_skip_whitespace(&buffer), error_pointer, &global_hooks);
|
||||
if (end == NULL)
|
||||
if (!parse_value(item, buffer_skip_whitespace(&buffer), error_pointer, &global_hooks))
|
||||
{
|
||||
/* parse failure. ep is set. */
|
||||
goto fail;
|
||||
}
|
||||
|
||||
end = buffer_at_offset(&buffer);
|
||||
|
||||
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
|
||||
if (require_null_terminated)
|
||||
{
|
||||
@ -1125,12 +1126,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i
|
||||
}
|
||||
|
||||
/* Parser core - when encountering text, process appropriately. */
|
||||
static const unsigned char *parse_value(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
{
|
||||
const unsigned char *content_pointer = NULL;
|
||||
if ((input_buffer == NULL) || (input_buffer->content == NULL))
|
||||
{
|
||||
return NULL; /* no input */
|
||||
return false; /* no input */
|
||||
}
|
||||
|
||||
/* parse the different types of values */
|
||||
@ -1139,14 +1139,14 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
|
||||
{
|
||||
item->type = cJSON_NULL;
|
||||
input_buffer->offset += 4;
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
}
|
||||
/* false */
|
||||
if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
|
||||
{
|
||||
item->type = cJSON_False;
|
||||
input_buffer->offset += 5;
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
}
|
||||
/* true */
|
||||
if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
|
||||
@ -1154,12 +1154,12 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
|
||||
item->type = cJSON_True;
|
||||
item->valueint = 1;
|
||||
input_buffer->offset += 4;
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
}
|
||||
/* string */
|
||||
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
|
||||
{
|
||||
return content_pointer = parse_string(item, input_buffer, error_pointer, hooks);
|
||||
return parse_string(item, input_buffer, error_pointer, hooks);
|
||||
}
|
||||
/* number */
|
||||
if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
|
||||
@ -1174,14 +1174,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
|
||||
/* object */
|
||||
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
|
||||
{
|
||||
content_pointer = parse_object(item, input_buffer, error_pointer, hooks);
|
||||
if (content_pointer == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
input_buffer->offset = (size_t)(content_pointer - input_buffer->content);
|
||||
return buffer_at_offset(input_buffer);
|
||||
return parse_object(item, input_buffer, error_pointer, hooks);
|
||||
}
|
||||
|
||||
/* failure. */
|
||||
@ -1198,7 +1191,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
|
||||
*error_pointer = input_buffer->content;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Render a value to text. */
|
||||
@ -1280,7 +1273,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons
|
||||
}
|
||||
|
||||
/* Build an array from input text. */
|
||||
static const unsigned char *parse_array(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
{
|
||||
cJSON *head = NULL; /* head of the linked list */
|
||||
cJSON *current_item = NULL;
|
||||
@ -1337,7 +1330,7 @@ static const unsigned char *parse_array(cJSON * const item, parse_buffer * const
|
||||
/* parse next value */
|
||||
input_buffer->offset++;
|
||||
buffer_skip_whitespace(input_buffer);
|
||||
if (parse_value(current_item, input_buffer, error_pointer, hooks) == NULL)
|
||||
if (!parse_value(current_item, input_buffer, error_pointer, hooks))
|
||||
{
|
||||
goto fail; /* failed to parse value */
|
||||
}
|
||||
@ -1357,7 +1350,7 @@ success:
|
||||
|
||||
input_buffer->offset++;
|
||||
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if (head != NULL)
|
||||
@ -1365,7 +1358,7 @@ fail:
|
||||
cJSON_Delete(head);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Render an array to text */
|
||||
@ -1429,7 +1422,7 @@ static cJSON_bool print_array(const cJSON * const item, const size_t depth, cons
|
||||
}
|
||||
|
||||
/* Build an object from the text. */
|
||||
static const unsigned char *parse_object(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
|
||||
{
|
||||
cJSON *head = NULL; /* linked list head */
|
||||
cJSON *current_item = NULL;
|
||||
@ -1484,7 +1477,7 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
|
||||
/* parse the name of the child */
|
||||
input_buffer->offset++;
|
||||
buffer_skip_whitespace(input_buffer);
|
||||
if (parse_string(current_item, input_buffer, error_pointer, hooks) == NULL)
|
||||
if (!parse_string(current_item, input_buffer, error_pointer, hooks))
|
||||
{
|
||||
goto fail; /* faile to parse name */
|
||||
}
|
||||
@ -1503,7 +1496,7 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
|
||||
/* parse the value */
|
||||
input_buffer->offset++;
|
||||
buffer_skip_whitespace(input_buffer);
|
||||
if (parse_value(current_item, input_buffer, error_pointer, hooks) == NULL)
|
||||
if (!parse_value(current_item, input_buffer, error_pointer, hooks))
|
||||
{
|
||||
goto fail; /* failed to parse value */
|
||||
}
|
||||
@ -1522,7 +1515,7 @@ success:
|
||||
item->child = head;
|
||||
|
||||
input_buffer->offset++;
|
||||
return buffer_at_offset(input_buffer);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if (head != NULL)
|
||||
@ -1530,7 +1523,7 @@ fail:
|
||||
cJSON_Delete(head);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Render an object to text. */
|
||||
|
@ -51,7 +51,7 @@ static void assert_not_array(const char *json)
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NULL(parse_array(item, &buffer, &error_pointer, &global_hooks));
|
||||
TEST_ASSERT_FALSE(parse_array(item, &buffer, &error_pointer, &global_hooks));
|
||||
assert_is_invalid(item);
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ static void assert_parse_array(const char *json)
|
||||
buffer.length = strlen(json) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NOT_NULL(parse_array(item, &buffer, &error_pointer, &global_hooks));
|
||||
TEST_ASSERT_TRUE(parse_array(item, &buffer, &error_pointer, &global_hooks));
|
||||
assert_is_array(item);
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ static void assert_parse_number(const char *string, int integer, double real)
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NOT_NULL(parse_number(item, &buffer));
|
||||
TEST_ASSERT_TRUE(parse_number(item, &buffer));
|
||||
assert_is_number(item);
|
||||
TEST_ASSERT_EQUAL_INT(integer, item->valueint);
|
||||
TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);
|
||||
|
@ -59,7 +59,7 @@ static void assert_not_object(const char *json)
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NULL(parse_object(item, &parsebuffer, &error_pointer, &global_hooks));
|
||||
TEST_ASSERT_FALSE(parse_object(item, &parsebuffer, &error_pointer, &global_hooks));
|
||||
assert_is_invalid(item);
|
||||
reset(item);
|
||||
}
|
||||
@ -71,7 +71,7 @@ static void assert_parse_object(const char *json)
|
||||
parsebuffer.length = strlen(json) + sizeof("");
|
||||
parsebuffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NOT_NULL(parse_object(item, &parsebuffer, &error_pointer, &global_hooks));
|
||||
TEST_ASSERT_TRUE(parse_object(item, &parsebuffer, &error_pointer, &global_hooks));
|
||||
assert_is_object(item);
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ static void assert_parse_string(const char *string, const char *expected)
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(parse_string(item, &buffer, &error_pointer, &global_hooks), "Couldn't parse string.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer, &error_pointer, &global_hooks), "Couldn't parse string.");
|
||||
assert_is_string(item);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
|
||||
global_hooks.deallocate(item->valuestring);
|
||||
@ -66,7 +66,7 @@ static void assert_not_parse_string(const char * const string)
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
|
||||
TEST_ASSERT_NULL_MESSAGE(parse_string(item, &buffer, &error_pointer, &global_hooks), "Malformed string should not be accepted.");
|
||||
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer, &error_pointer, &global_hooks), "Malformed string should not be accepted.");
|
||||
assert_is_invalid(item);
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ static void assert_parse_value(const char *string, int type)
|
||||
buffer.content = (const unsigned char*) string;
|
||||
buffer.length = strlen(string) + sizeof("");
|
||||
buffer.offset = 0;
|
||||
TEST_ASSERT_NOT_NULL(parse_value(item, &buffer, &error_pointer, &global_hooks));
|
||||
TEST_ASSERT_TRUE(parse_value(item, &buffer, &error_pointer, &global_hooks));
|
||||
assert_is_value(item, type);
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,7 @@ static void assert_print_array(const char * const expected, const char * const i
|
||||
unformatted_buffer.noalloc = true;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(parse_array(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse array.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse array.");
|
||||
|
||||
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.");
|
||||
|
@ -54,7 +54,7 @@ static void assert_print_object(const char * const expected, const char * const
|
||||
unformatted_buffer.noalloc = true;
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(parse_object(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse object.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse object.");
|
||||
|
||||
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.");
|
||||
|
@ -46,7 +46,7 @@ static void assert_print_value(const char *input)
|
||||
|
||||
memset(item, 0, sizeof(item));
|
||||
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(parse_value(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse value.");
|
||||
TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer, &error_pointer, &global_hooks), "Failed to parse 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.");
|
||||
|
Loading…
Reference in New Issue
Block a user