X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fvalue.c;h=1982b90077f43f178a9f951280d3d408ad4597d0;hb=4cd777712b1e4ec17dc9efcced80a90f83ec1915;hp=b06cb4f2f5879edbfaac45eae384601ce42b9d64;hpb=ca7703fbd127a6279e9842a843f317a639db87e2;p=jansson.git diff --git a/src/value.c b/src/value.c index b06cb4f..1982b90 100644 --- a/src/value.c +++ b/src/value.c @@ -15,41 +15,6 @@ #include "utf.h" #include "util.h" -#define container_of(ptr_, type_, member_) \ - ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_)) - -typedef struct { - json_t json; - hashtable_t hashtable; -} json_object_t; - -typedef struct { - json_t json; - unsigned int size; - unsigned int entries; - json_t **table; -} json_array_t; - -typedef struct { - json_t json; - char *value; -} json_string_t; - -typedef struct { - json_t json; - double value; -} 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_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) { @@ -98,6 +63,9 @@ json_t *json_object(void) free(object); return NULL; } + + object->visited = 0; + return &object->json; } @@ -136,7 +104,7 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) if(!key || !value) return -1; - if(!json_is_object(json)) + if(!json_is_object(json) || json == value) { json_decref(value); return -1; @@ -273,6 +241,8 @@ json_t *json_array(void) return NULL; } + array->visited = 0; + return &array->json; } @@ -315,7 +285,7 @@ int json_array_set_new(json_t *json, unsigned int index, json_t *value) if(!value) return -1; - if(!json_is_array(json)) + if(!json_is_array(json) || json == value) { json_decref(value); return -1; @@ -383,7 +353,7 @@ int json_array_append_new(json_t *json, json_t *value) if(!value) return -1; - if(!json_is_array(json)) + if(!json_is_array(json) || json == value) { json_decref(value); return -1; @@ -409,7 +379,7 @@ int json_array_insert_new(json_t *json, unsigned int index, json_t *value) if(!value) return -1; - if(!json_is_array(json)) { + if(!json_is_array(json) || json == value) { json_decref(value); return -1; } @@ -538,6 +508,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 +555,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 +592,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); }