Skip to content

Commit c594538

Browse files
authored
[cleanup] JSon (#3874)
1 parent 46458b1 commit c594538

File tree

71 files changed

+259
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+259
-351
lines changed

AutoSave/AutoSaveSettings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void AutoSaveSettings::FromJSON(const JSONItem& json)
1515

1616
JSONItem AutoSaveSettings::ToJSON() const
1717
{
18-
JSONItem json = JSONItem::createObject(GetName());
18+
JSONItem json = JSONItem::createObject();
1919
json.addProperty("m_flags", m_flags);
2020
json.addProperty("m_checkInterval", m_checkInterval);
2121
return json;

CodeFormatter/SourceFormatterBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void SourceFormatterBase::FromJSON(const JSONItem& json)
1717

1818
JSONItem SourceFormatterBase::ToJSON() const
1919
{
20-
JSONItem ele = JSONItem::createObject(GetName());
20+
JSONItem ele = JSONItem::createObject();
2121
ele.addProperty("languages", m_languages);
2222
ele.addProperty("flags", m_flags);
2323
ele.addProperty("config_file", m_configFile);

CodeLite/JSON.cpp

Lines changed: 8 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ cJSON* JSON::release()
128128
JSONItem::JSONItem(cJSON* json)
129129
: m_json(json)
130130
{
131-
if (m_json) {
132-
m_propertyName = m_json->string ? m_json->string : "";
133-
m_type = m_json->type;
134-
}
135131
}
136132

137133
JSONItem JSONItem::operator[](int index) const
@@ -244,42 +240,6 @@ bool JSONItem::isString() const
244240
return m_json->type == cJSON_String;
245241
}
246242

247-
void JSONItem::append(const JSONItem& element)
248-
{
249-
if (!m_json) {
250-
return;
251-
}
252-
253-
switch (element.getType()) {
254-
case cJSON_False:
255-
cJSON_AddFalseToObject(m_json, element.GetPropertyName().mb_str(wxConvUTF8).data());
256-
break;
257-
258-
case cJSON_True:
259-
cJSON_AddTrueToObject(m_json, element.GetPropertyName().mb_str(wxConvUTF8).data());
260-
break;
261-
262-
case cJSON_NULL:
263-
cJSON_AddNullToObject(m_json, element.GetPropertyName().mb_str(wxConvUTF8).data());
264-
break;
265-
266-
case cJSON_Number:
267-
cJSON_AddNumberToObject(m_json, element.GetPropertyName().mb_str(wxConvUTF8).data(), element.m_valueNumer);
268-
break;
269-
270-
case cJSON_String:
271-
cJSON_AddStringToObject(m_json,
272-
element.GetPropertyName().mb_str(wxConvUTF8).data(),
273-
element.m_valueString.mb_str(wxConvUTF8).data());
274-
break;
275-
276-
case cJSON_Array:
277-
case cJSON_Object:
278-
cJSON_AddItemToObject(m_json, element.GetPropertyName().mb_str(wxConvUTF8).data(), element.m_json);
279-
break;
280-
}
281-
}
282-
283243
void JSONItem::arrayAppend(const char* value)
284244
{
285245
if (!m_json) {
@@ -304,55 +264,25 @@ void JSONItem::arrayAppend(int number) { arrayAppend((double)number); }
304264

305265
void JSONItem::arrayAppend(const wxString& value) { arrayAppend((const char*)value.mb_str(wxConvUTF8).data()); }
306266

307-
void JSONItem::arrayAppend(const JSONItem& element)
267+
void JSONItem::arrayAppend(JSONItem&& element)
308268
{
309269
if (!m_json) {
310270
return;
311271
}
312-
313-
cJSON* p = NULL;
314-
switch (element.getType()) {
315-
case cJSON_False:
316-
p = cJSON_CreateFalse();
317-
break;
318-
319-
case cJSON_True:
320-
p = cJSON_CreateTrue();
321-
break;
322-
323-
case cJSON_NULL:
324-
p = cJSON_CreateNull();
325-
break;
326-
327-
case cJSON_Number:
328-
p = cJSON_CreateNumber(element.m_valueNumer);
329-
break;
330-
331-
case cJSON_String:
332-
p = cJSON_CreateString(element.m_valueString.mb_str(wxConvUTF8).data());
333-
break;
334-
case cJSON_Array:
335-
case cJSON_Object:
336-
p = element.m_json;
337-
break;
338-
}
339-
if (p) {
340-
cJSON_AddItemToArray(m_json, p);
272+
if (element.m_json) {
273+
cJSON_AddItemToArray(m_json, element.m_json);
341274
}
342275
}
343276

344277
JSONItem JSONItem::createArray()
345278
{
346279
JSONItem arr(cJSON_CreateArray());
347-
arr.setType(cJSON_Array);
348280
return arr;
349281
}
350282

351-
JSONItem JSONItem::createObject(const wxString& name)
283+
JSONItem JSONItem::createObject()
352284
{
353285
JSONItem obj(cJSON_CreateObject());
354-
obj.SetPropertyName(name);
355-
obj.setType(cJSON_Object);
356286
return obj;
357287
}
358288

@@ -598,7 +528,7 @@ JSONItem& JSONItem::addProperty(const wxString& name, const wxStringMap_t& strin
598528
JSONItem obj = JSONItem::createObject();
599529
obj.addProperty("key", key);
600530
obj.addProperty("value", value);
601-
arr.arrayAppend(obj);
531+
arr.arrayAppend(std::move(obj));
602532
}
603533
addProperty(name, arr);
604534
return *this;
@@ -691,8 +621,8 @@ JSONItem JSONItem::AddArray(const wxString& name)
691621

692622
JSONItem JSONItem::AddObject(const wxString& name)
693623
{
694-
JSONItem json = createObject(name);
695-
append(json);
624+
JSONItem json = createObject();
625+
addProperty(name, json);
696626
return json;
697627
}
698628

@@ -757,11 +687,7 @@ std::vector<int> JSONItem::toIntArray(const std::vector<int>& defaultValue) cons
757687

758688
JSONItem& JSONItem::addProperty(const wxString& name, const std::vector<int>& arr_int)
759689
{
760-
if (!m_json) {
761-
return *this;
762-
}
763-
764-
if (m_type != cJSON_Object) {
690+
if (!m_json || !isObject()) {
765691
return *this;
766692
}
767693

CodeLite/JSON.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class JSON;
5454
class WXDLLIMPEXP_CL JSONItem
5555
{
5656
friend JSON;
57+
5758
public:
5859
explicit JSONItem(cJSON* json);
5960
JSONItem() = default;
@@ -144,10 +145,10 @@ class WXDLLIMPEXP_CL JSONItem
144145
// Writers
145146
////////////////////////////////////////////////
146147
/**
147-
* @brief create new named object and append it to this json element
148+
* @brief create new object
148149
* @return the newly created object
149150
*/
150-
static JSONItem createObject(const wxString& name = wxT(""));
151+
static JSONItem createObject();
151152
/**
152153
* @brief create new array
153154
* @return the newly created array
@@ -164,11 +165,6 @@ class WXDLLIMPEXP_CL JSONItem
164165
*/
165166
JSONItem AddObject(const wxString& name);
166167

167-
/**
168-
* @brief append new element to this json element
169-
*/
170-
void append(const JSONItem& element);
171-
172168
JSONItem& addProperty(const wxString& name, const wxString& value);
173169
JSONItem& addProperty(const wxString& name, const std::string& value);
174170
JSONItem& addProperty(const wxString& name, const wxChar* value);
@@ -209,7 +205,7 @@ class WXDLLIMPEXP_CL JSONItem
209205
* @brief append new number
210206
* @return the newly added property
211207
*/
212-
void arrayAppend(const JSONItem& element);
208+
void arrayAppend(JSONItem&& element);
213209
void arrayAppend(const wxString& value);
214210
void arrayAppend(const char* value);
215211
void arrayAppend(const std::string& value);
@@ -219,12 +215,6 @@ class WXDLLIMPEXP_CL JSONItem
219215
bool isOk() const { return m_json != NULL; }
220216

221217
private:
222-
223-
const wxString& GetPropertyName() const { return m_propertyName; }
224-
void SetPropertyName(const wxString& name) { m_propertyName = name; }
225-
int getType() const { return m_type; }
226-
void setType(int m_type) { this->m_type = m_type; }
227-
228218
/**
229219
* @brief release the internal pointer
230220
*/
@@ -234,20 +224,14 @@ class WXDLLIMPEXP_CL JSONItem
234224
m_json = nullptr;
235225
return temp;
236226
}
227+
237228
private:
238229
cJSON* m_json = nullptr;
239-
wxString m_propertyName;
240-
int m_type = wxNOT_FOUND;
241-
242-
// Values
243-
wxString m_valueString;
244-
double m_valueNumer = 0;
245230
};
246231

247232
//////////////////////////////////////////////////////////////////////////
248233
//////////////////////////////////////////////////////////////////////////
249-
enum class JsonType
250-
{
234+
enum class JsonType {
251235
Array,
252236
Null,
253237
Object,

CodeLite/LSP/CodeActionRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LSP::CodeActionRequest::CodeActionRequest(const LSP::TextDocumentIdentifier& tex
1111
m_params->As<CodeActionParams>()->SetTextDocument(textDocument);
1212
m_params->As<CodeActionParams>()->SetRange(range);
1313
m_params->As<CodeActionParams>()->SetDiagnostics(diags);
14-
LSP_DEBUG() << ToJSON(wxEmptyString).format(true) << endl;
14+
LSP_DEBUG() << ToJSON().format(true) << endl;
1515
}
1616

1717
void LSP::CodeActionRequest::OnResponse(const LSP::ResponseMessage& response, wxEvtHandler* owner)

CodeLite/LSP/CompletionItem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CompletionItem.h"
22

3-
JSONItem LSP::CompletionItem::ToJSON(const wxString& name) const { return JSONItem(NULL); }
3+
JSONItem LSP::CompletionItem::ToJSON() const { return JSONItem(NULL); }
44

55
void LSP::CompletionItem::FromJSON(const JSONItem& json)
66
{

CodeLite/LSP/CompletionItem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class WXDLLIMPEXP_CL CompletionItem : public Serializable
6464

6565
public:
6666
CompletionItem() = default;
67-
virtual ~CompletionItem() = default;
68-
virtual JSONItem ToJSON(const wxString& name) const;
69-
virtual void FromJSON(const JSONItem& json);
67+
~CompletionItem() override = default;
68+
JSONItem ToJSON() const override;
69+
void FromJSON(const JSONItem& json) override;
7070
void SetDetail(const wxString& detail) { this->m_detail = detail; }
7171
void SetDocumentation(const MarkupContent& documentation) { this->m_documentation = documentation; }
7272
void SetFilterText(const wxString& filterText) { this->m_filterText = filterText; }

CodeLite/LSP/InitializeRequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ LSP::InitializeRequest::InitializeRequest(bool withTokenTypes, const wxString& r
88
m_rootUri = rootUri;
99
}
1010

11-
JSONItem LSP::InitializeRequest::ToJSON(const wxString& name) const
11+
JSONItem LSP::InitializeRequest::ToJSON() const
1212
{
13-
JSONItem json = Request::ToJSON(name);
13+
JSONItem json = Request::ToJSON();
1414

1515
// add the 'params'
1616
JSONItem params = JSONItem::createObject();

CodeLite/LSP/InitializeRequest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WXDLLIMPEXP_CL InitializeRequest : public LSP::Request
2727
}
2828
int GetProcessId() const { return m_processId; }
2929
const wxString& GetRootUri() const { return m_rootUri; }
30-
JSONItem ToJSON(const wxString& name) const override;
30+
JSONItem ToJSON() const override;
3131
void OnResponse(const LSP::ResponseMessage& response, wxEvtHandler* owner) override;
3232
bool IsPositionDependentRequest() const override { return false; }
3333
void SetInitOptions(const wxString& initOptions) { this->m_initOptions = initOptions; }

CodeLite/LSP/InitializedNotification.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace LSP
44
{
55
struct InitializedParams : public Params {
6-
JSONItem ToJSON(const wxString& name) const override
6+
JSONItem ToJSON() const override
77
{
8-
wxUnusedVar(name);
9-
return JSONItem::createObject(name);
8+
return JSONItem::createObject();
109
}
1110

1211
void FromJSON(const JSONItem& json) override { wxUnusedVar(json); };

0 commit comments

Comments
 (0)