#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)
{
free(object);
return NULL;
}
+
+ object->visited = 0;
+
return &object->json;
}
free(object);
}
+unsigned int json_object_size(const json_t *json)
+{
+ json_object_t *object;
+
+ if(!json_is_object(json))
+ return -1;
+
+ object = json_to_object(json);
+ return object->hashtable.size;
+}
+
json_t *json_object_get(const json_t *json, const char *key)
{
json_object_t *object;
if(!key || !value)
return -1;
- if(!json_is_object(json))
+ if(!json_is_object(json) || json == value)
{
json_decref(value);
return -1;
int json_object_set_new(json_t *json, const char *key, json_t *value)
{
- if(!utf8_check_string(key, -1))
+ if(!key || !utf8_check_string(key, -1))
{
json_decref(value);
return -1;
return hashtable_del(&object->hashtable, key);
}
+int json_object_clear(json_t *json)
+{
+ json_object_t *object;
+
+ if(!json_is_object(json))
+ return -1;
+
+ object = json_to_object(json);
+ hashtable_clear(&object->hashtable);
+
+ return 0;
+}
+
+int json_object_update(json_t *object, json_t *other)
+{
+ void *iter;
+
+ if(!json_is_object(object) || !json_is_object(other))
+ return -1;
+
+ iter = json_object_iter(other);
+ while(iter) {
+ const char *key;
+ json_t *value;
+
+ key = json_object_iter_key(iter);
+ value = json_object_iter_value(iter);
+
+ if(json_object_set(object, key, value))
+ return -1;
+
+ iter = json_object_iter_next(other, iter);
+ }
+
+ return 0;
+}
+
void *json_object_iter(json_t *json)
{
json_object_t *object;
{
json_array_t *array = malloc(sizeof(json_array_t));
if(!array)
- return NULL;
+ return NULL;
json_init(&array->json, JSON_ARRAY);
array->entries = 0;
return NULL;
}
+ array->visited = 0;
+
return &array->json;
}
if(!value)
return -1;
- if(!json_is_array(json))
+ if(!json_is_array(json) || json == value)
{
json_decref(value);
return -1;
if(!value)
return -1;
- if(!json_is_array(json))
+ if(!json_is_array(json) || json == value)
{
json_decref(value);
return -1;
if(!value)
return -1;
- if(!json_is_array(json)) {
+ if(!json_is_array(json) || json == value) {
json_decref(value);
return -1;
}
string = malloc(sizeof(json_string_t));
if(!string)
- return NULL;
+ return NULL;
json_init(&string->json, JSON_STRING);
string->value = strdup(value);
return json_to_string(json)->value;
}
+int json_string_set(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);
{
json_integer_t *integer = malloc(sizeof(json_integer_t));
if(!integer)
- return NULL;
+ return NULL;
json_init(&integer->json, JSON_INTEGER);
integer->value = value;
return json_to_integer(json)->value;
}
+int json_integer_set(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);
{
json_real_t *real = malloc(sizeof(json_real_t));
if(!real)
- return NULL;
+ return NULL;
json_init(&real->json, JSON_REAL);
real->value = value;
return json_to_real(json)->value;
}
-static void json_delete_real (json_real_t *real)
+int json_real_set(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);
}