Skip to content

Commit 6741cef

Browse files
committed
Moved to rapidjson
moved to rapidjson and using sax based parser
1 parent 03df8bf commit 6741cef

File tree

9 files changed

+533
-11
lines changed

9 files changed

+533
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ NppJSONViewer/x64
1212
NppJSONViewer/NppJSONViewer.vcxproj.user
1313
*.aps
1414
*.zip
15+
.vs/

NppJSONViewer/JSONDialog.cpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2020
#include "JSONDialog.h"
2121
#include "PluginDefinition.h"
2222
#include "json.h"
23+
#include <sstream>
24+
#include "StopWatch.h"
25+
#include "SaxHandler.h"
26+
#include "rapidjson/reader.h"
2327

28+
using win32::Stopwatch;
2429
extern NppData nppData;
2530

2631
/*
@@ -48,7 +53,7 @@ HTREEITEM JSONDialog::initTree(HWND hWndDlg)
4853
/*
4954
inserts a node in the tree
5055
*/
51-
HTREEITEM JSONDialog::insertToTree(HWND hWndDlg,HTREEITEM parent,char *text)
56+
HTREEITEM JSONDialog::insertToTree(HWND hWndDlg,HTREEITEM parent, const char *text)
5257
{
5358
TV_INSERTSTRUCT tvinsert;
5459
HTREEITEM item = NULL;
@@ -71,11 +76,48 @@ HTREEITEM JSONDialog::insertToTree(HWND hWndDlg,HTREEITEM parent,char *text)
7176
return item;
7277
}
7378

79+
HTREEITEM JSONDialog::insertToTree(HTREEITEM parent, const char *text) {
80+
return this->insertToTree(this->getHSelf(), parent, text);
81+
}
82+
7483
void JSONDialog::setJSON(char* json)
7584
{
7685
curJSON=json;
77-
if(this->isCreated())
78-
drawTree();
86+
if (this->isCreated())
87+
//drawTree();
88+
drawTreeSaxParse();
89+
}
90+
91+
void JSONDialog::populateTreeUsingSax(HWND hWndDlg, HTREEITEM tree_root, char *json) {
92+
Stopwatch sw;
93+
sw.Start();
94+
SaxHandler handler(this, tree_root);
95+
rapidjson::Reader reader;
96+
97+
rapidjson::StringStream ss(json);
98+
if (!reader.Parse<rapidjson::kParseNumbersAsStringsFlag>(ss, handler)) {
99+
::MessageBox(nppData._nppHandle, TEXT("Could not parse!!"), TEXT("JSON Viewer"), MB_OK | MB_ICONERROR);
100+
101+
//mark the error position
102+
// Get the current scintilla
103+
int which = -1;
104+
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
105+
if (which == -1)
106+
return;
107+
108+
HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
109+
size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
110+
111+
size_t errPosition = start + reader.GetErrorOffset();
112+
::SendMessage(curScintilla, SCI_SETSEL, errPosition, errPosition + 1);
113+
}
114+
115+
sw.Stop();
116+
long long elapsed = sw.ElapsedMilliseconds();
117+
std::wstringstream s;
118+
s << "tree_time:" << elapsed << " ms";
119+
120+
//::MessageBox(nppData._nppHandle, s.str().c_str(), TEXT("JSON Viewer"), MB_OK);
79121
}
80122

81123
void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_root, int level)
@@ -273,7 +315,7 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
273315
newItem=insertToTree(hWndDlg,tree_root,"null");
274316
break;
275317
}
276-
318+
// DFS
277319
if (json_root->child != NULL)
278320
{
279321
json_t *ita;
@@ -290,6 +332,28 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
290332
}
291333
}
292334

335+
/*
336+
parses curJSON and draws the tree.
337+
marks the error location in case of a parsing error
338+
*/
339+
void JSONDialog::drawTreeSaxParse()
340+
{
341+
HTREEITEM tree_root;
342+
tree_root = initTree(this->getHSelf());
343+
344+
if (strlen(curJSON) == 0) {
345+
insertToTree(this->getHSelf(), tree_root, "Error:Please select a JSON String.");
346+
TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE1), tree_root, TVE_EXPAND);
347+
return;
348+
}
349+
350+
Stopwatch sw;
351+
sw.Start();
352+
populateTreeUsingSax(this->getHSelf(), tree_root, curJSON);
353+
TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE1), tree_root, TVE_EXPAND);
354+
}
355+
356+
293357
/*
294358
parses curJSON and draws the tree.
295359
marks the error location in case of a parsing error
@@ -316,9 +380,14 @@ void JSONDialog::drawTree()
316380
}
317381

318382
json_jpi_init (jpi);
319-
383+
Stopwatch sw;
384+
sw.Start();
320385
err = json_parse_fragment (jpi, curJSON);
386+
sw.Stop();
387+
long long parse_time = sw.ElapsedMilliseconds();
321388

389+
sw.Reset();
390+
sw.Start();
322391
if((err == JSON_WAITING_FOR_EOF) || (err == JSON_OK))
323392
{
324393
populateTree(this->getHSelf(),tree_root,jpi->cursor,0);
@@ -345,6 +414,14 @@ void JSONDialog::drawTree()
345414

346415
free(jpi);
347416
}
417+
sw.Stop();
418+
long long tree_time = sw.ElapsedMilliseconds();
419+
std::wstringstream s;
420+
s << "parse_time:" << parse_time<<" ms, tree_time:"<<tree_time<<" ms";
421+
422+
::MessageBox(nppData._nppHandle, s.str().c_str(), TEXT("JSON Viewer"), MB_OK | MB_ICONERROR);
423+
424+
348425
}
349426

350427
INT_PTR CALLBACK JSONDialog::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)

NppJSONViewer/JSONDialog.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ class JSONDialog : public DockingDlgInterface
3131
char *curJSON;
3232
HANDLE hTree;
3333
void drawTree();
34+
void drawTreeSaxParse();
3435
HTREEITEM initTree(HWND hWndDlg);
35-
HTREEITEM insertToTree(HWND hWndDlg,HTREEITEM parent,char *text);
36+
HTREEITEM insertToTree(HWND hWndDlg,HTREEITEM parent,const char *text);
3637
void populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_root, int level);
38+
void populateTreeUsingSax(HWND hWndDlg, HTREEITEM tree_root, char * json);
3739
public :
3840
JSONDialog() : DockingDlgInterface(IDD_TREE){};
3941

42+
HTREEITEM insertToTree(HTREEITEM parent, const char *text);
43+
4044
virtual void display(bool toShow = true) const {
4145
DockingDlgInterface::display(toShow);
4246
};

NppJSONViewer/NPPJSONViewer.vcxproj

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug|Win32">
55
<Configuration>Debug</Configuration>
@@ -27,22 +27,22 @@
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2828
<ConfigurationType>DynamicLibrary</ConfigurationType>
2929
<CharacterSet>Unicode</CharacterSet>
30-
<PlatformToolset>v120_xp</PlatformToolset>
30+
<PlatformToolset>v141_xp</PlatformToolset>
3131
</PropertyGroup>
3232
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3333
<ConfigurationType>DynamicLibrary</ConfigurationType>
3434
<CharacterSet>Unicode</CharacterSet>
35-
<PlatformToolset>v120_xp</PlatformToolset>
35+
<PlatformToolset>v141_xp</PlatformToolset>
3636
</PropertyGroup>
3737
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
3838
<ConfigurationType>DynamicLibrary</ConfigurationType>
3939
<CharacterSet>Unicode</CharacterSet>
40-
<PlatformToolset>v120_xp</PlatformToolset>
40+
<PlatformToolset>v141_xp</PlatformToolset>
4141
</PropertyGroup>
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4343
<ConfigurationType>DynamicLibrary</ConfigurationType>
4444
<CharacterSet>Unicode</CharacterSet>
45-
<PlatformToolset>v120_xp</PlatformToolset>
45+
<PlatformToolset>v141_xp</PlatformToolset>
4646
</PropertyGroup>
4747
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
4848
<ImportGroup Label="ExtensionSettings">
@@ -75,6 +75,18 @@
7575
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
7676
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
7777
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
79+
<IncludePath>C:\Users\kapil\source\repos\JSON-Viewer\thirdparty\rapidjson\include;$(IncludePath)</IncludePath>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
82+
<IncludePath>C:\Users\kapil\source\repos\JSON-Viewer\thirdparty\rapidjson\include;$(IncludePath)</IncludePath>
83+
</PropertyGroup>
84+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
85+
<IncludePath>C:\Users\kapil\source\repos\JSON-Viewer\thirdparty\rapidjson\include;$(IncludePath)</IncludePath>
86+
</PropertyGroup>
87+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
88+
<IncludePath>C:\Users\kapil\source\repos\JSON-Viewer\thirdparty\rapidjson\include;$(IncludePath)</IncludePath>
89+
</PropertyGroup>
7890
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7991
<ClCompile>
8092
<Optimization>Disabled</Optimization>
@@ -163,6 +175,7 @@
163175
<ClCompile Include="JSONDialog.cpp" />
164176
<ClCompile Include="NppPlugin.cpp" />
165177
<ClCompile Include="PluginDefinition.cpp" />
178+
<ClCompile Include="SaxHandler.cpp" />
166179
<ClCompile Include="StaticDialog.cpp" />
167180
</ItemGroup>
168181
<ItemGroup>
@@ -177,8 +190,11 @@
177190
<ClInclude Include="PluginDefinition.h" />
178191
<ClInclude Include="PluginInterface.h" />
179192
<ClInclude Include="resource.h" />
193+
<ClInclude Include="SaxHandler.h" />
180194
<ClInclude Include="Scintilla.h" />
181195
<ClInclude Include="StaticDialog.h" />
196+
<ClInclude Include="StopWatch.h" />
197+
<ClInclude Include="utils.h" />
182198
<ClInclude Include="Window.h" />
183199
</ItemGroup>
184200
<ItemGroup>

NppJSONViewer/NPPJSONViewer.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<ClCompile Include="StaticDialog.cpp">
3434
<Filter>Source Files</Filter>
3535
</ClCompile>
36+
<ClCompile Include="SaxHandler.cpp">
37+
<Filter>Source Files</Filter>
38+
</ClCompile>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<ClInclude Include="Docking.h">
@@ -77,6 +80,15 @@
7780
<ClInclude Include="resource.h">
7881
<Filter>Header Files</Filter>
7982
</ClInclude>
83+
<ClInclude Include="StopWatch.h">
84+
<Filter>Header Files</Filter>
85+
</ClInclude>
86+
<ClInclude Include="SaxHandler.h">
87+
<Filter>Header Files</Filter>
88+
</ClInclude>
89+
<ClInclude Include="utils.h">
90+
<Filter>Header Files</Filter>
91+
</ClInclude>
8092
</ItemGroup>
8193
<ItemGroup>
8294
<ResourceCompile Include="resource.rc">

0 commit comments

Comments
 (0)