Make the argument of json_deep_copy() const

Fixes #126.
This commit is contained in:
Petri Lehtinen 2013-06-17 08:40:43 +03:00
parent f639fbd2c3
commit df3dfabc18
3 changed files with 21 additions and 13 deletions

View File

@ -1382,7 +1382,7 @@ copied in a recursive fashion.
Returns a shallow copy of *value*, or *NULL* on error. Returns a shallow copy of *value*, or *NULL* on error.
.. function:: json_t *json_deep_copy(json_t *value) .. function:: json_t *json_deep_copy(const json_t *value)
.. refcounting:: new .. refcounting:: new

View File

@ -231,7 +231,7 @@ int json_equal(json_t *value1, json_t *value2);
/* copying */ /* copying */
json_t *json_copy(json_t *value); json_t *json_copy(json_t *value);
json_t *json_deep_copy(json_t *value); json_t *json_deep_copy(const json_t *value);
/* decoding */ /* decoding */

View File

@ -292,19 +292,27 @@ static json_t *json_object_copy(json_t *object)
return result; return result;
} }
static json_t *json_object_deep_copy(json_t *object) static json_t *json_object_deep_copy(const json_t *object)
{ {
json_t *result; json_t *result;
void *iter;
const char *key;
json_t *value;
result = json_object(); result = json_object();
if(!result) if(!result)
return NULL; return NULL;
json_object_foreach(object, key, value) /* Cannot use json_object_foreach because object has to be cast
non-const */
iter = json_object_iter((json_t *)object);
while(iter) {
const char *key;
const json_t *value;
key = json_object_iter_key(iter);
value = json_object_iter_value(iter);
json_object_set_new_nocheck(result, key, json_deep_copy(value)); json_object_set_new_nocheck(result, key, json_deep_copy(value));
iter = json_object_iter_next((json_t *)object, iter);
}
return result; return result;
} }
@ -595,7 +603,7 @@ static json_t *json_array_copy(json_t *array)
return result; return result;
} }
static json_t *json_array_deep_copy(json_t *array) static json_t *json_array_deep_copy(const json_t *array)
{ {
json_t *result; json_t *result;
size_t i; size_t i;
@ -687,7 +695,7 @@ static int json_string_equal(json_t *string1, json_t *string2)
return strcmp(json_string_value(string1), json_string_value(string2)) == 0; return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
} }
static json_t *json_string_copy(json_t *string) static json_t *json_string_copy(const json_t *string)
{ {
return json_string_nocheck(json_string_value(string)); return json_string_nocheck(json_string_value(string));
} }
@ -734,7 +742,7 @@ static int json_integer_equal(json_t *integer1, json_t *integer2)
return json_integer_value(integer1) == json_integer_value(integer2); return json_integer_value(integer1) == json_integer_value(integer2);
} }
static json_t *json_integer_copy(json_t *integer) static json_t *json_integer_copy(const json_t *integer)
{ {
return json_integer(json_integer_value(integer)); return json_integer(json_integer_value(integer));
} }
@ -786,7 +794,7 @@ static int json_real_equal(json_t *real1, json_t *real2)
return json_real_value(real1) == json_real_value(real2); return json_real_value(real1) == json_real_value(real2);
} }
static json_t *json_real_copy(json_t *real) static json_t *json_real_copy(const json_t *real)
{ {
return json_real(json_real_value(real)); return json_real(json_real_value(real));
} }
@ -912,7 +920,7 @@ json_t *json_copy(json_t *json)
return NULL; return NULL;
} }
json_t *json_deep_copy(json_t *json) json_t *json_deep_copy(const json_t *json)
{ {
if(!json) if(!json)
return NULL; return NULL;
@ -936,7 +944,7 @@ json_t *json_deep_copy(json_t *json)
return json_real_copy(json); return json_real_copy(json);
if(json_is_true(json) || json_is_false(json) || json_is_null(json)) if(json_is_true(json) || json_is_false(json) || json_is_null(json))
return json; return (json_t *)json;
return NULL; return NULL;
} }