bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen struct json_tree_node *root, *cur, *cur_child;
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen pool = pool_alloconly_create("json tree", 1024);
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen tree->root = tree->cur = p_new(tree->pool, struct json_tree_node, 1);
fcd3d6214ce1b8169b6481c78e02d9054901fed3Phil Carmody tree->cur->value_type = container == JSON_TYPE_ARRAY ? container : JSON_TYPE_OBJECT;
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainenvoid json_tree_deinit(struct json_tree **_tree)
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainenjson_tree_append_child(struct json_tree *tree, enum json_type type,
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen node = p_new(tree->pool, struct json_tree_node, 1);
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen node->value.str = p_strdup(tree->pool, value);
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainenjson_tree_set_cur(struct json_tree *tree, struct json_tree_node *node)
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainenjson_tree_append_value(struct json_tree *tree, enum json_type type,
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen /* "key": value - we already added the node and set its key,
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen so now just set the value */
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen tree->cur->value.str = p_strdup(tree->pool, value);
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen /* element in array - add a new node */
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainenint json_tree_append(struct json_tree *tree, enum json_type type,
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen if (tree->cur->value_type != JSON_TYPE_OBJECT)
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen if (json_tree_append_value(tree, type, NULL) < 0)
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen if (tree->cur->value_type == JSON_TYPE_OBJECT_KEY)
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen else if (tree->cur->value_type == JSON_TYPE_ARRAY) {
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen if (json_tree_append_value(tree, type, value) < 0)
fcd3d6214ce1b8169b6481c78e02d9054901fed3Phil Carmodyjson_tree_find_key(const struct json_tree_node *node, const char *key)
fcd3d6214ce1b8169b6481c78e02d9054901fed3Phil Carmody i_assert(node->value_type == JSON_TYPE_OBJECT);
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen if (node->key != NULL && strcmp(node->key, key) == 0)
fcd3d6214ce1b8169b6481c78e02d9054901fed3Phil Carmodyjson_tree_find_child_with(const struct json_tree_node *node,
31e6dbee5fc7d6c33b44c75c7e18ea576f44184bTimo Sirainen i_assert(node->value_type == JSON_TYPE_OBJECT ||
fcd3d6214ce1b8169b6481c78e02d9054901fed3Phil Carmody for (node = json_tree_get_child(node); node != NULL; node = node->next) {