10#include "../nyx_node_internal.h"
14static void internal_dict_clear(
44 internal_dict_clear(
object);
51static void internal_dict_clear(
nyx_dict_t *
object)
55 for(nyx_dict_node_t *node = object->head; node != NULL;)
59 nyx_dict_node_t *temp = node;
65 temp->value->parent = NULL;
67 nyx_object_unref(temp->value);
86 internal_dict_clear(
object);
95 for(nyx_dict_node_t *prev_node = NULL, *curr_node = object->head; curr_node != NULL; prev_node = curr_node, curr_node = curr_node->next)
97 if(strcmp(curr_node->key, key) == 0)
101 if(prev_node == NULL)
103 object->head = curr_node->next;
107 prev_node->next = curr_node->next;
112 if(curr_node == object->tail)
114 object->tail = prev_node;
119 curr_node->value->parent = NULL;
121 nyx_object_unref(curr_node->value);
138 if(iter->
head != NULL)
141 *key = iter->
head->key;
145 *
object = iter->
head->value;
148 iter->
idx += 0x0000000000001;
163 for(nyx_dict_node_t *curr_node = object->head; curr_node != NULL; curr_node = curr_node->next)
165 if(strcmp(curr_node->key, key) == 0)
167 return curr_node->value;
182 if(!NYX_OBJECT_CHECK_MAGIC(value))
208 bool modified =
true;
210 for(nyx_dict_node_t *curr_node = object->head; curr_node != NULL; curr_node = curr_node->next)
212 if(strcmp(curr_node->key, key) == 0)
214 modified = !nyx_object_equal(curr_node->value, value);
216 curr_node->value->parent = NULL;
218 nyx_object_ref( value );
219 nyx_object_unref(curr_node->value);
221 curr_node->value = value;
229 nyx_dict_node_t *node =
nyx_memory_alloc(
sizeof(nyx_dict_node_t) + strlen(key) + 1);
231 node->key = strcpy((
str_t) (node + 1), key);
233 nyx_object_ref(value);
240 if(object->head == NULL)
247 object->tail->next = node;
266 for(nyx_dict_node_t *node = object->head; node != NULL; node = node->next, result++) { }
277 nyx_string_builder_t *sb = nyx_string_builder_new();
279 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE,
"{");
281 for(nyx_dict_node_t *curr_node = object->head; curr_node != NULL; curr_node = curr_node->next)
285 str_t curr_node_val = nyx_object_to_string(curr_node->value);
287 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE,
"\"");
288 nyx_string_builder_append(sb, NYX_SB_ESCAPE_JSON, curr_node->key);
289 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE,
"\"",
":", curr_node_val);
295 if(curr_node->next != NULL)
297 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE,
",");
303 nyx_string_builder_append(sb, NYX_SB_NO_ESCAPE,
"}");
305 str_t result = nyx_string_builder_to_string(sb);
307 nyx_string_builder_free(sb);
size_t idx
Current zero-based iteration index.
struct nyx_dict_node_s * head
Next JSON object to visit.
size_t nyx_dict_size(const nyx_dict_t *object)
Gets the number of items in the provided JSON dict object.
bool nyx_dict_set(nyx_dict_t *object, STR_t key, void *value)
Sets a JSON object in the provided JSON dict object.
bool nyx_dict_iterate(nyx_dict_iter_t *iter, STR_t *key, nyx_object_t **object)
Iterates over a JSON dict object.
str_t nyx_dict_to_string(const nyx_dict_t *object)
Returns a string representing the provided JSON dict object.
nyx_object_t * nyx_dict_get(const nyx_dict_t *object, STR_t key)
Gets the JSON object of the provided key.
void nyx_dict_del(nyx_dict_t *object, STR_t key)
Deletes the entry of the provided key.
void nyx_dict_clear(nyx_dict_t *object)
Clears the content of the provided JSON dict object.
nyx_dict_t * nyx_dict_new(void)
Allocates a new JSON dict object.
Struct describing a JSON dict iterator.
Struct describing a JSON dict object.
#define NYX_LOG_FATAL(fmt,...)
Logs a fatal message.
#define NYX_LOG_ERROR(fmt,...)
Logs an error message.
#define STR_t
Alias for const char *.
__NYX_NULLABLE__ buff_t nyx_memory_alloc(__NYX_ZEROABLE__ size_t size)
Similar to libc malloc except that a memory overflow causes the node to stop.
__NYX_ZEROABLE__ size_t nyx_memory_free(__NYX_NULLABLE__ buff_t buff)
Similar to libc free except that it returns the amount of memory freed.
#define str_t
Alias for char *.
@ NYX_TYPE_DICT
Dict object.
Struct describing a JSON object.