aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2009-10-13 22:51:04 +0300
committerPetri Lehtinen <petri@digip.org>2009-10-14 08:23:02 +0300
commit951d091f071a3b1fe4c0ae29e31e94b9c475473e (patch)
tree6564fa0f50caa86e9a95f4db71b7d7003d423f07 /src
parent185e107d24f24a4641d598722bc3f60eac10effc (diff)
downloadjansson-951d091f071a3b1fe4c0ae29e31e94b9c475473e.zip
jansson-951d091f071a3b1fe4c0ae29e31e94b9c475473e.tar.gz
jansson-951d091f071a3b1fe4c0ae29e31e94b9c475473e.tar.bz2
Make integer, real and string mutable
Added functions: json_string_set json_integer_set json_real_set While at it, clarify the documentation and parameter naming of json_{string,integer,real}_value() a bit.
Diffstat (limited to 'src')
-rw-r--r--src/jansson.h10
-rw-r--r--src/value.c41
2 files changed, 47 insertions, 4 deletions
diff --git a/src/jansson.h b/src/jansson.h
index 6751998..aff76e6 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -115,11 +115,15 @@ int json_array_insert(json_t *array, unsigned int index, json_t *value)
return json_array_insert_new(array, index, json_incref(value));
}
-const char *json_string_value(const json_t *json);
-int json_integer_value(const json_t *json);
-double json_real_value(const json_t *json);
+const char *json_string_value(const json_t *string);
+int json_integer_value(const json_t *integer);
+double json_real_value(const json_t *real);
double json_number_value(const json_t *json);
+int json_string_set(const json_t *string, const char *value);
+int json_integer_set(const json_t *integer, int value);
+int json_real_set(const json_t *real, double value);
+
/* loading, printing */
diff --git a/src/value.c b/src/value.c
index b06cb4f..076e335 100644
--- a/src/value.c
+++ b/src/value.c
@@ -538,6 +538,25 @@ const char *json_string_value(const json_t *json)
return json_to_string(json)->value;
}
+int json_string_set(const json_t *json, const char *value)
+{
+ char *dup;
+ json_string_t *string;
+
+ if(!json_is_string(json) || !value || !utf8_check_string(value, -1))
+ return -1;
+
+ dup = strdup(value);
+ if(!dup)
+ return -1;
+
+ string = json_to_string(json);
+ free(string->value);
+ string->value = dup;
+
+ return 0;
+}
+
static void json_delete_string(json_string_t *string)
{
free(string->value);
@@ -566,6 +585,16 @@ int json_integer_value(const json_t *json)
return json_to_integer(json)->value;
}
+int json_integer_set(const json_t *json, int value)
+{
+ if(!json_is_integer(json))
+ return -1;
+
+ json_to_integer(json)->value = value;
+
+ return 0;
+}
+
static void json_delete_integer(json_integer_t *integer)
{
free(integer);
@@ -593,7 +622,17 @@ double json_real_value(const json_t *json)
return json_to_real(json)->value;
}
-static void json_delete_real (json_real_t *real)
+int json_real_set(const json_t *json, double value)
+{
+ if(!json_is_real(json))
+ return 0;
+
+ json_to_real(json)->value = value;
+
+ return 0;
+}
+
+static void json_delete_real(json_real_t *real)
{
free(real);
}