diff --git a/parson.c b/parson.c index 4727324..fc7621f 100644 --- a/parson.c +++ b/parson.c @@ -130,8 +130,9 @@ static int append_string(char *buf, const char *string); /* Various */ static char * parson_strndup(const char *string, size_t n) { char *output_string = (char*)parson_malloc(n + 1); - if (!output_string) + if (!output_string) { return NULL; + } output_string[n] = '\0'; strncpy(output_string, string, n); return output_string; @@ -215,13 +216,16 @@ static int is_valid_utf8(const char *string, size_t string_len) { } static int is_decimal(const char *string, size_t length) { - if (length > 1 && string[0] == '0' && string[1] != '.') + if (length > 1 && string[0] == '0' && string[1] != '.') { return 0; - if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') + } + if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { return 0; + } while (length--) - if (strchr("xX", string[length])) + if (strchr("xX", string[length])) { return 0; + } return 1; } @@ -230,8 +234,9 @@ static char * read_file(const char * filename) { size_t file_size; long pos; char *file_contents; - if (!fp) + if (!fp) { return NULL; + } fseek(fp, 0L, SEEK_END); pos = ftell(fp); if (pos < 0) { @@ -263,8 +268,9 @@ static void remove_comments(char *string, const char *start_token, const char *e char *ptr = NULL, current_char; size_t start_token_len = strlen(start_token); size_t end_token_len = strlen(end_token); - if (start_token_len == 0 || end_token_len == 0) - return; + if (start_token_len == 0 || end_token_len == 0) { + return; + } while ((current_char = *string) != '\0') { if (current_char == '\\' && !escaped) { escaped = 1; @@ -273,15 +279,18 @@ static void remove_comments(char *string, const char *start_token, const char *e } else if (current_char == '\"' && !escaped) { in_string = !in_string; } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { - for(i = 0; i < start_token_len; i++) + for(i = 0; i < start_token_len; i++) { string[i] = ' '; - string = string + start_token_len; + } + string = string + start_token_len; ptr = strstr(string, end_token); - if (!ptr) + if (!ptr) { return; - for (i = 0; i < (ptr - string) + end_token_len; i++) + } + for (i = 0; i < (ptr - string) + end_token_len; i++) { string[i] = ' '; - string = ptr + end_token_len - 1; + } + string = ptr + end_token_len - 1; } escaped = 0; string++; @@ -291,8 +300,9 @@ static void remove_comments(char *string, const char *start_token, const char *e /* JSON Object */ static JSON_Object * json_object_init(void) { JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); - if (!new_obj) + if (!new_obj) { return NULL; + } new_obj->names = (char**)NULL; new_obj->values = (JSON_Value**)NULL; new_obj->capacity = 0; @@ -310,15 +320,18 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V } if (object->count >= object->capacity) { size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); - if (new_capacity > OBJECT_MAX_CAPACITY) + if (new_capacity > OBJECT_MAX_CAPACITY) { return JSONFailure; - if (json_object_resize(object, new_capacity) == JSONFailure) + } + if (json_object_resize(object, new_capacity) == JSONFailure) { return JSONFailure; + } } index = object->count; object->names[index] = parson_strdup(name); - if (object->names[index] == NULL) + if (object->names[index] == NULL) { return JSONFailure; + } object->values[index] = value; object->count++; return JSONSuccess; @@ -335,8 +348,9 @@ static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) } temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); - if (temp_names == NULL) + if (temp_names == NULL) { return JSONFailure; + } temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); if (temp_values == NULL) { @@ -360,10 +374,12 @@ static JSON_Value * json_object_nget_value(const JSON_Object *object, const char size_t i, name_length; for (i = 0; i < json_object_get_count(object); i++) { name_length = strlen(object->names[i]); - if (name_length != n) + if (name_length != n) { continue; - if (strncmp(object->names[i], name, n) == 0) + } + if (strncmp(object->names[i], name, n) == 0) { return object->values[i]; + } } return NULL; } @@ -381,8 +397,9 @@ static void json_object_free(JSON_Object *object) { /* JSON Array */ static JSON_Array * json_array_init(void) { JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); - if (!new_array) + if (!new_array) { return NULL; + } new_array->items = (JSON_Value**)NULL; new_array->capacity = 0; new_array->count = 0; @@ -392,10 +409,12 @@ static JSON_Array * json_array_init(void) { static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { if (array->count >= array->capacity) { size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); - if (new_capacity > ARRAY_MAX_CAPACITY) + if (new_capacity > ARRAY_MAX_CAPACITY) { return JSONFailure; - if (json_array_resize(array, new_capacity) == JSONFailure) + } + if (json_array_resize(array, new_capacity) == JSONFailure) { return JSONFailure; + } } array->items[array->count] = value; array->count++; @@ -430,8 +449,9 @@ static void json_array_free(JSON_Array *array) { /* JSON Value */ static JSON_Value * json_value_init_string_no_copy(char *string) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONString; new_value->value.string = string; return new_value; @@ -463,8 +483,9 @@ static int parse_utf_16(const char **unprocessed, char **processed) { char *processed_ptr = *processed; const char *unprocessed_ptr = *unprocessed; unprocessed_ptr++; /* skips u */ - if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) - return JSONFailure; + if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) { + return JSONFailure; + } if (cp < 0x80) { *processed_ptr = cp; /* 0xxxxxxx */ } else if (cp < 0x800) { @@ -478,7 +499,7 @@ static int parse_utf_16(const char **unprocessed, char **processed) { lead = cp; unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ - !is_utf16_hex((const unsigned char*)unprocessed_ptr) || + !is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &trail) == EOF || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ return JSONFailure; @@ -520,8 +541,9 @@ static char* process_string(const char *input, size_t len) { case 'r': *output_ptr = '\r'; break; case 't': *output_ptr = '\t'; break; case 'u': - if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) + if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) { goto error; + } break; default: goto error; @@ -539,8 +561,9 @@ static char* process_string(const char *input, size_t len) { final_size = (size_t)(output_ptr-output) + 1; /* todo: don't resize if final_size == initial_size */ resized_output = (char*)parson_malloc(final_size); - if (resized_output == NULL) + if (resized_output == NULL) { goto error; + } memcpy(resized_output, output, final_size); parson_free(output); return resized_output; @@ -563,8 +586,9 @@ static char * get_quoted_string(const char **string) { } static JSON_Value * parse_value(const char **string, size_t nesting) { - if (nesting > MAX_NESTING) + if (nesting > MAX_NESTING) { return NULL; + } SKIP_WHITESPACES(string); switch (**string) { case '{': @@ -590,8 +614,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) { JSON_Value *output_value = json_value_init_object(), *new_value = NULL; JSON_Object *output_object = json_value_get_object(output_value); char *new_key = NULL; - if (output_value == NULL || **string != '{') + if (output_value == NULL || **string != '{') { return NULL; + } SKIP_CHAR(string); SKIP_WHITESPACES(string); if (**string == '}') { /* empty object */ @@ -620,8 +645,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) { } parson_free(new_key); SKIP_WHITESPACES(string); - if (**string != ',') + if (**string != ',') { break; + } SKIP_CHAR(string); SKIP_WHITESPACES(string); } @@ -638,8 +664,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) { static JSON_Value * parse_array_value(const char **string, size_t nesting) { JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL; JSON_Array *output_array = json_value_get_array(output_value); - if (!output_value || **string != '[') + if (!output_value || **string != '[') { return NULL; + } SKIP_CHAR(string); SKIP_WHITESPACES(string); if (**string == ']') { /* empty array */ @@ -652,14 +679,15 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) { json_value_free(output_value); return NULL; } - if(json_array_add(output_array, new_array_value) == JSONFailure) { + if (json_array_add(output_array, new_array_value) == JSONFailure) { parson_free(new_array_value); json_value_free(output_value); return NULL; } SKIP_WHITESPACES(string); - if (**string != ',') + if (**string != ',') { break; + } SKIP_CHAR(string); SKIP_WHITESPACES(string); } @@ -676,8 +704,9 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) { static JSON_Value * parse_string_value(const char **string) { JSON_Value *value = NULL; char *new_string = get_quoted_string(string); - if (new_string == NULL) + if (new_string == NULL) { return NULL; + } value = json_value_init_string_no_copy(new_string); if (value == NULL) { parson_free(new_string); @@ -747,89 +776,113 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le array = json_value_get_array(value); count = json_array_get_count(array); APPEND_STRING("["); - if (count > 0 && is_pretty) + if (count > 0 && is_pretty) { APPEND_STRING("\n"); + } for (i = 0; i < count; i++) { - if (is_pretty) + if (is_pretty) { APPEND_INDENT(level+1); + } temp_value = json_array_get_value(array, i); written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); - if (written < 0) + if (written < 0) { return -1; - if (buf != NULL) + } + if (buf != NULL) { buf += written; + } written_total += written; - if (i < (count - 1)) + if (i < (count - 1)) { APPEND_STRING(","); - if (is_pretty) + } + if (is_pretty) { APPEND_STRING("\n"); + } } - if (count > 0 && is_pretty) + if (count > 0 && is_pretty) { APPEND_INDENT(level); + } APPEND_STRING("]"); return written_total; case JSONObject: object = json_value_get_object(value); count = json_object_get_count(object); APPEND_STRING("{"); - if (count > 0 && is_pretty) + if (count > 0 && is_pretty) { APPEND_STRING("\n"); + } for (i = 0; i < count; i++) { key = json_object_get_name(object, i); - if (is_pretty) + if (is_pretty) { APPEND_INDENT(level+1); + } written = json_serialize_string(key, buf); - if (written < 0) + if (written < 0) { return -1; - if (buf != NULL) + } + if (buf != NULL) { buf += written; + } written_total += written; APPEND_STRING(":"); - if (is_pretty) + if (is_pretty) { APPEND_STRING(" "); + } temp_value = json_object_get_value(object, key); written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); - if (written < 0) + if (written < 0) { return -1; - if (buf != NULL) + } + if (buf != NULL) { buf += written; + } written_total += written; - if (i < (count - 1)) + if (i < (count - 1)) { APPEND_STRING(","); - if (is_pretty) + } + if (is_pretty) { APPEND_STRING("\n"); + } } - if (count > 0 && is_pretty) + if (count > 0 && is_pretty) { APPEND_INDENT(level); + } APPEND_STRING("}"); return written_total; case JSONString: string = json_value_get_string(value); written = json_serialize_string(string, buf); - if (written < 0) + if (written < 0) { return -1; - if (buf != NULL) + } + if (buf != NULL) { buf += written; + } written_total += written; return written_total; case JSONBoolean: - if (json_value_get_boolean(value)) + if (json_value_get_boolean(value)) { APPEND_STRING("true"); - else + } else { APPEND_STRING("false"); + } return written_total; case JSONNumber: num = json_value_get_number(value); - if (buf != NULL) + if (buf != NULL) { num_buf = buf; - if (num == ((double)(int)num)) /* check if num is integer */ + } + if (num == ((double)(int)num)) { /* check if num is integer */ written = sprintf(num_buf, "%d", (int)num); - else + } else { written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); - if (written < 0) + } + if (written < 0) { return -1; - if (buf != NULL) + } + if (buf != NULL) { buf += written; + } written_total += written; return written_total; case JSONNull: @@ -894,8 +947,9 @@ static int append_string(char *buf, const char *string) { JSON_Value * json_parse_file(const char *filename) { char *file_contents = read_file(filename); JSON_Value *output_value = NULL; - if (file_contents == NULL) + if (file_contents == NULL) { return NULL; + } output_value = json_parse_string(file_contents); parson_free(file_contents); return output_value; @@ -904,16 +958,18 @@ JSON_Value * json_parse_file(const char *filename) { JSON_Value * json_parse_file_with_comments(const char *filename) { char *file_contents = read_file(filename); JSON_Value *output_value = NULL; - if (file_contents == NULL) + if (file_contents == NULL) { return NULL; + } output_value = json_parse_string_with_comments(file_contents); parson_free(file_contents); return output_value; } JSON_Value * json_parse_string(const char *string) { - if (string == NULL) + if (string == NULL) { return NULL; + } return parse_value((const char**)&string, 0); } @@ -921,8 +977,9 @@ JSON_Value * json_parse_string_with_comments(const char *string) { JSON_Value *result = NULL; char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; string_mutable_copy = parson_strdup(string); - if (string_mutable_copy == NULL) + if (string_mutable_copy == NULL) { return NULL; + } remove_comments(string_mutable_copy, "/*", "*/"); remove_comments(string_mutable_copy, "//", "\n"); string_mutable_copy_ptr = string_mutable_copy; @@ -934,8 +991,9 @@ JSON_Value * json_parse_string_with_comments(const char *string) { /* JSON Object API */ JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { - if (object == NULL || name == NULL) + if (object == NULL || name == NULL) { return NULL; + } return json_object_nget_value(object, name, strlen(name)); } @@ -961,8 +1019,9 @@ int json_object_get_boolean(const JSON_Object *object, const char *name) { JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { const char *dot_position = strchr(name, '.'); - if (!dot_position) + if (!dot_position) { return json_object_get_value(object, name); + } object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); return json_object_dotget_value(object, dot_position + 1); } @@ -992,14 +1051,16 @@ size_t json_object_get_count(const JSON_Object *object) { } const char * json_object_get_name(const JSON_Object *object, size_t index) { - if (object == NULL || index >= json_object_get_count(object)) + if (object == NULL || index >= json_object_get_count(object)) { return NULL; + } return object->names[index]; } JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) { - if (object == NULL || index >= json_object_get_count(object)) + if (object == NULL || index >= json_object_get_count(object)) { return NULL; + } return object->values[index]; } @@ -1023,8 +1084,9 @@ int json_object_dothas_value_of_type(const JSON_Object *object, const char *name /* JSON Array API */ JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { - if (array == NULL || index >= json_array_get_count(array)) + if (array == NULL || index >= json_array_get_count(array)) { return NULL; + } return array->items[index]; } @@ -1083,7 +1145,9 @@ void json_value_free(JSON_Value *value) { json_object_free(value->value.object); break; case JSONString: - if (value->value.string) { parson_free(value->value.string); } + if (value->value.string) { + parson_free(value->value.string); + } break; case JSONArray: json_array_free(value->value.array); @@ -1096,8 +1160,9 @@ void json_value_free(JSON_Value *value) { JSON_Value * json_value_init_object(void) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONObject; new_value->value.object = json_object_init(); if (!new_value->value.object) { @@ -1109,8 +1174,9 @@ JSON_Value * json_value_init_object(void) { JSON_Value * json_value_init_array(void) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONArray; new_value->value.array = json_array_init(); if (!new_value->value.array) { @@ -1124,24 +1190,29 @@ JSON_Value * json_value_init_string(const char *string) { char *copy = NULL; JSON_Value *value; size_t string_len = 0; - if (string == NULL) + if (string == NULL) { return NULL; + } string_len = strlen(string); - if (!is_valid_utf8(string, string_len)) + if (!is_valid_utf8(string, string_len)) { return NULL; + } copy = parson_strndup(string, string_len); - if (copy == NULL) + if (copy == NULL) { return NULL; + } value = json_value_init_string_no_copy(copy); - if (value == NULL) + if (value == NULL) { parson_free(copy); + } return value; } JSON_Value * json_value_init_number(double number) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONNumber; new_value->value.number = number; return new_value; @@ -1149,8 +1220,9 @@ JSON_Value * json_value_init_number(double number) { JSON_Value * json_value_init_boolean(int boolean) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONBoolean; new_value->value.boolean = boolean ? 1 : 0; return new_value; @@ -1158,8 +1230,9 @@ JSON_Value * json_value_init_boolean(int boolean) { JSON_Value * json_value_init_null(void) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) + if (!new_value) { return NULL; + } new_value->type = JSONNull; return new_value; } @@ -1176,8 +1249,9 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) { case JSONArray: temp_array = json_value_get_array(value); return_value = json_value_init_array(); - if (return_value == NULL) + if (return_value == NULL) { return NULL; + } temp_array_copy = json_value_get_array(return_value); for (i = 0; i < json_array_get_count(temp_array); i++) { temp_value = json_array_get_value(temp_array, i); @@ -1196,8 +1270,9 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) { case JSONObject: temp_object = json_value_get_object(value); return_value = json_value_init_object(); - if (return_value == NULL) + if (return_value == NULL) { return NULL; + } temp_object_copy = json_value_get_object(return_value); for (i = 0; i < json_object_get_count(temp_object); i++) { temp_key = json_object_get_name(temp_object, i); @@ -1221,11 +1296,13 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) { case JSONString: temp_string = json_value_get_string(value); temp_string_copy = parson_strdup(temp_string); - if (temp_string_copy == NULL) + if (temp_string_copy == NULL) { return NULL; + } return_value = json_value_init_string_no_copy(temp_string_copy); - if (return_value == NULL) + if (return_value == NULL) { parson_free(temp_string_copy); + } return return_value; case JSONNull: return json_value_init_null(); @@ -1285,8 +1362,9 @@ char * json_serialize_to_string(const JSON_Value *value) { return NULL; } buf = (char*)parson_malloc(buf_size_bytes); - if (buf == NULL) + if (buf == NULL) { return NULL; + } serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); if (serialization_result == JSONFailure) { json_free_serialized_string(buf); @@ -1304,11 +1382,13 @@ size_t json_serialization_size_pretty(const JSON_Value *value) { JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { int written = -1; size_t needed_size_in_bytes = json_serialization_size_pretty(value); - if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { return JSONFailure; + } written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); - if (written < 0) + if (written < 0) { return JSONFailure; + } return JSONSuccess; } @@ -1342,8 +1422,9 @@ char * json_serialize_to_string_pretty(const JSON_Value *value) { return NULL; } buf = (char*)parson_malloc(buf_size_bytes); - if (buf == NULL) + if (buf == NULL) { return NULL; + } serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); if (serialization_result == JSONFailure) { json_free_serialized_string(buf); @@ -1386,8 +1467,9 @@ JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *v JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { JSON_Value *value = json_value_init_string(string); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_replace_value(array, i, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1397,8 +1479,9 @@ JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* s JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { JSON_Value *value = json_value_init_number(number); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_replace_value(array, i, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1408,8 +1491,9 @@ JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_replace_value(array, i, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1419,8 +1503,9 @@ JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { JSON_Value *value = json_value_init_null(); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_replace_value(array, i, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1430,8 +1515,9 @@ JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { JSON_Status json_array_clear(JSON_Array *array) { size_t i = 0; - if (array == NULL) + if (array == NULL) { return JSONFailure; + } for (i = 0; i < json_array_get_count(array); i++) { json_value_free(json_array_get_value(array, i)); } @@ -1440,15 +1526,17 @@ JSON_Status json_array_clear(JSON_Array *array) { } JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { - if (array == NULL || value == NULL) + if (array == NULL || value == NULL) { return JSONFailure; + } return json_array_add(array, value); } JSON_Status json_array_append_string(JSON_Array *array, const char *string) { JSON_Value *value = json_value_init_string(string); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_append_value(array, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1458,8 +1546,9 @@ JSON_Status json_array_append_string(JSON_Array *array, const char *string) { JSON_Status json_array_append_number(JSON_Array *array, double number) { JSON_Value *value = json_value_init_number(number); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_append_value(array, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1469,8 +1558,9 @@ JSON_Status json_array_append_number(JSON_Array *array, double number) { JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_append_value(array, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1480,8 +1570,9 @@ JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { JSON_Status json_array_append_null(JSON_Array *array) { JSON_Value *value = json_value_init_null(); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_array_append_value(array, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1492,8 +1583,9 @@ JSON_Status json_array_append_null(JSON_Array *array) { JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { size_t i = 0; JSON_Value *old_value; - if (object == NULL || name == NULL || value == NULL) + if (object == NULL || name == NULL || value == NULL) { return JSONFailure; + } old_value = json_object_get_value(object, name); if (old_value != NULL) { /* free and overwrite old value */ json_value_free(old_value); @@ -1529,8 +1621,9 @@ JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON char *current_name = NULL; JSON_Object *temp_obj = NULL; JSON_Value *new_value = NULL; - if (value == NULL || name == NULL || value == NULL) + if (value == NULL || name == NULL || value == NULL) { return JSONFailure; + } dot_pos = strchr(name, '.'); if (dot_pos == NULL) { return json_object_set_value(object, name, value); @@ -1557,8 +1650,9 @@ JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { JSON_Value *value = json_value_init_string(string); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_object_dotset_value(object, name, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1568,8 +1662,9 @@ JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, con JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { JSON_Value *value = json_value_init_number(number); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_object_dotset_value(object, name, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1579,8 +1674,9 @@ JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, dou JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { JSON_Value *value = json_value_init_boolean(boolean); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_object_dotset_value(object, name, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1590,8 +1686,9 @@ JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, in JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { JSON_Value *value = json_value_init_null(); - if (value == NULL) + if (value == NULL) { return JSONFailure; + } if (json_object_dotset_value(object, name, value) == JSONFailure) { json_value_free(value); return JSONFailure; @@ -1601,8 +1698,9 @@ JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { JSON_Status json_object_remove(JSON_Object *object, const char *name) { size_t i = 0, last_item_index = 0; - if (object == NULL || json_object_get_value(object, name) == NULL) + if (object == NULL || json_object_get_value(object, name) == NULL) { return JSONFailure; + } last_item_index = json_object_get_count(object) - 1; for (i = 0; i < json_object_get_count(object); i++) { if (strcmp(object->names[i], name) == 0) { @@ -1657,19 +1755,22 @@ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { JSON_Value_Type schema_type = JSONError, value_type = JSONError; const char *key = NULL; size_t i = 0, count = 0; - if (schema == NULL || value == NULL) + if (schema == NULL || value == NULL) { return JSONFailure; + } schema_type = json_value_get_type(schema); value_type = json_value_get_type(value); - if (schema_type != value_type && schema_type != JSONNull) /* null represents all values */ + if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */ return JSONFailure; + } switch (schema_type) { case JSONArray: schema_array = json_value_get_array(schema); value_array = json_value_get_array(value); count = json_array_get_count(schema_array); - if (count == 0) + if (count == 0) { return JSONSuccess; /* Empty array allows all types */ + } /* Get first value from array, rest is ignored */ temp_schema_value = json_array_get_value(schema_array, 0); for (i = 0; i < json_array_get_count(value_array); i++) { @@ -1683,18 +1784,21 @@ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { schema_object = json_value_get_object(schema); value_object = json_value_get_object(value); count = json_object_get_count(schema_object); - if (count == 0) + if (count == 0) { return JSONSuccess; /* Empty object allows all objects */ - else if (json_object_get_count(value_object) < count) + } else if (json_object_get_count(value_object) < count) { return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ + } for (i = 0; i < count; i++) { key = json_object_get_name(schema_object, i); temp_schema_value = json_object_get_value(schema_object, key); temp_value = json_object_get_value(value_object, key); - if (temp_value == NULL) + if (temp_value == NULL) { return JSONFailure; - if (json_validate(temp_schema_value, temp_value) == JSONFailure) + } + if (json_validate(temp_schema_value, temp_value) == JSONFailure) { return JSONFailure; + } } return JSONSuccess; case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: