Decode and check for correct UTF-8
[jansson.git] / src / value.c
index 998446c..6f0094b 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <jansson.h>
 #include "hashtable.h"
+#include "jansson_private.h"
+#include "utf.h"
 #include "util.h"
 
 #define container_of(ptr_, type_, member_)  \
@@ -29,12 +31,18 @@ typedef struct {
 typedef struct {
     json_t json;
     double value;
-} json_number_t;
+} json_real_t;
+
+typedef struct {
+    json_t json;
+    int value;
+} json_integer_t;
 
 #define json_to_object(json_)  container_of(json_, json_object_t, json)
 #define json_to_array(json_)   container_of(json_, json_array_t, json)
 #define json_to_string(json_)  container_of(json_, json_string_t, json)
-#define json_to_number(json_)  container_of(json_, json_number_t, json)
+#define json_to_real(json_)   container_of(json_, json_real_t, json)
+#define json_to_integer(json_) container_of(json_, json_integer_t, json)
 
 static inline void json_init(json_t *json, json_type type)
 {
@@ -103,7 +111,7 @@ json_t *json_object_get(const json_t *json, const char *key)
     return hashtable_get(&object->hashtable, key);
 }
 
-int json_object_set(json_t *json, const char *key, json_t *value)
+int json_object_set_nocheck(json_t *json, const char *key, json_t *value)
 {
     json_object_t *object;
 
@@ -114,6 +122,14 @@ int json_object_set(json_t *json, const char *key, json_t *value)
     return hashtable_set(&object->hashtable, strdup(key), json_incref(value));
 }
 
+int json_object_set(json_t *json, const char *key, json_t *value)
+{
+    if(!utf8_check_string(key, -1))
+        return -1;
+
+    return json_object_set_nocheck(json, key, value);
+}
+
 int json_object_del(json_t *json, const char *key)
 {
     json_object_t *object;
@@ -249,7 +265,7 @@ int json_array_append(json_t *json, json_t *value)
 
 /*** string ***/
 
-json_t *json_string(const char *value)
+json_t *json_string_nocheck(const char *value)
 {
     json_string_t *string = malloc(sizeof(json_string_t));
     if(!string)
@@ -260,6 +276,14 @@ json_t *json_string(const char *value)
     return &string->json;
 }
 
+json_t *json_string(const char *value)
+{
+    if(!utf8_check_string(value, -1))
+        return NULL;
+
+    return json_string_nocheck(value);
+}
+
 const char *json_string_value(const json_t *json)
 {
     if(!json_is_string(json))
@@ -274,31 +298,71 @@ static void json_delete_string(json_string_t *string)
     free(string);
 }
 
-json_t *json_number(double value)
+
+/*** integer ***/
+
+json_t *json_integer(int value)
 {
-    json_number_t *number = malloc(sizeof(json_number_t));
-    if(!number)
+    json_integer_t *integer = malloc(sizeof(json_integer_t));
+    if(!integer)
        return NULL;
-    json_init(&number->json, JSON_NUMBER);
+    json_init(&integer->json, JSON_INTEGER);
 
-    number->value = value;
-    return &number->json;
+    integer->value = value;
+    return &integer->json;
 }
 
+int json_integer_value(const json_t *json)
+{
+    if(!json_is_integer(json))
+        return 0;
 
-/*** number ***/
+    return json_to_integer(json)->value;
+}
 
-double json_number_value(const json_t *json)
+static void json_delete_integer(json_integer_t *integer)
 {
-    if(!json_is_number(json))
-        return 0.0;
+    free(integer);
+}
+
+
+/*** real ***/
+
+json_t *json_real(double value)
+{
+    json_real_t *real = malloc(sizeof(json_real_t));
+    if(!real)
+       return NULL;
+    json_init(&real->json, JSON_REAL);
+
+    real->value = value;
+    return &real->json;
+}
+
+double json_real_value(const json_t *json)
+{
+    if(!json_is_real(json))
+        return 0;
 
-    return json_to_number(json)->value;
+    return json_to_real(json)->value;
 }
 
-static void json_delete_number(json_number_t *number)
+static void json_delete_real (json_real_t *real)
 {
-    free(number);
+    free(real);
+}
+
+
+/*** number ***/
+
+double json_number_value(const json_t *json)
+{
+    if(json_is_integer(json))
+        return json_integer_value(json);
+    else if(json_is_real(json))
+        return json_real_value(json);
+    else
+        return 0.0;
 }
 
 
@@ -347,8 +411,11 @@ void json_delete(json_t *json)
     else if(json_is_string(json))
         json_delete_string(json_to_string(json));
 
-    else if(json_is_number(json))
-        json_delete_number(json_to_number(json));
+    else if(json_is_integer(json))
+        json_delete_integer(json_to_integer(json));
+
+    else if(json_is_real(json))
+        json_delete_real(json_to_real(json));
 
     /* json_delete is not called for true, false or null */
 }