66#include " ScintillaEditor.h"
77#include " Profile.h"
88#include < format>
9+ #include < regex>
910
1011
1112JsonViewDlg::JsonViewDlg (HINSTANCE hIntance, const NppData &nppData, const bool &isReady, int nCmdId, std::shared_ptr<Setting> &pSetting)
@@ -80,6 +81,9 @@ void JsonViewDlg::FormatJson()
8081 }
8182 else
8283 {
84+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::FormatJson, selectedText, res, NULL ))
85+ return ;
86+
8387 ReportError (res);
8488 }
8589}
@@ -94,13 +98,78 @@ void JsonViewDlg::CompressJson()
9498 if (res.success )
9599 {
96100 m_Editor->ReplaceSelection (res.response );
101+ HightlightAsJson ();
97102 }
98103 else
99104 {
105+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::GetCompressedJson, selectedText, res, NULL ))
106+ return ;
107+
100108 ReportError (res);
101109 }
102110}
103111
112+ bool JsonViewDlg::CheckForTokenUndefined (eMethod method, std::string selectedText, Result &res, HTREEITEM tree_root)
113+ {
114+ const auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
115+
116+ if (m_pSetting->parseOptions .bReplaceUndefined )
117+ {
118+ auto text = selectedText.substr (res.error_pos , 9 );
119+ std::transform (
120+ text.begin (),
121+ text.end (),
122+ text.begin (),
123+ [](unsigned char c)
124+ {
125+ return (unsigned char )std::tolower (c);
126+ });
127+ if (text == " undefined" )
128+ {
129+ try
130+ {
131+ std::regex regex (" ([:\\ [,])([\\ s]*?)undefined([\\ s,}]*?)" , std::regex_constants::icase);
132+ text = std::regex_replace (selectedText, regex, " $1$2null" );
133+ switch (method)
134+ {
135+ case eMethod::FormatJson:
136+ res = JsonHandler (m_pSetting->parseOptions ).FormatJson (text, le, lf, indentChar, indentLen);
137+ break ;
138+ case eMethod::GetCompressedJson:
139+ res = JsonHandler (m_pSetting->parseOptions ).GetCompressedJson (text);
140+ break ;
141+ case eMethod::ParseJson:
142+ {
143+ RapidJsonHandler handler (this , tree_root);
144+ rapidjson::StringBuffer sb;
145+ res = JsonHandler (m_pSetting->parseOptions ).ParseJson <flgBaseReader>(text, sb, handler);
146+ break ;
147+ }
148+ case eMethod::ValidateJson:
149+ res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (text);
150+ break ;
151+ }
152+ if (res.success )
153+ {
154+ m_Editor->ReplaceSelection ((method == eMethod::ParseJson || method == eMethod::ValidateJson) ? text : res.response );
155+ HightlightAsJson ();
156+ return true ;
157+ }
158+ else
159+ {
160+ m_Editor->ReplaceSelection (text);
161+ m_Editor->MakeSelection (m_Editor->GetSelectionStart (), static_cast <int >(text.length ()));
162+ m_Editor->RefreshSelectionPos ();
163+ }
164+ }
165+ catch (const std::exception&)
166+ {
167+ }
168+ }
169+ }
170+ return false ;
171+ }
172+
104173void JsonViewDlg::HandleTabActivated ()
105174{
106175 const bool bIsVisible = isCreated () && isVisible ();
@@ -135,6 +204,12 @@ void JsonViewDlg::ValidateJson()
135204 }
136205 else
137206 {
207+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::ValidateJson, selectedText, res, NULL ))
208+ {
209+ ShowMessage (JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
210+ return ;
211+ }
212+
138213 ReportError (res);
139214 }
140215}
@@ -197,6 +272,9 @@ auto JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &j
197272 Result res = JsonHandler (m_pSetting->parseOptions ).ParseJson <flgBaseReader>(jsonText, sb, handler);
198273 if (!res.success )
199274 {
275+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::ParseJson, jsonText, res, tree_root))
276+ return retVal;
277+
200278 // Intimate user
201279 if (jsonText.empty ())
202280 {
@@ -603,7 +681,7 @@ void JsonViewDlg::ReportError(const Result &result)
603681{
604682 // Mark the error position
605683 size_t start = m_Editor->GetSelectionStart () + result.error_pos ;
606- size_t end = start + m_Editor->GetSelectionEnd ();
684+ size_t end = m_Editor->GetSelectionEnd ();
607685 m_Editor->MakeSelection (start, end);
608686
609687 // Intimate user
0 commit comments