json-tree.c revision fcd3d6214ce1b8169b6481c78e02d9054901fed3
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch/* Copyright (c) 2015-2016 Dovecot authors, see the included COPYING file */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct json_tree_node *root, *cur, *cur_child;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch pool = pool_alloconly_create("json tree", 1024);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch tree->root = tree->cur = p_new(tree->pool, struct json_tree_node, 1);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch tree->cur->value_type = container == JSON_TYPE_ARRAY ? container : JSON_TYPE_OBJECT;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschvoid json_tree_deinit(struct json_tree **_tree)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschjson_tree_append_child(struct json_tree *tree, enum json_type type,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch node = p_new(tree->pool, struct json_tree_node, 1);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch node->value.str = p_strdup(tree->pool, value);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschjson_tree_set_cur(struct json_tree *tree, struct json_tree_node *node)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschjson_tree_append_value(struct json_tree *tree, enum json_type type,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* "key": value - we already added the node and set its key,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch so now just set the value */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch tree->cur->value.str = p_strdup(tree->pool, value);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* element in array - add a new node */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschint json_tree_append(struct json_tree *tree, enum json_type type,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (tree->cur->value_type != JSON_TYPE_OBJECT)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (json_tree_append_value(tree, type, NULL) < 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (tree->cur->value_type == JSON_TYPE_OBJECT_KEY)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch else if (tree->cur->value_type == JSON_TYPE_ARRAY) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (json_tree_append_value(tree, type, value) < 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschjson_tree_find_key(const struct json_tree_node *node, const char *key)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_assert(node->value_type == JSON_TYPE_OBJECT);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (node->key != NULL && strcmp(node->key, key) == 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschjson_tree_find_child_with(const struct json_tree_node *node,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_assert(node->value_type == JSON_TYPE_OBJECT ||
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch for (node = json_tree_get_child(node); node != NULL; node = node->next) {