@@ -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;
2429extern NppData nppData;
2530
2631/*
@@ -48,7 +53,7 @@ HTREEITEM JSONDialog::initTree(HWND hWndDlg)
4853/*
4954inserts 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+
7483void 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
81123void 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/*
294358parses curJSON and draws the tree.
295359marks 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
350427INT_PTR CALLBACK JSONDialog::run_dlgProc (UINT message, WPARAM wParam, LPARAM lParam)
0 commit comments