Skip to content

Commit 2813a7e

Browse files
committed
Handle array for different types
1 parent a561017 commit 2813a7e

File tree

2 files changed

+161
-170
lines changed

2 files changed

+161
-170
lines changed
Lines changed: 158 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,228 +1,216 @@
11
#include "RapidJsonHandler.h"
22
#include "JsonViewDlg.h"
33

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";
77

88

99
bool RapidJsonHandler::Null()
1010
{
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;
2317
}
2418

2519
bool RapidJsonHandler::Bool(bool b)
2620
{
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;
3927
}
4028

4129
bool RapidJsonHandler::Int(int /*i*/)
4230
{
43-
return true;
31+
return true;
4432
}
4533

4634
bool RapidJsonHandler::Uint(unsigned /*i*/)
4735
{
48-
return true;
36+
return true;
4937
}
5038

5139
bool RapidJsonHandler::Int64(int64_t /*i*/)
5240
{
53-
return true;
41+
return true;
5442
}
5543

5644
bool RapidJsonHandler::Uint64(uint64_t /*i*/)
5745
{
58-
return true;
46+
return true;
5947
}
6048

6149
bool RapidJsonHandler::Double(double /*d*/)
6250
{
63-
return true;
51+
return true;
6452
}
6553

66-
bool RapidJsonHandler::RawNumber(const Ch* str, unsigned /*length*/, bool /*copy*/)
54+
bool RapidJsonHandler::RawNumber(const Ch *str, unsigned /*length*/, bool /*copy*/)
6755
{
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;
8062
}
8163

82-
bool RapidJsonHandler::String(const Ch* str, unsigned /*length*/, bool /*copy*/)
64+
bool RapidJsonHandler::String(const Ch *str, unsigned /*length*/, bool /*copy*/)
8365
{
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;
11380
}
11481

115-
bool RapidJsonHandler::Key(const Ch* str, unsigned /*length*/, bool /*copy*/)
82+
bool RapidJsonHandler::Key(const Ch *str, unsigned /*length*/, bool /*copy*/)
11683
{
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;
12188
}
12289

12390
bool RapidJsonHandler::StartObject()
12491
{
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;
163130
}
164131

165132
bool RapidJsonHandler::EndObject(unsigned /*memberCount*/)
166133
{
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;
174141
}
175142

176143
bool RapidJsonHandler::StartArray()
177144
{
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;
216183
}
217184

218185
bool RapidJsonHandler::EndArray(unsigned /*elementCount*/)
219186
{
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;
227194
}
228195

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+
}

NppJSONViewer/NppJsonViewer/RapidJsonHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ class RapidJsonHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>,
4646
bool EndObject(unsigned memberCount);
4747
bool StartArray();
4848
bool EndArray(unsigned elementCount);
49+
50+
private:
51+
void HandleArray(TreeNode *node, const char *const str);
4952
};

0 commit comments

Comments
 (0)