Using braces in every if statement + whitespace cleanup.

pull/49/head
Krzysztof Gabis 8 years ago
parent a1c356eaa9
commit c22be794ce
  1. 328
      parson.c

@ -130,8 +130,9 @@ static int append_string(char *buf, const char *string);
/* Various */ /* Various */
static char * parson_strndup(const char *string, size_t n) { static char * parson_strndup(const char *string, size_t n) {
char *output_string = (char*)parson_malloc(n + 1); char *output_string = (char*)parson_malloc(n + 1);
if (!output_string) if (!output_string) {
return NULL; return NULL;
}
output_string[n] = '\0'; output_string[n] = '\0';
strncpy(output_string, string, n); strncpy(output_string, string, n);
return output_string; 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) { 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; return 0;
if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') }
if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') {
return 0; return 0;
}
while (length--) while (length--)
if (strchr("xX", string[length])) if (strchr("xX", string[length])) {
return 0; return 0;
}
return 1; return 1;
} }
@ -230,8 +234,9 @@ static char * read_file(const char * filename) {
size_t file_size; size_t file_size;
long pos; long pos;
char *file_contents; char *file_contents;
if (!fp) if (!fp) {
return NULL; return NULL;
}
fseek(fp, 0L, SEEK_END); fseek(fp, 0L, SEEK_END);
pos = ftell(fp); pos = ftell(fp);
if (pos < 0) { 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; char *ptr = NULL, current_char;
size_t start_token_len = strlen(start_token); size_t start_token_len = strlen(start_token);
size_t end_token_len = strlen(end_token); size_t end_token_len = strlen(end_token);
if (start_token_len == 0 || end_token_len == 0) if (start_token_len == 0 || end_token_len == 0) {
return; return;
}
while ((current_char = *string) != '\0') { while ((current_char = *string) != '\0') {
if (current_char == '\\' && !escaped) { if (current_char == '\\' && !escaped) {
escaped = 1; escaped = 1;
@ -273,15 +279,18 @@ static void remove_comments(char *string, const char *start_token, const char *e
} else if (current_char == '\"' && !escaped) { } else if (current_char == '\"' && !escaped) {
in_string = !in_string; in_string = !in_string;
} else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { } 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[i] = ' ';
string = string + start_token_len; }
string = string + start_token_len;
ptr = strstr(string, end_token); ptr = strstr(string, end_token);
if (!ptr) if (!ptr) {
return; return;
for (i = 0; i < (ptr - string) + end_token_len; i++) }
for (i = 0; i < (ptr - string) + end_token_len; i++) {
string[i] = ' '; string[i] = ' ';
string = ptr + end_token_len - 1; }
string = ptr + end_token_len - 1;
} }
escaped = 0; escaped = 0;
string++; string++;
@ -291,8 +300,9 @@ static void remove_comments(char *string, const char *start_token, const char *e
/* JSON Object */ /* JSON Object */
static JSON_Object * json_object_init(void) { static JSON_Object * json_object_init(void) {
JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
if (!new_obj) if (!new_obj) {
return NULL; return NULL;
}
new_obj->names = (char**)NULL; new_obj->names = (char**)NULL;
new_obj->values = (JSON_Value**)NULL; new_obj->values = (JSON_Value**)NULL;
new_obj->capacity = 0; 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) { if (object->count >= object->capacity) {
size_t new_capacity = MAX(object->capacity * 2, STARTING_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; return JSONFailure;
if (json_object_resize(object, new_capacity) == JSONFailure) }
if (json_object_resize(object, new_capacity) == JSONFailure) {
return JSONFailure; return JSONFailure;
}
} }
index = object->count; index = object->count;
object->names[index] = parson_strdup(name); object->names[index] = parson_strdup(name);
if (object->names[index] == NULL) if (object->names[index] == NULL) {
return JSONFailure; return JSONFailure;
}
object->values[index] = value; object->values[index] = value;
object->count++; object->count++;
return JSONSuccess; 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*)); temp_names = (char**)parson_malloc(new_capacity * sizeof(char*));
if (temp_names == NULL) if (temp_names == NULL) {
return JSONFailure; return JSONFailure;
}
temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
if (temp_values == NULL) { 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; size_t i, name_length;
for (i = 0; i < json_object_get_count(object); i++) { for (i = 0; i < json_object_get_count(object); i++) {
name_length = strlen(object->names[i]); name_length = strlen(object->names[i]);
if (name_length != n) if (name_length != n) {
continue; continue;
if (strncmp(object->names[i], name, n) == 0) }
if (strncmp(object->names[i], name, n) == 0) {
return object->values[i]; return object->values[i];
}
} }
return NULL; return NULL;
} }
@ -381,8 +397,9 @@ static void json_object_free(JSON_Object *object) {
/* JSON Array */ /* JSON Array */
static JSON_Array * json_array_init(void) { static JSON_Array * json_array_init(void) {
JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
if (!new_array) if (!new_array) {
return NULL; return NULL;
}
new_array->items = (JSON_Value**)NULL; new_array->items = (JSON_Value**)NULL;
new_array->capacity = 0; new_array->capacity = 0;
new_array->count = 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) { static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
if (array->count >= array->capacity) { if (array->count >= array->capacity) {
size_t new_capacity = MAX(array->capacity * 2, STARTING_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; return JSONFailure;
if (json_array_resize(array, new_capacity) == JSONFailure) }
if (json_array_resize(array, new_capacity) == JSONFailure) {
return JSONFailure; return JSONFailure;
}
} }
array->items[array->count] = value; array->items[array->count] = value;
array->count++; array->count++;
@ -430,8 +449,9 @@ static void json_array_free(JSON_Array *array) {
/* JSON Value */ /* JSON Value */
static JSON_Value * json_value_init_string_no_copy(char *string) { static JSON_Value * json_value_init_string_no_copy(char *string) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONString; new_value->type = JSONString;
new_value->value.string = string; new_value->value.string = string;
return new_value; return new_value;
@ -463,8 +483,9 @@ static int parse_utf_16(const char **unprocessed, char **processed) {
char *processed_ptr = *processed; char *processed_ptr = *processed;
const char *unprocessed_ptr = *unprocessed; const char *unprocessed_ptr = *unprocessed;
unprocessed_ptr++; /* skips u */ unprocessed_ptr++; /* skips u */
if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) {
return JSONFailure; return JSONFailure;
}
if (cp < 0x80) { if (cp < 0x80) {
*processed_ptr = cp; /* 0xxxxxxx */ *processed_ptr = cp; /* 0xxxxxxx */
} else if (cp < 0x800) { } else if (cp < 0x800) {
@ -478,7 +499,7 @@ static int parse_utf_16(const char **unprocessed, char **processed) {
lead = cp; lead = cp;
unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ 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 || sscanf(unprocessed_ptr, "%4x", &trail) == EOF ||
trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
return JSONFailure; return JSONFailure;
@ -520,8 +541,9 @@ static char* process_string(const char *input, size_t len) {
case 'r': *output_ptr = '\r'; break; case 'r': *output_ptr = '\r'; break;
case 't': *output_ptr = '\t'; break; case 't': *output_ptr = '\t'; break;
case 'u': case 'u':
if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) {
goto error; goto error;
}
break; break;
default: default:
goto error; goto error;
@ -539,8 +561,9 @@ static char* process_string(const char *input, size_t len) {
final_size = (size_t)(output_ptr-output) + 1; final_size = (size_t)(output_ptr-output) + 1;
/* todo: don't resize if final_size == initial_size */ /* todo: don't resize if final_size == initial_size */
resized_output = (char*)parson_malloc(final_size); resized_output = (char*)parson_malloc(final_size);
if (resized_output == NULL) if (resized_output == NULL) {
goto error; goto error;
}
memcpy(resized_output, output, final_size); memcpy(resized_output, output, final_size);
parson_free(output); parson_free(output);
return resized_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) { static JSON_Value * parse_value(const char **string, size_t nesting) {
if (nesting > MAX_NESTING) if (nesting > MAX_NESTING) {
return NULL; return NULL;
}
SKIP_WHITESPACES(string); SKIP_WHITESPACES(string);
switch (**string) { switch (**string) {
case '{': 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_Value *output_value = json_value_init_object(), *new_value = NULL;
JSON_Object *output_object = json_value_get_object(output_value); JSON_Object *output_object = json_value_get_object(output_value);
char *new_key = NULL; char *new_key = NULL;
if (output_value == NULL || **string != '{') if (output_value == NULL || **string != '{') {
return NULL; return NULL;
}
SKIP_CHAR(string); SKIP_CHAR(string);
SKIP_WHITESPACES(string); SKIP_WHITESPACES(string);
if (**string == '}') { /* empty object */ if (**string == '}') { /* empty object */
@ -620,8 +645,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) {
} }
parson_free(new_key); parson_free(new_key);
SKIP_WHITESPACES(string); SKIP_WHITESPACES(string);
if (**string != ',') if (**string != ',') {
break; break;
}
SKIP_CHAR(string); SKIP_CHAR(string);
SKIP_WHITESPACES(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) { 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_Value *output_value = json_value_init_array(), *new_array_value = NULL;
JSON_Array *output_array = json_value_get_array(output_value); JSON_Array *output_array = json_value_get_array(output_value);
if (!output_value || **string != '[') if (!output_value || **string != '[') {
return NULL; return NULL;
}
SKIP_CHAR(string); SKIP_CHAR(string);
SKIP_WHITESPACES(string); SKIP_WHITESPACES(string);
if (**string == ']') { /* empty array */ 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); json_value_free(output_value);
return NULL; 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); parson_free(new_array_value);
json_value_free(output_value); json_value_free(output_value);
return NULL; return NULL;
} }
SKIP_WHITESPACES(string); SKIP_WHITESPACES(string);
if (**string != ',') if (**string != ',') {
break; break;
}
SKIP_CHAR(string); SKIP_CHAR(string);
SKIP_WHITESPACES(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) { static JSON_Value * parse_string_value(const char **string) {
JSON_Value *value = NULL; JSON_Value *value = NULL;
char *new_string = get_quoted_string(string); char *new_string = get_quoted_string(string);
if (new_string == NULL) if (new_string == NULL) {
return NULL; return NULL;
}
value = json_value_init_string_no_copy(new_string); value = json_value_init_string_no_copy(new_string);
if (value == NULL) { if (value == NULL) {
parson_free(new_string); 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); array = json_value_get_array(value);
count = json_array_get_count(array); count = json_array_get_count(array);
APPEND_STRING("["); APPEND_STRING("[");
if (count > 0 && is_pretty) if (count > 0 && is_pretty) {
APPEND_STRING("\n"); APPEND_STRING("\n");
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if (is_pretty) if (is_pretty) {
APPEND_INDENT(level+1); APPEND_INDENT(level+1);
}
temp_value = json_array_get_value(array, i); temp_value = json_array_get_value(array, i);
written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
if (written < 0) if (written < 0) {
return -1; return -1;
if (buf != NULL) }
if (buf != NULL) {
buf += written; buf += written;
}
written_total += written; written_total += written;
if (i < (count - 1)) if (i < (count - 1)) {
APPEND_STRING(","); APPEND_STRING(",");
if (is_pretty) }
if (is_pretty) {
APPEND_STRING("\n"); APPEND_STRING("\n");
}
} }
if (count > 0 && is_pretty) if (count > 0 && is_pretty) {
APPEND_INDENT(level); APPEND_INDENT(level);
}
APPEND_STRING("]"); APPEND_STRING("]");
return written_total; return written_total;
case JSONObject: case JSONObject:
object = json_value_get_object(value); object = json_value_get_object(value);
count = json_object_get_count(object); count = json_object_get_count(object);
APPEND_STRING("{"); APPEND_STRING("{");
if (count > 0 && is_pretty) if (count > 0 && is_pretty) {
APPEND_STRING("\n"); APPEND_STRING("\n");
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
key = json_object_get_name(object, i); key = json_object_get_name(object, i);
if (is_pretty) if (is_pretty) {
APPEND_INDENT(level+1); APPEND_INDENT(level+1);
}
written = json_serialize_string(key, buf); written = json_serialize_string(key, buf);
if (written < 0) if (written < 0) {
return -1; return -1;
if (buf != NULL) }
if (buf != NULL) {
buf += written; buf += written;
}
written_total += written; written_total += written;
APPEND_STRING(":"); APPEND_STRING(":");
if (is_pretty) if (is_pretty) {
APPEND_STRING(" "); APPEND_STRING(" ");
}
temp_value = json_object_get_value(object, key); temp_value = json_object_get_value(object, key);
written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
if (written < 0) if (written < 0) {
return -1; return -1;
if (buf != NULL) }
if (buf != NULL) {
buf += written; buf += written;
}
written_total += written; written_total += written;
if (i < (count - 1)) if (i < (count - 1)) {
APPEND_STRING(","); APPEND_STRING(",");
if (is_pretty) }
if (is_pretty) {
APPEND_STRING("\n"); APPEND_STRING("\n");
}
} }
if (count > 0 && is_pretty) if (count > 0 && is_pretty) {
APPEND_INDENT(level); APPEND_INDENT(level);
}
APPEND_STRING("}"); APPEND_STRING("}");
return written_total; return written_total;
case JSONString: case JSONString:
string = json_value_get_string(value); string = json_value_get_string(value);
written = json_serialize_string(string, buf); written = json_serialize_string(string, buf);
if (written < 0) if (written < 0) {
return -1; return -1;
if (buf != NULL) }
if (buf != NULL) {
buf += written; buf += written;
}
written_total += written; written_total += written;
return written_total; return written_total;
case JSONBoolean: case JSONBoolean:
if (json_value_get_boolean(value)) if (json_value_get_boolean(value)) {
APPEND_STRING("true"); APPEND_STRING("true");
else } else {
APPEND_STRING("false"); APPEND_STRING("false");
}
return written_total; return written_total;
case JSONNumber: case JSONNumber:
num = json_value_get_number(value); num = json_value_get_number(value);
if (buf != NULL) if (buf != NULL) {
num_buf = buf; 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); written = sprintf(num_buf, "%d", (int)num);
else } else {
written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
if (written < 0) }
if (written < 0) {
return -1; return -1;
if (buf != NULL) }
if (buf != NULL) {
buf += written; buf += written;
}
written_total += written; written_total += written;
return written_total; return written_total;
case JSONNull: case JSONNull:
@ -894,8 +947,9 @@ static int append_string(char *buf, const char *string) {
JSON_Value * json_parse_file(const char *filename) { JSON_Value * json_parse_file(const char *filename) {
char *file_contents = read_file(filename); char *file_contents = read_file(filename);
JSON_Value *output_value = NULL; JSON_Value *output_value = NULL;
if (file_contents == NULL) if (file_contents == NULL) {
return NULL; return NULL;
}
output_value = json_parse_string(file_contents); output_value = json_parse_string(file_contents);
parson_free(file_contents); parson_free(file_contents);
return output_value; 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) { JSON_Value * json_parse_file_with_comments(const char *filename) {
char *file_contents = read_file(filename); char *file_contents = read_file(filename);
JSON_Value *output_value = NULL; JSON_Value *output_value = NULL;
if (file_contents == NULL) if (file_contents == NULL) {
return NULL; return NULL;
}
output_value = json_parse_string_with_comments(file_contents); output_value = json_parse_string_with_comments(file_contents);
parson_free(file_contents); parson_free(file_contents);
return output_value; return output_value;
} }
JSON_Value * json_parse_string(const char *string) { JSON_Value * json_parse_string(const char *string) {
if (string == NULL) if (string == NULL) {
return NULL; return NULL;
}
return parse_value((const char**)&string, 0); 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; JSON_Value *result = NULL;
char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
string_mutable_copy = parson_strdup(string); string_mutable_copy = parson_strdup(string);
if (string_mutable_copy == NULL) if (string_mutable_copy == NULL) {
return NULL; return NULL;
}
remove_comments(string_mutable_copy, "/*", "*/"); remove_comments(string_mutable_copy, "/*", "*/");
remove_comments(string_mutable_copy, "//", "\n"); remove_comments(string_mutable_copy, "//", "\n");
string_mutable_copy_ptr = string_mutable_copy; 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 Object API */
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { 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 NULL;
}
return json_object_nget_value(object, name, strlen(name)); 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) { JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
const char *dot_position = strchr(name, '.'); const char *dot_position = strchr(name, '.');
if (!dot_position) if (!dot_position) {
return json_object_get_value(object, name); return json_object_get_value(object, name);
}
object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); object = json_value_get_object(json_object_nget_value(object, name, dot_position - name));
return json_object_dotget_value(object, dot_position + 1); 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) { 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 NULL;
}
return object->names[index]; return object->names[index];
} }
JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t 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 NULL;
}
return object->values[index]; 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 Array API */
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { 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 NULL;
}
return array->items[index]; return array->items[index];
} }
@ -1083,7 +1145,9 @@ void json_value_free(JSON_Value *value) {
json_object_free(value->value.object); json_object_free(value->value.object);
break; break;
case JSONString: case JSONString:
if (value->value.string) { parson_free(value->value.string); } if (value->value.string) {
parson_free(value->value.string);
}
break; break;
case JSONArray: case JSONArray:
json_array_free(value->value.array); 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 * json_value_init_object(void) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONObject; new_value->type = JSONObject;
new_value->value.object = json_object_init(); new_value->value.object = json_object_init();
if (!new_value->value.object) { 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 * json_value_init_array(void) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONArray; new_value->type = JSONArray;
new_value->value.array = json_array_init(); new_value->value.array = json_array_init();
if (!new_value->value.array) { if (!new_value->value.array) {
@ -1124,24 +1190,29 @@ JSON_Value * json_value_init_string(const char *string) {
char *copy = NULL; char *copy = NULL;
JSON_Value *value; JSON_Value *value;
size_t string_len = 0; size_t string_len = 0;
if (string == NULL) if (string == NULL) {
return NULL; return NULL;
}
string_len = strlen(string); string_len = strlen(string);
if (!is_valid_utf8(string, string_len)) if (!is_valid_utf8(string, string_len)) {
return NULL; return NULL;
}
copy = parson_strndup(string, string_len); copy = parson_strndup(string, string_len);
if (copy == NULL) if (copy == NULL) {
return NULL; return NULL;
}
value = json_value_init_string_no_copy(copy); value = json_value_init_string_no_copy(copy);
if (value == NULL) if (value == NULL) {
parson_free(copy); parson_free(copy);
}
return value; return value;
} }
JSON_Value * json_value_init_number(double number) { JSON_Value * json_value_init_number(double number) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONNumber; new_value->type = JSONNumber;
new_value->value.number = number; new_value->value.number = number;
return new_value; 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 * json_value_init_boolean(int boolean) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONBoolean; new_value->type = JSONBoolean;
new_value->value.boolean = boolean ? 1 : 0; new_value->value.boolean = boolean ? 1 : 0;
return new_value; return new_value;
@ -1158,8 +1230,9 @@ JSON_Value * json_value_init_boolean(int boolean) {
JSON_Value * json_value_init_null(void) { JSON_Value * json_value_init_null(void) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) if (!new_value) {
return NULL; return NULL;
}
new_value->type = JSONNull; new_value->type = JSONNull;
return new_value; return new_value;
} }
@ -1176,8 +1249,9 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) {
case JSONArray: case JSONArray:
temp_array = json_value_get_array(value); temp_array = json_value_get_array(value);
return_value = json_value_init_array(); return_value = json_value_init_array();
if (return_value == NULL) if (return_value == NULL) {
return NULL; return NULL;
}
temp_array_copy = json_value_get_array(return_value); temp_array_copy = json_value_get_array(return_value);
for (i = 0; i < json_array_get_count(temp_array); i++) { for (i = 0; i < json_array_get_count(temp_array); i++) {
temp_value = json_array_get_value(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: case JSONObject:
temp_object = json_value_get_object(value); temp_object = json_value_get_object(value);
return_value = json_value_init_object(); return_value = json_value_init_object();
if (return_value == NULL) if (return_value == NULL) {
return NULL; return NULL;
}
temp_object_copy = json_value_get_object(return_value); temp_object_copy = json_value_get_object(return_value);
for (i = 0; i < json_object_get_count(temp_object); i++) { for (i = 0; i < json_object_get_count(temp_object); i++) {
temp_key = json_object_get_name(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: case JSONString:
temp_string = json_value_get_string(value); temp_string = json_value_get_string(value);
temp_string_copy = parson_strdup(temp_string); temp_string_copy = parson_strdup(temp_string);
if (temp_string_copy == NULL) if (temp_string_copy == NULL) {
return NULL; return NULL;
}
return_value = json_value_init_string_no_copy(temp_string_copy); return_value = json_value_init_string_no_copy(temp_string_copy);
if (return_value == NULL) if (return_value == NULL) {
parson_free(temp_string_copy); parson_free(temp_string_copy);
}
return return_value; return return_value;
case JSONNull: case JSONNull:
return json_value_init_null(); return json_value_init_null();
@ -1285,8 +1362,9 @@ char * json_serialize_to_string(const JSON_Value *value) {
return NULL; return NULL;
} }
buf = (char*)parson_malloc(buf_size_bytes); buf = (char*)parson_malloc(buf_size_bytes);
if (buf == NULL) if (buf == NULL) {
return NULL; return NULL;
}
serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
if (serialization_result == JSONFailure) { if (serialization_result == JSONFailure) {
json_free_serialized_string(buf); 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) { JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
int written = -1; int written = -1;
size_t needed_size_in_bytes = json_serialization_size_pretty(value); 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; return JSONFailure;
}
written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL);
if (written < 0) if (written < 0) {
return JSONFailure; return JSONFailure;
}
return JSONSuccess; return JSONSuccess;
} }
@ -1342,8 +1422,9 @@ char * json_serialize_to_string_pretty(const JSON_Value *value) {
return NULL; return NULL;
} }
buf = (char*)parson_malloc(buf_size_bytes); buf = (char*)parson_malloc(buf_size_bytes);
if (buf == NULL) if (buf == NULL) {
return NULL; return NULL;
}
serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
if (serialization_result == JSONFailure) { if (serialization_result == JSONFailure) {
json_free_serialized_string(buf); 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_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) {
JSON_Value *value = json_value_init_string(string); JSON_Value *value = json_value_init_string(string);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_replace_value(array, i, value) == JSONFailure) { if (json_array_replace_value(array, i, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_replace_number(JSON_Array *array, size_t i, double number) {
JSON_Value *value = json_value_init_number(number); JSON_Value *value = json_value_init_number(number);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_replace_value(array, i, value) == JSONFailure) { if (json_array_replace_value(array, i, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) {
JSON_Value *value = json_value_init_boolean(boolean); JSON_Value *value = json_value_init_boolean(boolean);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_replace_value(array, i, value) == JSONFailure) { if (json_array_replace_value(array, i, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_replace_null(JSON_Array *array, size_t i) {
JSON_Value *value = json_value_init_null(); JSON_Value *value = json_value_init_null();
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_replace_value(array, i, value) == JSONFailure) { if (json_array_replace_value(array, i, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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) { JSON_Status json_array_clear(JSON_Array *array) {
size_t i = 0; size_t i = 0;
if (array == NULL) if (array == NULL) {
return JSONFailure; return JSONFailure;
}
for (i = 0; i < json_array_get_count(array); i++) { for (i = 0; i < json_array_get_count(array); i++) {
json_value_free(json_array_get_value(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) { 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 JSONFailure;
}
return json_array_add(array, value); return json_array_add(array, value);
} }
JSON_Status json_array_append_string(JSON_Array *array, const char *string) { JSON_Status json_array_append_string(JSON_Array *array, const char *string) {
JSON_Value *value = json_value_init_string(string); JSON_Value *value = json_value_init_string(string);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_append_value(array, value) == JSONFailure) { if (json_array_append_value(array, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_append_number(JSON_Array *array, double number) {
JSON_Value *value = json_value_init_number(number); JSON_Value *value = json_value_init_number(number);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_append_value(array, value) == JSONFailure) { if (json_array_append_value(array, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_append_boolean(JSON_Array *array, int boolean) {
JSON_Value *value = json_value_init_boolean(boolean); JSON_Value *value = json_value_init_boolean(boolean);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_append_value(array, value) == JSONFailure) { if (json_array_append_value(array, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_array_append_null(JSON_Array *array) {
JSON_Value *value = json_value_init_null(); JSON_Value *value = json_value_init_null();
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_array_append_value(array, value) == JSONFailure) { if (json_array_append_value(array, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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) { JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
size_t i = 0; size_t i = 0;
JSON_Value *old_value; JSON_Value *old_value;
if (object == NULL || name == NULL || value == NULL) if (object == NULL || name == NULL || value == NULL) {
return JSONFailure; return JSONFailure;
}
old_value = json_object_get_value(object, name); old_value = json_object_get_value(object, name);
if (old_value != NULL) { /* free and overwrite old value */ if (old_value != NULL) { /* free and overwrite old value */
json_value_free(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; char *current_name = NULL;
JSON_Object *temp_obj = NULL; JSON_Object *temp_obj = NULL;
JSON_Value *new_value = NULL; JSON_Value *new_value = NULL;
if (value == NULL || name == NULL || value == NULL) if (value == NULL || name == NULL || value == NULL) {
return JSONFailure; return JSONFailure;
}
dot_pos = strchr(name, '.'); dot_pos = strchr(name, '.');
if (dot_pos == NULL) { if (dot_pos == NULL) {
return json_object_set_value(object, name, value); 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_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) {
JSON_Value *value = json_value_init_string(string); JSON_Value *value = json_value_init_string(string);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_object_dotset_value(object, name, value) == JSONFailure) { if (json_object_dotset_value(object, name, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) {
JSON_Value *value = json_value_init_number(number); JSON_Value *value = json_value_init_number(number);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_object_dotset_value(object, name, value) == JSONFailure) { if (json_object_dotset_value(object, name, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) {
JSON_Value *value = json_value_init_boolean(boolean); JSON_Value *value = json_value_init_boolean(boolean);
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_object_dotset_value(object, name, value) == JSONFailure) { if (json_object_dotset_value(object, name, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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_Status json_object_dotset_null(JSON_Object *object, const char *name) {
JSON_Value *value = json_value_init_null(); JSON_Value *value = json_value_init_null();
if (value == NULL) if (value == NULL) {
return JSONFailure; return JSONFailure;
}
if (json_object_dotset_value(object, name, value) == JSONFailure) { if (json_object_dotset_value(object, name, value) == JSONFailure) {
json_value_free(value); json_value_free(value);
return JSONFailure; 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) { JSON_Status json_object_remove(JSON_Object *object, const char *name) {
size_t i = 0, last_item_index = 0; 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; return JSONFailure;
}
last_item_index = json_object_get_count(object) - 1; last_item_index = json_object_get_count(object) - 1;
for (i = 0; i < json_object_get_count(object); i++) { for (i = 0; i < json_object_get_count(object); i++) {
if (strcmp(object->names[i], name) == 0) { 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; JSON_Value_Type schema_type = JSONError, value_type = JSONError;
const char *key = NULL; const char *key = NULL;
size_t i = 0, count = 0; size_t i = 0, count = 0;
if (schema == NULL || value == NULL) if (schema == NULL || value == NULL) {
return JSONFailure; return JSONFailure;
}
schema_type = json_value_get_type(schema); schema_type = json_value_get_type(schema);
value_type = json_value_get_type(value); 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; return JSONFailure;
}
switch (schema_type) { switch (schema_type) {
case JSONArray: case JSONArray:
schema_array = json_value_get_array(schema); schema_array = json_value_get_array(schema);
value_array = json_value_get_array(value); value_array = json_value_get_array(value);
count = json_array_get_count(schema_array); count = json_array_get_count(schema_array);
if (count == 0) if (count == 0) {
return JSONSuccess; /* Empty array allows all types */ return JSONSuccess; /* Empty array allows all types */
}
/* Get first value from array, rest is ignored */ /* Get first value from array, rest is ignored */
temp_schema_value = json_array_get_value(schema_array, 0); temp_schema_value = json_array_get_value(schema_array, 0);
for (i = 0; i < json_array_get_count(value_array); i++) { 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); schema_object = json_value_get_object(schema);
value_object = json_value_get_object(value); value_object = json_value_get_object(value);
count = json_object_get_count(schema_object); count = json_object_get_count(schema_object);
if (count == 0) if (count == 0) {
return JSONSuccess; /* Empty object allows all objects */ 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 */ return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
key = json_object_get_name(schema_object, i); key = json_object_get_name(schema_object, i);
temp_schema_value = json_object_get_value(schema_object, key); temp_schema_value = json_object_get_value(schema_object, key);
temp_value = json_object_get_value(value_object, key); temp_value = json_object_get_value(value_object, key);
if (temp_value == NULL) if (temp_value == NULL) {
return JSONFailure; return JSONFailure;
if (json_validate(temp_schema_value, temp_value) == JSONFailure) }
if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
return JSONFailure; return JSONFailure;
}
} }
return JSONSuccess; return JSONSuccess;
case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: case JSONString: case JSONNumber: case JSONBoolean: case JSONNull:

Loading…
Cancel
Save