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!
pull/35/head
Krzysztof Gabis 9 years ago
parent 8324ff92bf
commit c9b920c4a3
  1. 13
      parson.c
  2. 2
      parson.h
  3. 9
      tests.c

@ -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;
}

@ -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

@ -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);

Loading…
Cancel
Save