Make integer, real and string mutable
authorPetri Lehtinen <petri@digip.org>
Tue, 13 Oct 2009 19:51:04 +0000 (22:51 +0300)
committerPetri Lehtinen <petri@digip.org>
Wed, 14 Oct 2009 05:23:02 +0000 (08:23 +0300)
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.

doc/apiref.rst
src/jansson.h
src/value.c
test/testprogs/test_simple.c

index 1a6a77b..cc36117 100644 (file)
@@ -194,11 +194,18 @@ String
    Returns a new JSON string, or *NULL* on error. *value* must be a
    valid UTF-8 encoded Unicode string.
 
-.. cfunction:: const char *json_string_value(const json_t *json)
+.. cfunction:: const char *json_string_value(const json_t *string)
 
-   Returns the associated value of the JSON string *json* as a null
-   terminated UTF-8 encoded string, or *NULL* if *json* is not a JSON
-   string.
+   Returns the associated value of *string* as a null terminated UTF-8
+   encoded string, or *NULL* if *string* is not a JSON string.
+
+.. cfunction:: int json_string_set(const json_t *string, const char *value)
+
+   Sets the associated value of *string* to *value*. *value* must be a
+   valid UTF-8 encoded Unicode string. Returns 0 on success and -1 on
+   error.
+
+   .. versionadded:: 1.1
 
 
 Number
@@ -210,10 +217,17 @@ Number
 
    Returns a new JSON integer, or *NULL* on error.
 
-.. cfunction:: int json_integer_value(const json_t *json)
+.. cfunction:: int json_integer_value(const json_t *integer)
+
+   Returns the associated value of *integer*, or 0 if *json* is not a
+   JSON integer.
+
+.. cfunction:: int json_integer_set(const json_t *integer, int value)
 
-   Returns the associated value the JSON integer *json*. If *json* is
-   *NULL* or not a JSON integer, 0 is returned.
+   Sets the associated value of *integer* to *value*. Returns 0 on
+   success and -1 if *integer* is not a JSON integer.
+
+   .. versionadded:: 1.1
 
 .. cfunction:: json_t *json_real(double value)
 
@@ -221,10 +235,17 @@ Number
 
    Returns a new JSON real, or *NULL* on error.
 
-.. cfunction:: double json_real_value(const json_t *json)
+.. cfunction:: double json_real_value(const json_t *real)
+
+   Returns the associated value of *real*, or 0.0 if *real* is not a
+   JSON real.
 
-   Returns the associated value of the JSON real *json*. If *json* is
-   *NULL* or not a JSON real, 0.0 is returned.
+.. cfunction:: int json_real_set(const json_t *real, double value)
+
+   Sets the associated value of *real* to *value*. Returns 0 on
+   success and -1 if *real* is not a JSON real.
+
+   .. versionadded:: 1.1
 
 In addition to the functions above, there's a common query function
 for integers and reals:
index 6751998..aff76e6 100644 (file)
@@ -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 */
 
index b06cb4f..076e335 100644 (file)
@@ -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);
 }
index 4491ed2..9d7691e 100644 (file)
@@ -56,12 +56,24 @@ int main()
         fail("json_string failed");
     if(strcmp(json_string_value(value), "foo"))
         fail("invalid string value");
+
+    if(json_string_set(value, "bar"))
+        fail("json_string_set failed");
+    if(strcmp(json_string_value(value), "bar"))
+        fail("invalid string value");
+
     json_decref(value);
 
     value = json_string(NULL);
     if(value)
         fail("json_string(NULL) failed");
 
+    /* invalid UTF-8  */
+    value = json_string("a\xefz");
+    if(value)
+        fail("json_string(<invalid utf-8>) failed");
+
+
     value = json_integer(123);
     if(!value)
         fail("json_integer failed");
@@ -69,6 +81,14 @@ int main()
         fail("invalid integer value");
     if(json_number_value(value) != 123.0)
         fail("invalid number value");
+
+    if(json_integer_set(value, 321))
+        fail("json_integer_set failed");
+    if(json_integer_value(value) != 321)
+        fail("invalid integer value");
+    if(json_number_value(value) != 321.0)
+        fail("invalid number value");
+
     json_decref(value);
 
     value = json_real(123.123);
@@ -78,6 +98,14 @@ int main()
         fail("invalid integer value");
     if(json_number_value(value) != 123.123)
         fail("invalid number value");
+
+    if(json_real_set(value, 321.321))
+        fail("json_real_set failed");
+    if(json_real_value(value) != 321.321)
+        fail("invalid real value");
+    if(json_number_value(value) != 321.321)
+        fail("invalid number value");
+
     json_decref(value);
 
     value = json_true();