|
1 | 1 | #include "RapidJsonHandler.h" |
2 | 2 | #include "JsonViewDlg.h" |
3 | 3 |
|
4 | | -const char* const STR_NULL = "null"; |
5 | | -const char* const STR_TRUE = "true"; |
6 | | -const char* const STR_FALSE = "false"; |
| 4 | +const char *const STR_NULL = "null"; |
| 5 | +const char *const STR_TRUE = "true"; |
| 6 | +const char *const STR_FALSE = "false"; |
7 | 7 |
|
8 | 8 |
|
9 | 9 | bool RapidJsonHandler::Null() |
10 | 10 | { |
11 | | - if (!m_NodeStack.size()) |
12 | | - return false; |
13 | | - |
14 | | - TreeNode* parent = m_NodeStack.top(); |
15 | | - parent->node.type = JsonNodeType::BOOL; |
16 | | - parent->node.key = m_strLastKey; |
17 | | - parent->node.value = STR_NULL; |
18 | | - m_strLastKey.clear(); |
19 | | - |
20 | | - // Print and pop the value |
21 | | - m_dlg->InsertToTree(parent->subRoot, parent->node.key + " : " + parent->node.value); |
22 | | - return true; |
| 11 | + if (!m_NodeStack.size()) |
| 12 | + return false; |
| 13 | + |
| 14 | + TreeNode *parent = m_NodeStack.top(); |
| 15 | + HandleArray(parent, STR_NULL); |
| 16 | + return true; |
23 | 17 | } |
24 | 18 |
|
25 | 19 | bool RapidJsonHandler::Bool(bool b) |
26 | 20 | { |
27 | | - if (!m_NodeStack.size()) |
28 | | - return false; |
29 | | - |
30 | | - TreeNode* parent = m_NodeStack.top(); |
31 | | - parent->node.type = JsonNodeType::BOOL; |
32 | | - parent->node.key = m_strLastKey; |
33 | | - parent->node.value = b ? STR_TRUE : STR_FALSE; |
34 | | - m_strLastKey.clear(); |
35 | | - |
36 | | - // Print and pop the value |
37 | | - m_dlg->InsertToTree(parent->subRoot, parent->node.key + " : " + parent->node.value); |
38 | | - return true; |
| 21 | + if (!m_NodeStack.size()) |
| 22 | + return false; |
| 23 | + |
| 24 | + TreeNode *parent = m_NodeStack.top(); |
| 25 | + HandleArray(parent, b ? STR_TRUE : STR_FALSE); |
| 26 | + return true; |
39 | 27 | } |
40 | 28 |
|
41 | 29 | bool RapidJsonHandler::Int(int /*i*/) |
42 | 30 | { |
43 | | - return true; |
| 31 | + return true; |
44 | 32 | } |
45 | 33 |
|
46 | 34 | bool RapidJsonHandler::Uint(unsigned /*i*/) |
47 | 35 | { |
48 | | - return true; |
| 36 | + return true; |
49 | 37 | } |
50 | 38 |
|
51 | 39 | bool RapidJsonHandler::Int64(int64_t /*i*/) |
52 | 40 | { |
53 | | - return true; |
| 41 | + return true; |
54 | 42 | } |
55 | 43 |
|
56 | 44 | bool RapidJsonHandler::Uint64(uint64_t /*i*/) |
57 | 45 | { |
58 | | - return true; |
| 46 | + return true; |
59 | 47 | } |
60 | 48 |
|
61 | 49 | bool RapidJsonHandler::Double(double /*d*/) |
62 | 50 | { |
63 | | - return true; |
| 51 | + return true; |
64 | 52 | } |
65 | 53 |
|
66 | | -bool RapidJsonHandler::RawNumber(const Ch* str, unsigned /*length*/, bool /*copy*/) |
| 54 | +bool RapidJsonHandler::RawNumber(const Ch *str, unsigned /*length*/, bool /*copy*/) |
67 | 55 | { |
68 | | - if (!m_NodeStack.size()) |
69 | | - return false; |
70 | | - |
71 | | - TreeNode* parent = m_NodeStack.top(); |
72 | | - parent->node.type = JsonNodeType::NUMBER; |
73 | | - parent->node.key = m_strLastKey; |
74 | | - parent->node.value = str; |
75 | | - m_strLastKey.clear(); |
76 | | - |
77 | | - // Print and pop the value |
78 | | - m_dlg->InsertToTree(parent->subRoot, parent->node.key + " : " + parent->node.value); |
79 | | - return true; |
| 56 | + if (!m_NodeStack.size()) |
| 57 | + return false; |
| 58 | + |
| 59 | + TreeNode *parent = m_NodeStack.top(); |
| 60 | + HandleArray(parent, str); |
| 61 | + return true; |
80 | 62 | } |
81 | 63 |
|
82 | | -bool RapidJsonHandler::String(const Ch* str, unsigned /*length*/, bool /*copy*/) |
| 64 | +bool RapidJsonHandler::String(const Ch *str, unsigned /*length*/, bool /*copy*/) |
83 | 65 | { |
84 | | - if (!str) |
85 | | - return false; |
86 | | - |
87 | | - // handle case, when there is only a value in input |
88 | | - if (m_NodeStack.empty()) |
89 | | - { |
90 | | - m_dlg->InsertToTree(m_treeRoot, str); |
91 | | - return true; |
92 | | - } |
93 | | - |
94 | | - TreeNode* parent = m_NodeStack.top(); |
95 | | - |
96 | | - if (parent->node.type != JsonNodeType::ARRAY) |
97 | | - { |
98 | | - parent->node.key = m_strLastKey; |
99 | | - parent->node.value = str; |
100 | | - m_strLastKey.clear(); |
101 | | - } |
102 | | - else |
103 | | - { |
104 | | - parent->node.key = std::to_string(parent->counter); |
105 | | - parent->node.value = str; |
106 | | - parent->counter++; |
107 | | - } |
108 | | - |
109 | | - // insert |
110 | | - m_dlg->InsertToTree(parent->subRoot, parent->node.key + " : \"" + parent->node.value + "\""); |
111 | | - |
112 | | - return true; |
| 66 | + if (!str) |
| 67 | + return false; |
| 68 | + |
| 69 | + // handle case, when there is only a value in input |
| 70 | + if (m_NodeStack.empty()) |
| 71 | + { |
| 72 | + m_dlg->InsertToTree(m_treeRoot, str); |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + TreeNode *parent = m_NodeStack.top(); |
| 77 | + HandleArray(parent, str); |
| 78 | + |
| 79 | + return true; |
113 | 80 | } |
114 | 81 |
|
115 | | -bool RapidJsonHandler::Key(const Ch* str, unsigned /*length*/, bool /*copy*/) |
| 82 | +bool RapidJsonHandler::Key(const Ch *str, unsigned /*length*/, bool /*copy*/) |
116 | 83 | { |
117 | | - m_strLastKey = "\""; |
118 | | - m_strLastKey += str; |
119 | | - m_strLastKey += "\""; |
120 | | - return true; |
| 84 | + m_strLastKey = "\""; |
| 85 | + m_strLastKey += str; |
| 86 | + m_strLastKey += "\""; |
| 87 | + return true; |
121 | 88 | } |
122 | 89 |
|
123 | 90 | bool RapidJsonHandler::StartObject() |
124 | 91 | { |
125 | | - TreeNode* parent = nullptr; |
126 | | - if (m_NodeStack.empty()) |
127 | | - { |
128 | | - parent = new TreeNode; |
129 | | - parent->node.type = JsonNodeType::OBJECT; |
130 | | - parent->subRoot = m_treeRoot; |
131 | | - parent->counter = 0; |
132 | | - m_NodeStack.push(parent); |
133 | | - } |
134 | | - else |
135 | | - { |
136 | | - parent = m_NodeStack.top(); |
137 | | - } |
138 | | - |
139 | | - if (!m_strLastKey.empty() || parent->node.type == JsonNodeType::ARRAY) |
140 | | - { |
141 | | - HTREEITEM newNode = nullptr; |
142 | | - if (parent->node.type != JsonNodeType::ARRAY) |
143 | | - { |
144 | | - newNode = m_dlg->InsertToTree(parent->subRoot, m_strLastKey); |
145 | | - m_strLastKey.clear(); |
146 | | - } |
147 | | - else |
148 | | - { |
149 | | - // It is an array |
150 | | - std::string arr = "[" + std::to_string(parent->counter) + "]"; |
151 | | - newNode = m_dlg->InsertToTree(parent->subRoot, arr); |
152 | | - } |
153 | | - |
154 | | - parent->counter++; |
155 | | - TreeNode* newTreeNode = new TreeNode; |
156 | | - newTreeNode->node.type = JsonNodeType::OBJECT; |
157 | | - newTreeNode->subRoot = newNode; |
158 | | - newTreeNode->counter = 0; |
159 | | - m_NodeStack.push(newTreeNode); |
160 | | - } |
161 | | - |
162 | | - return true; |
| 92 | + TreeNode *parent = nullptr; |
| 93 | + if (m_NodeStack.empty()) |
| 94 | + { |
| 95 | + parent = new TreeNode; |
| 96 | + parent->node.type = JsonNodeType::OBJECT; |
| 97 | + parent->subRoot = m_treeRoot; |
| 98 | + parent->counter = 0; |
| 99 | + m_NodeStack.push(parent); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + parent = m_NodeStack.top(); |
| 104 | + } |
| 105 | + |
| 106 | + if (!m_strLastKey.empty() || parent->node.type == JsonNodeType::ARRAY) |
| 107 | + { |
| 108 | + HTREEITEM newNode = nullptr; |
| 109 | + if (parent->node.type != JsonNodeType::ARRAY) |
| 110 | + { |
| 111 | + newNode = m_dlg->InsertToTree(parent->subRoot, m_strLastKey); |
| 112 | + m_strLastKey.clear(); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + // It is an array |
| 117 | + std::string arr = "[" + std::to_string(parent->counter) + "]"; |
| 118 | + newNode = m_dlg->InsertToTree(parent->subRoot, arr); |
| 119 | + } |
| 120 | + |
| 121 | + parent->counter++; |
| 122 | + TreeNode *newTreeNode = new TreeNode; |
| 123 | + newTreeNode->node.type = JsonNodeType::OBJECT; |
| 124 | + newTreeNode->subRoot = newNode; |
| 125 | + newTreeNode->counter = 0; |
| 126 | + m_NodeStack.push(newTreeNode); |
| 127 | + } |
| 128 | + |
| 129 | + return true; |
163 | 130 | } |
164 | 131 |
|
165 | 132 | bool RapidJsonHandler::EndObject(unsigned /*memberCount*/) |
166 | 133 | { |
167 | | - if (!m_NodeStack.empty()) |
168 | | - { |
169 | | - TreeNode* node = m_NodeStack.top(); |
170 | | - m_NodeStack.pop(); |
171 | | - delete node; |
172 | | - } |
173 | | - return true; |
| 134 | + if (!m_NodeStack.empty()) |
| 135 | + { |
| 136 | + TreeNode *node = m_NodeStack.top(); |
| 137 | + m_NodeStack.pop(); |
| 138 | + delete node; |
| 139 | + } |
| 140 | + return true; |
174 | 141 | } |
175 | 142 |
|
176 | 143 | bool RapidJsonHandler::StartArray() |
177 | 144 | { |
178 | | - TreeNode* parent = nullptr; |
179 | | - if (m_NodeStack.empty()) |
180 | | - { |
181 | | - parent = new TreeNode; |
182 | | - parent->node.type = JsonNodeType::ARRAY; |
183 | | - parent->subRoot = m_treeRoot; |
184 | | - parent->counter = 0; |
185 | | - m_NodeStack.push(parent); |
186 | | - return true; |
187 | | - } |
188 | | - else |
189 | | - { |
190 | | - parent = m_NodeStack.top(); |
191 | | - } |
192 | | - |
193 | | - if (!m_strLastKey.empty() || parent->node.type == JsonNodeType::ARRAY) |
194 | | - { |
195 | | - HTREEITEM newNode; |
196 | | - if (parent->node.type != JsonNodeType::ARRAY) |
197 | | - { |
198 | | - newNode = m_dlg->InsertToTree(parent->subRoot, m_strLastKey); |
199 | | - m_strLastKey.clear(); |
200 | | - } |
201 | | - else |
202 | | - { |
203 | | - // It is an array |
204 | | - std::string arr = "[" + std::to_string(parent->counter) + "]"; |
205 | | - newNode = m_dlg->InsertToTree(parent->subRoot, arr); |
206 | | - } |
207 | | - |
208 | | - parent->counter++; |
209 | | - TreeNode* newTreeNode = new TreeNode; |
210 | | - newTreeNode->node.type = JsonNodeType::ARRAY; |
211 | | - newTreeNode->subRoot = newNode; |
212 | | - newTreeNode->counter = 0; |
213 | | - m_NodeStack.push(newTreeNode); |
214 | | - } |
215 | | - return true; |
| 145 | + TreeNode *parent = nullptr; |
| 146 | + if (m_NodeStack.empty()) |
| 147 | + { |
| 148 | + parent = new TreeNode; |
| 149 | + parent->node.type = JsonNodeType::ARRAY; |
| 150 | + parent->subRoot = m_treeRoot; |
| 151 | + parent->counter = 0; |
| 152 | + m_NodeStack.push(parent); |
| 153 | + return true; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + parent = m_NodeStack.top(); |
| 158 | + } |
| 159 | + |
| 160 | + if (!m_strLastKey.empty() || parent->node.type == JsonNodeType::ARRAY) |
| 161 | + { |
| 162 | + HTREEITEM newNode; |
| 163 | + if (parent->node.type != JsonNodeType::ARRAY) |
| 164 | + { |
| 165 | + newNode = m_dlg->InsertToTree(parent->subRoot, m_strLastKey); |
| 166 | + m_strLastKey.clear(); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + // It is an array |
| 171 | + std::string arr = "[" + std::to_string(parent->counter) + "]"; |
| 172 | + newNode = m_dlg->InsertToTree(parent->subRoot, arr); |
| 173 | + } |
| 174 | + |
| 175 | + parent->counter++; |
| 176 | + TreeNode *newTreeNode = new TreeNode; |
| 177 | + newTreeNode->node.type = JsonNodeType::ARRAY; |
| 178 | + newTreeNode->subRoot = newNode; |
| 179 | + newTreeNode->counter = 0; |
| 180 | + m_NodeStack.push(newTreeNode); |
| 181 | + } |
| 182 | + return true; |
216 | 183 | } |
217 | 184 |
|
218 | 185 | bool RapidJsonHandler::EndArray(unsigned /*elementCount*/) |
219 | 186 | { |
220 | | - if (!m_NodeStack.empty()) |
221 | | - { |
222 | | - TreeNode* node = m_NodeStack.top(); |
223 | | - m_NodeStack.pop(); |
224 | | - delete node; |
225 | | - } |
226 | | - return true; |
| 187 | + if (!m_NodeStack.empty()) |
| 188 | + { |
| 189 | + TreeNode *node = m_NodeStack.top(); |
| 190 | + m_NodeStack.pop(); |
| 191 | + delete node; |
| 192 | + } |
| 193 | + return true; |
227 | 194 | } |
228 | 195 |
|
| 196 | +void RapidJsonHandler::HandleArray(TreeNode *node, const char *const str) |
| 197 | +{ |
| 198 | + if (!node || !str) |
| 199 | + return; |
| 200 | + |
| 201 | + if (node->node.type != JsonNodeType::ARRAY) |
| 202 | + { |
| 203 | + node->node.key = m_strLastKey; |
| 204 | + node->node.value = str; |
| 205 | + m_strLastKey.clear(); |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + node->node.key = std::to_string(node->counter); |
| 210 | + node->node.value = str; |
| 211 | + node->counter++; |
| 212 | + } |
| 213 | + |
| 214 | + // insert |
| 215 | + m_dlg->InsertToTree(node->subRoot, node->node.key + " : \"" + node->node.value + "\""); |
| 216 | +} |
0 commit comments