Mercurial > dovecot > core-2.2
annotate src/lib/json-tree.c @ 22955:812e5c961328
fts: Indexing virtual mailbox didn't always index the last mails
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 03 May 2018 18:33:00 +0300 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */ |
18399
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "json-tree.h" |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 struct json_tree { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 pool_t pool; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 struct json_tree_node *root, *cur, *cur_child; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 }; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 struct json_tree *json_tree_init(void) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 struct json_tree *tree; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 pool_t pool; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 pool = pool_alloconly_create("json tree", 1024); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 tree = p_new(pool, struct json_tree, 1); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 tree->pool = pool; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 tree->root = tree->cur = p_new(tree->pool, struct json_tree_node, 1); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 tree->cur->value_type = JSON_TYPE_OBJECT; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 return tree; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 void json_tree_deinit(struct json_tree **_tree) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 struct json_tree *tree = *_tree; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 *_tree = NULL; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 pool_unref(&tree->pool); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 static void |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 json_tree_append_child(struct json_tree *tree, enum json_type type, |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 const char *value) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 struct json_tree_node *node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 node = p_new(tree->pool, struct json_tree_node, 1); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 node->parent = tree->cur; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 node->value_type = type; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 node->value.str = p_strdup(tree->pool, value); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 if (tree->cur_child == NULL) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 tree->cur->value.child = node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 else |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 tree->cur_child->next = node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
47 tree->cur_child = node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
48 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 static void |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 json_tree_set_cur(struct json_tree *tree, struct json_tree_node *node) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 tree->cur = node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 tree->cur_child = tree->cur->value.child; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 if (tree->cur_child != NULL) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 while (tree->cur_child->next != NULL) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 tree->cur_child = tree->cur_child->next; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
58 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 static int |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
62 json_tree_append_value(struct json_tree *tree, enum json_type type, |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 const char *value) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 switch (tree->cur->value_type) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 case JSON_TYPE_OBJECT_KEY: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 /* "key": value - we already added the node and set its key, |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 so now just set the value */ |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
69 tree->cur->value_type = type; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
70 tree->cur->value.str = p_strdup(tree->pool, value); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 json_tree_set_cur(tree, tree->cur->parent); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
72 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 case JSON_TYPE_ARRAY: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 /* element in array - add a new node */ |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 json_tree_append_child(tree, type, value); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 default: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
80 return 0; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 int json_tree_append(struct json_tree *tree, enum json_type type, |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 const char *value) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 switch (type) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
87 case JSON_TYPE_OBJECT_KEY: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 if (tree->cur->value_type != JSON_TYPE_OBJECT) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 json_tree_append_child(tree, type, NULL); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 json_tree_set_cur(tree, tree->cur_child); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 tree->cur->key = p_strdup(tree->pool, value); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 case JSON_TYPE_ARRAY: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
95 if (json_tree_append_value(tree, type, NULL) < 0) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
97 json_tree_set_cur(tree, tree->cur_child); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
98 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
99 case JSON_TYPE_OBJECT: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
100 if (tree->cur->value_type == JSON_TYPE_OBJECT_KEY) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
101 tree->cur->value_type = JSON_TYPE_OBJECT; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
102 else if (tree->cur->value_type == JSON_TYPE_ARRAY) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 json_tree_append_child(tree, type, NULL); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
104 json_tree_set_cur(tree, tree->cur_child); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
105 } else { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
106 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
107 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
108 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
109 case JSON_TYPE_OBJECT_END: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
110 if (tree->cur->parent == NULL || |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
111 tree->cur->value_type != JSON_TYPE_OBJECT) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
112 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 json_tree_set_cur(tree, tree->cur->parent); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
114 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
115 case JSON_TYPE_ARRAY_END: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
116 if (tree->cur->parent == NULL || |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 tree->cur->value_type != JSON_TYPE_ARRAY) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
118 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
119 json_tree_set_cur(tree, tree->cur->parent); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
120 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 case JSON_TYPE_STRING: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 case JSON_TYPE_NUMBER: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
123 case JSON_TYPE_TRUE: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
124 case JSON_TYPE_FALSE: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
125 case JSON_TYPE_NULL: |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
126 if (json_tree_append_value(tree, type, value) < 0) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
127 return -1; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
128 break; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
129 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
130 return 0; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
131 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
132 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
133 struct json_tree_node *json_tree_root(struct json_tree *tree) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
134 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
135 return tree->root; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
136 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
137 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
138 struct json_tree_node * |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
139 json_tree_find_key(struct json_tree_node *node, const char *key) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
140 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 for (; node != NULL; node = node->next) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
142 if (node->key != NULL && strcmp(node->key, key) == 0) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
143 return node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
144 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
145 return NULL; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
146 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
147 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
148 struct json_tree_node * |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 json_tree_find_child_with(struct json_tree_node *node, |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 const char *key, const char *value) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
151 { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 struct json_tree_node *child; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
153 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
154 i_assert(node->value_type == JSON_TYPE_OBJECT || |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
155 node->value_type == JSON_TYPE_ARRAY); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
156 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
157 for (node = node->value.child; node != NULL; node = node->next) { |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
158 if (node->value_type != JSON_TYPE_OBJECT) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
159 continue; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
160 |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
161 child = json_tree_find_key(node->value.child, key); |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
162 if (child != NULL && child->value.str != NULL && |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
163 strcmp(child->value.str, value) == 0) |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
164 return node; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
165 } |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
166 return NULL; |
6bde7868cffd
lib: Added json-tree API for parsing JSON input into a tree structure.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
167 } |