diff options
Diffstat (limited to 'src/object.c')
-rw-r--r-- | src/object.c | 143 |
1 files changed, 70 insertions, 73 deletions
diff --git a/src/object.c b/src/object.c index fea4787..0474f27 100644 --- a/src/object.c +++ b/src/object.c @@ -1,18 +1,16 @@ +#define JSON_PRIVATE #include <assert.h> #include <stdlib.h> #include <string.h> #include <json.h> #include "internal.h" -//inline static size_t hash(const char *key) { - //if (!key) - //return 0; - //size_t ret = 5381; - //for (const char *p = key; *p != '\0'; p++) { - //ret = ((ret << 5) + ret) + *p; - //} - //return ret; -//} +inline static void adjust_siblings(struct json *json, struct json *src) { + if (json->prev) + json->prev->next = src; + if (json->next) + json->next->prev = src; +} void add_to_object(struct json *dest, char *key, struct json *src) { assert(dest->type == JSON_OBJECT); @@ -20,7 +18,6 @@ void add_to_object(struct json *dest, char *key, struct json *src) { assert(src); src->key = (free(src->key), key); - //src->index = hash(src->key) % dest->children->maxitems; for (size_t i = 0; i < dest->children->nitems; i++) { struct json *e = dest->children->items[i]; @@ -31,8 +28,7 @@ void add_to_object(struct json *dest, char *key, struct json *src) { src->prev = e->prev; src->next = e->next; - if (e->prev) e->prev->next = src; - if (e->next) e->next->prev = src; + adjust_siblings(e, src); json_clear(e); free(e); @@ -43,13 +39,6 @@ void add_to_object(struct json *dest, char *key, struct json *src) { add_children(dest, src); } -struct json *json_new_object(void) { - struct json *json = json_new(); - json->type = JSON_OBJECT; - init_children(json); - return json; -} - enum json_parse_result parse_object(struct json **json_out, struct raw_json *raw, size_t depth) { assert(raw->data[raw->index] == '{'); enum json_parse_result ret = JSON_PARSE_OK; @@ -63,7 +52,7 @@ enum json_parse_result parse_object(struct json **json_out, struct raw_json *raw goto err; } - json = json_new_object(); + json = json_object(); if (!json) return JSON_OOM; @@ -126,82 +115,90 @@ end: return ret; } -bool json_object_add(struct json *dest, const char *key, struct json *src) { +struct json *json_set(struct json *dest, const char *key, struct json *src) { if (!dest || !key || !src) - return false; + return NULL; + json_detach(src); add_to_object(dest, strdup(key), src); - return true; + return src; } -struct json *json_object_get_mut(struct json *obj, const char *key) { - if (obj->type != JSON_OBJECT) return NULL; - struct json *j = NULL; - json_foreach(j, obj) { +struct json *json_get_mut(struct json *json, const char *key) { + if (!json_is_object(json) || !key) + return NULL; + json_foreach(j, json) { if (strcmp(j->key, key) == 0) - break; + return j; } - return j; + return NULL; } -struct json *json_object_getn_mut(struct json *obj, const char *key, size_t n) { - if (obj->type != JSON_OBJECT) return NULL; - struct json *j = NULL; - json_foreach(j, obj) { - if (strncmp(j->key, key, n) == 0) - break; +struct json *json_get_sized_mut(struct json *json, size_t count, const char key[count]) { + if (!json_is_object(json) || !key) + return NULL; + json_foreach(j, json) { + if (strncmp(j->key, key, count) == 0) + return j; } - return j; -} - -const struct json *json_object_get_const(const struct json *obj, const char *key) { - return json_object_get_mut((struct json *) obj, key); + return NULL; } -const struct json *json_object_getn_const(const struct json *obj, const char *key, size_t n) { - return json_object_getn_mut((struct json *) obj, key, n); +const struct json *json_get_const(const struct json *obj, const char *key) { + return json_get_mut((struct json *) obj, key); } -struct json *json_object_add_object(struct json *dest, const char *key) { - struct json *obj = json_new_object(); - json_object_add(dest, key, obj); - return obj; +const struct json *json_get_sized_const(const struct json *obj, size_t count, const char key[count]) { + return json_get_sized_mut((struct json *) obj, count, key); } -struct json *json_object_add_array(struct json *dest, const char *key) { - struct json *array = json_new_array(); - json_object_add(dest, key, array); - return array; -} -struct json *json_object_add_number(struct json *dest, const char *key, double num) { - struct json *number = json_new_number(num); - json_object_add(dest, key, number); - return number; -} - -struct json *json_object_add_string(struct json *dest, const char *key, const char *string) { - struct json *str = json_new_string_copy(string); - json_object_add(dest, key, str); - return str; +struct json *json_detach_key(struct json *json, const char *key) { + struct json *tmp = json_get(json, key); + json_detach(tmp); + return tmp; } -struct json *json_object_add_bool(struct json *dest, const char *key, bool boolean) { - struct json *b = json_new_bool(boolean); - json_object_add(dest, key, b); - return b; +struct json *json_swap_key(struct json *json, const char *key, struct json *src) { + if (!json_is_object(json) || !key) + return NULL; + json_detach(src); + json_foreach(j, json) { + if (strcmp(j->key, key)) { + json->children->items[j->index] = src; + src->index = j->index; + adjust_siblings(j, src); + return j; + } + } + return NULL; } -struct json *json_object_add_null(struct json *dest, const char *key) { - struct json *n = json_new_null(); - json_object_add(dest, key, n); - return n; +void json_delete_key(struct json *json, const char *key) { + struct json *tmp = json_detach_key(json, key); + json_delete(tmp); } -struct json *json_object_detach(struct json *json, const char *key) { - struct json *tmp = json_object_get(json, key); +struct json *json_detach_key_sized(struct json *json, size_t len, const char key[len]) { + struct json *tmp = json_get_sized(json, len, key); json_detach(tmp); return tmp; } -void json_object_delete(struct json *json, const char *key) { - struct json *tmp = json_object_detach(json, key); + +void json_delete_key_sized(struct json *json, size_t len, const char key[len]) { + struct json *tmp = json_detach_key_sized(json, len, key); json_delete(tmp); } + +struct json *json_swap_key_sized(struct json *json, size_t len, const char key[len], struct json *src) { + if (!json_is_object(json) || !key) + return NULL; + json_detach(src); + json_foreach(j, json) { + if (strncmp(j->key, key, len)) { + json->children->items[j->index] = src; + src->index = j->index; + adjust_siblings(j, src); + return j; + } + } + return NULL; +} |