Fixes a bug in json_array_remove and adds relevant tests (thanks to KB for finding this).

Also - it's 2016, time to update copyright notices, yay!
This commit is contained in:
Krzysztof Gabis 2016-01-13 20:47:47 +00:00
parent 8324ff92bf
commit c9b920c4a3
3 changed files with 18 additions and 6 deletions

View File

@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
Copyright (c) 2012 - 2015 Krzysztof Gabis
Copyright (c) 2012 - 2016 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -1322,21 +1322,26 @@ char * json_serialize_to_string_pretty(const JSON_Value *value) {
return buf;
}
void json_free_serialized_string(char *string) {
parson_free(string);
}
JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
JSON_Value *temp_value = NULL;
size_t last_element_ix = 0;
if (array == NULL || ix >= json_array_get_count(array)) {
return JSONFailure;
}
last_element_ix = json_array_get_count(array) - 1;
json_value_free(json_array_get_value(array, ix));
if (ix != last_element_ix) { /* Replace value with one from the end of array */
temp_value = json_array_get_value(array, last_element_ix);
if (temp_value == NULL) {
return JSONFailure;
}
array->items[ix] = temp_value;
}
array->count -= 1;
if (ix != last_element_ix) /* Replace value with one from the end of array */
array->items[ix] = json_array_get_value(array, last_element_ix);
return JSONSuccess;
}

View File

@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
Copyright (c) 2012 - 2015 Krzysztof Gabis
Copyright (c) 2012 - 2016 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,6 @@
/*
Parson ( http://kgabis.github.com/parson/ )
Copyright (c) 2012 - 2015 Krzysztof Gabis
Copyright (c) 2012 - 2016 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -311,7 +311,14 @@ void test_suite_5(void) {
TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure);
TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess);
TEST(json_array_remove(interests_arr, 0) == JSONSuccess);
TEST(json_array_remove(interests_arr, 1) == JSONSuccess);
TEST(json_array_remove(interests_arr, 0) == JSONSuccess);
TEST(json_array_remove(interests_arr, 0) == JSONFailure); /* should be empty by now */
TEST(json_object_remove(obj, "interests") == JSONSuccess);
/* UTF-8 tests */
TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess);