@@ -20,7 +20,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
20
#include " JSONDialog.h"
21
21
#include " PluginDefinition.h"
22
22
#include " json.h"
23
+ #include < sstream>
24
+ #include " StopWatch.h"
25
+ #include " SaxHandler.h"
26
+ #include " rapidjson/reader.h"
23
27
28
+ using win32::Stopwatch;
24
29
extern NppData nppData;
25
30
26
31
/*
@@ -48,7 +53,7 @@ HTREEITEM JSONDialog::initTree(HWND hWndDlg)
48
53
/*
49
54
inserts a node in the tree
50
55
*/
51
- HTREEITEM JSONDialog::insertToTree (HWND hWndDlg,HTREEITEM parent,char *text)
56
+ HTREEITEM JSONDialog::insertToTree (HWND hWndDlg,HTREEITEM parent, const char *text)
52
57
{
53
58
TV_INSERTSTRUCT tvinsert;
54
59
HTREEITEM item = NULL ;
@@ -71,11 +76,48 @@ HTREEITEM JSONDialog::insertToTree(HWND hWndDlg,HTREEITEM parent,char *text)
71
76
return item;
72
77
}
73
78
79
+ HTREEITEM JSONDialog::insertToTree (HTREEITEM parent, const char *text) {
80
+ return this ->insertToTree (this ->getHSelf (), parent, text);
81
+ }
82
+
74
83
void JSONDialog::setJSON (char * json)
75
84
{
76
85
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);
79
121
}
80
122
81
123
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_
273
315
newItem=insertToTree (hWndDlg,tree_root," null" );
274
316
break ;
275
317
}
276
-
318
+ // DFS
277
319
if (json_root->child != NULL )
278
320
{
279
321
json_t *ita;
@@ -290,6 +332,28 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
290
332
}
291
333
}
292
334
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
+
293
357
/*
294
358
parses curJSON and draws the tree.
295
359
marks the error location in case of a parsing error
@@ -316,9 +380,14 @@ void JSONDialog::drawTree()
316
380
}
317
381
318
382
json_jpi_init (jpi);
319
-
383
+ Stopwatch sw;
384
+ sw.Start ();
320
385
err = json_parse_fragment (jpi, curJSON);
386
+ sw.Stop ();
387
+ long long parse_time = sw.ElapsedMilliseconds ();
321
388
389
+ sw.Reset ();
390
+ sw.Start ();
322
391
if ((err == JSON_WAITING_FOR_EOF) || (err == JSON_OK))
323
392
{
324
393
populateTree (this ->getHSelf (),tree_root,jpi->cursor ,0 );
@@ -345,6 +414,14 @@ void JSONDialog::drawTree()
345
414
346
415
free (jpi);
347
416
}
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
+
348
425
}
349
426
350
427
INT_PTR CALLBACK JSONDialog::run_dlgProc (UINT message, WPARAM wParam, LPARAM lParam)
0 commit comments