aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2013-06-17 08:40:43 +0300
committerPetri Lehtinen <petri@digip.org>2013-06-17 08:40:49 +0300
commitdf3dfabc186987de3db136db13413ae409a8da8d (patch)
tree8515983512961a473467c03691ac402fbfe38e0b
parentf639fbd2c329ef60ba05aabade529686871458e1 (diff)
downloadjansson-pull-126.zip
jansson-pull-126.tar.gz
jansson-pull-126.tar.bz2
Make the argument of json_deep_copy() constpull-126
Fixes #126.
-rw-r--r--doc/apiref.rst2
-rw-r--r--src/jansson.h2
-rw-r--r--src/value.c30
3 files changed, 21 insertions, 13 deletions
diff --git a/doc/apiref.rst b/doc/apiref.rst
index 5a33e9f..d2475b2 100644
--- a/doc/apiref.rst
+++ b/doc/apiref.rst
@@ -1382,7 +1382,7 @@ copied in a recursive fashion.
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
diff --git a/src/jansson.h b/src/jansson.h
index d441fcf..79b4fc0 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -231,7 +231,7 @@ int json_equal(json_t *value1, json_t *value2);
/* copying */
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 */
diff --git a/src/value.c b/src/value.c
index 261c0f1..0ba6fb2 100644
--- a/src/value.c
+++ b/src/value.c
@@ -292,19 +292,27 @@ static json_t *json_object_copy(json_t *object)
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;
-
- const char *key;
- json_t *value;
+ void *iter;
result = json_object();
if(!result)
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));
+ iter = json_object_iter_next((json_t *)object, iter);
+ }
return result;
}
@@ -595,7 +603,7 @@ static json_t *json_array_copy(json_t *array)
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;
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;
}
-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));
}
@@ -734,7 +742,7 @@ static int json_integer_equal(json_t *integer1, json_t *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));
}
@@ -786,7 +794,7 @@ static int json_real_equal(json_t *real1, json_t *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));
}
@@ -912,7 +920,7 @@ json_t *json_copy(json_t *json)
return NULL;
}
-json_t *json_deep_copy(json_t *json)
+json_t *json_deep_copy(const json_t *json)
{
if(!json)
return NULL;
@@ -936,7 +944,7 @@ json_t *json_deep_copy(json_t *json)
return json_real_copy(json);
if(json_is_true(json) || json_is_false(json) || json_is_null(json))
- return json;
+ return (json_t *)json;
return NULL;
}