Skip to content

Commit f323904

Browse files
authored
[cleanup] cleanup Json interface (#3856)
1 parent c5636c6 commit f323904

File tree

6 files changed

+61
-156
lines changed

6 files changed

+61
-156
lines changed

CodeLite/JSON.cpp

Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include "JSON.h"
2727

28-
#include "StringUtils.h"
2928
#include "clFontHelper.h"
3029
#include "fileutils.h"
3130

@@ -37,11 +36,6 @@ JSON::JSON(const wxString& text)
3736
m_json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
3837
}
3938

40-
JSON::JSON(cJSON* json)
41-
: m_json(json)
42-
{
43-
}
44-
4539
JSON::JSON(JSONItem item)
4640
: m_json(item.release())
4741
{
@@ -95,8 +89,6 @@ JSONItem JSON::toElement() const
9589
return JSONItem(m_json);
9690
}
9791

98-
wxString JSON::errorString() const { return _errorString; }
99-
10092
JSONItem JSONItem::namedObject(const wxString& name) const
10193
{
10294
if (!m_json) {
@@ -142,33 +134,6 @@ JSONItem::JSONItem(cJSON* json)
142134
}
143135
}
144136

145-
JSONItem::JSONItem(const wxString& name, double val)
146-
: m_propertyName(name)
147-
, m_type(cJSON_Number)
148-
, m_valueNumer(val)
149-
{
150-
}
151-
152-
JSONItem::JSONItem(const wxString& name, const std::string& val)
153-
: m_propertyName(name)
154-
, m_type(cJSON_String)
155-
, m_valueString(val)
156-
{
157-
}
158-
159-
JSONItem::JSONItem(const wxString& name, const char* pval, size_t len)
160-
: m_propertyName(name)
161-
, m_type(cJSON_String)
162-
, m_valueString(pval, len)
163-
{
164-
}
165-
166-
JSONItem::JSONItem(const wxString& name, bool val)
167-
: m_propertyName(name)
168-
, m_type(val ? cJSON_True : cJSON_False)
169-
{
170-
}
171-
172137
JSONItem JSONItem::operator[](int index) const
173138
{
174139
if (isArray()) {
@@ -471,20 +436,26 @@ int JSONItem::arraySize() const
471436

472437
JSONItem& JSONItem::addProperty(const wxString& name, bool value)
473438
{
474-
append(JSONItem(name, value));
439+
if (m_json) {
440+
if (value) {
441+
cJSON_AddTrueToObject(m_json, name.mb_str(wxConvUTF8).data());
442+
} else {
443+
cJSON_AddFalseToObject(m_json, name.mb_str(wxConvUTF8).data());
444+
}
445+
}
475446
return *this;
476447
}
477448

478449
JSONItem& JSONItem::addProperty(const wxString& name, const wxString& value)
479450
{
480-
const std::string cstr = value.ToStdString(wxConvUTF8);
481-
append(JSONItem(name, cstr.data(), cstr.length()));
482-
return *this;
451+
return addProperty(name, value.ToStdString(wxConvUTF8));
483452
}
484453

485454
JSONItem& JSONItem::addProperty(const wxString& name, const std::string& value)
486455
{
487-
append(JSONItem(name, value));
456+
if (m_json) {
457+
cJSON_AddStringToObject(m_json, name.mb_str(wxConvUTF8).data(), value.data());
458+
}
488459
return *this;
489460
}
490461

@@ -495,7 +466,9 @@ JSONItem& JSONItem::addProperty(const wxString& name, const wxChar* value)
495466

496467
JSONItem& JSONItem::addProperty(const wxString& name, long value)
497468
{
498-
append(JSONItem(name, (double)value));
469+
if (m_json) {
470+
cJSON_AddNumberToObject(m_json, name.mb_str(wxConvUTF8).data(), value);
471+
}
499472
return *this;
500473
}
501474

@@ -659,31 +632,7 @@ JSONItem& JSONItem::addProperty(const wxString& name, const wxColour& colour)
659632
return addProperty(name, colourValue);
660633
}
661634
#endif
662-
JSONItem JSONItem::firstChild()
663-
{
664-
m_walker = NULL;
665-
if (!m_json) {
666-
return JSONItem(NULL);
667-
}
668-
669-
if (!m_json->child) {
670-
return JSONItem(NULL);
671-
}
672-
673-
m_walker = m_json->child;
674-
return JSONItem(m_walker);
675-
}
676-
677-
JSONItem JSONItem::nextChild()
678-
{
679-
if (!m_walker) {
680-
return JSONItem(NULL);
681-
}
682635

683-
JSONItem element(m_walker->next);
684-
m_walker = m_walker->next;
685-
return element;
686-
}
687636
JSONItem& JSONItem::addProperty(const wxString& name, const char* value, const wxMBConv& conv)
688637
{
689638
return addProperty(name, wxString(value, conv));
@@ -729,23 +678,6 @@ bool JSONItem::isNumber() const
729678
return m_json->type == cJSON_Number;
730679
}
731680

732-
JSONItem JSONItem::detachProperty(const wxString& name)
733-
{
734-
if (!m_json) {
735-
return JSONItem(NULL);
736-
}
737-
cJSON* j = cJSON_DetachItemFromObject(m_json, name.c_str());
738-
return JSONItem(j);
739-
}
740-
741-
wxFileName JSONItem::toFileName() const
742-
{
743-
if (!m_json) {
744-
return wxFileName();
745-
}
746-
return wxFileName(m_valueString);
747-
}
748-
749681
JSONItem& JSONItem::addProperty(const wxString& name, const wxFileName& filename)
750682
{
751683
return addProperty(name, filename.GetFullPath());
@@ -765,12 +697,12 @@ JSONItem JSONItem::AddObject(const wxString& name)
765697
return json;
766698
}
767699

768-
JSONItem& JSONItem::addProperty(const wxString& name, cJSON* pjson)
700+
JSONItem& JSONItem::addProperty(const wxString& name, JSON&& json)
769701
{
770702
if (!m_json) {
771703
return *this;
772704
}
773-
cJSON_AddItemToObject(m_json, name.mb_str(wxConvUTF8).data(), pjson);
705+
cJSON_AddItemToObject(m_json, name.mb_str(wxConvUTF8).data(), json.release());
774706
return *this;
775707
}
776708

CodeLite/JSON.h

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,40 @@
2525

2626
#ifndef ZJSONNODE_H
2727
#define ZJSONNODE_H
28-
// clang-format off
29-
#include <wx/vector.h>
30-
#include <wx/string.h>
31-
#include <wx/variant.h>
32-
#include <wx/filename.h>
33-
#include <string_view>
34-
#include <wx/gdicmn.h>
28+
3529
#include "codelite_exports.h"
36-
#include <map>
30+
#include "macros.h"
31+
3732
#include <cJSON.h>
33+
#include <map>
34+
#include <string_view>
35+
#include <type_traits>
36+
#include <vector>
37+
#include <wx/filename.h>
38+
#include <wx/gdicmn.h>
39+
#include <wx/string.h>
40+
#include <wx/variant.h>
41+
#include <wx/vector.h>
42+
3843
#if wxUSE_GUI
3944
#include <wx/arrstr.h>
4045
#include <wx/colour.h>
4146
#include <wx/font.h>
4247
#endif
43-
#include "macros.h"
44-
#include <vector>
45-
#include <type_traits>
46-
// clang-format on
4748

4849
//////////////////////////////////////////////////////////////////////////
4950
//////////////////////////////////////////////////////////////////////////
5051

52+
class JSON;
53+
5154
class WXDLLIMPEXP_CL JSONItem
5255
{
53-
protected:
54-
cJSON* m_json = nullptr;
55-
cJSON* m_walker = nullptr;
56-
wxString m_propertyName;
57-
int m_type = wxNOT_FOUND;
58-
59-
// Values
60-
wxString m_valueString;
61-
double m_valueNumer = 0;
62-
56+
friend JSON;
6357
public:
64-
JSONItem(cJSON* json);
65-
JSONItem(const wxString& name, double val);
66-
JSONItem(const wxString& name, const std::string& val);
67-
JSONItem(const wxString& name, const char* pval, size_t len);
68-
JSONItem(const wxString& name, bool val);
58+
explicit JSONItem(cJSON* json);
6959
JSONItem() = default;
7060
virtual ~JSONItem() = default;
7161

72-
// Walkers
73-
JSONItem firstChild();
74-
JSONItem nextChild();
75-
76-
// Setters
77-
////////////////////////////////////////////////
78-
void setType(int m_type) { this->m_type = m_type; }
79-
int getType() const { return m_type; }
80-
const wxString& GetPropertyName() const { return m_propertyName; }
81-
void SetPropertyName(const wxString& name) { m_propertyName = name; }
82-
8362
// Readers
8463
////////////////////////////////////////////////
8564
JSONItem namedObject(const wxString& name) const;
@@ -154,7 +133,6 @@ class WXDLLIMPEXP_CL JSONItem
154133
}
155134
size_t toSize_t(size_t defaultVal = 0) const;
156135
double toDouble(double defaultVal = -1.0) const;
157-
wxFileName toFileName() const;
158136

159137
wxColour toColour(const wxColour& defaultColour = wxNullColour) const;
160138
wxFont toFont(const wxFont& defaultFont = wxNullFont) const;
@@ -198,7 +176,7 @@ class WXDLLIMPEXP_CL JSONItem
198176
JSONItem& addProperty(const wxString& name, long value);
199177
JSONItem& addProperty(const wxString& name, size_t value);
200178
JSONItem& addProperty(const wxString& name, bool value);
201-
JSONItem& addProperty(const wxString& name, cJSON* pjson);
179+
JSONItem& addProperty(const wxString& name, JSON&& json);
202180
JSONItem& addProperty(const wxString& name, const wxFileName& filename);
203181
JSONItem& addProperty(const wxString& name, const std::vector<int>& arr_int);
204182
template <class T = int>
@@ -223,11 +201,6 @@ class WXDLLIMPEXP_CL JSONItem
223201
*/
224202
void removeProperty(const wxString& name);
225203

226-
/**
227-
* @brief detach element from json. Return the detached element
228-
*/
229-
JSONItem detachProperty(const wxString& name);
230-
231204
//////////////////////////////////////////////////
232205
// Array operations
233206
//////////////////////////////////////////////////
@@ -245,6 +218,13 @@ class WXDLLIMPEXP_CL JSONItem
245218

246219
bool isOk() const { return m_json != NULL; }
247220

221+
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+
248228
/**
249229
* @brief release the internal pointer
250230
*/
@@ -254,6 +234,14 @@ class WXDLLIMPEXP_CL JSONItem
254234
m_json = nullptr;
255235
return temp;
256236
}
237+
private:
238+
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;
257245
};
258246

259247
//////////////////////////////////////////////////////////////////////////
@@ -267,32 +255,27 @@ enum class JsonType
267255

268256
class WXDLLIMPEXP_CL JSON
269257
{
270-
protected:
271-
cJSON* m_json;
272-
wxString _errorString;
273-
274258
public:
275259
explicit JSON(JsonType type);
276260
explicit JSON(const wxString& text);
277261
explicit JSON(const wxFileName& filename);
278262
explicit JSON(JSONItem item);
279-
explicit JSON(cJSON* json);
280263

264+
// Make this class not copyable
265+
JSON(const JSON&) = delete;
266+
JSON& operator=(const JSON&) = delete;
281267
virtual ~JSON();
282268

283269
void save(const wxFileName& fn) const;
284-
wxString errorString() const;
285270
bool isOk() const { return m_json != NULL; }
286271

287272
JSONItem toElement() const;
288273

289274
void clear();
290275
cJSON* release();
291276

292-
private:
293-
// Make this class not copyable
294-
JSON(const JSON&) = delete;
295-
JSON& operator=(const JSON&) = delete;
277+
protected:
278+
cJSON* m_json = nullptr;
296279
};
297280

298281
#endif // ZJSONNODE_H

CodeLite/LSP/InitializeRequest.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,15 @@ JSONItem LSP::InitializeRequest::ToJSON(const wxString& name) const
1616
JSONItem params = JSONItem::createObject("params");
1717
json.append(params);
1818
params.addProperty("processId", GetProcessId());
19-
if (GetRootUri().IsEmpty()) {
20-
JSON nullItem(JsonType::Null);
21-
JSONItem nullObj = nullItem.toElement();
22-
params.append(nullObj);
23-
(void)nullItem.release(); // don't delete it on destruction, it is now owned by 'params'
24-
} else {
19+
if (!GetRootUri().IsEmpty()) {
2520
params.addProperty("rootUri", LSP::FileNameToURI(GetRootUri()));
2621
}
2722

2823
if (!m_initOptions.empty()) {
2924
// Parse the JSON string and set it as the 'initializationOptions
3025
JSON initializationOptions(m_initOptions);
3126
if (initializationOptions.isOk()) {
32-
cJSON* pjson = initializationOptions.release();
33-
params.addProperty(wxString("initializationOptions"), (cJSON*)pjson);
27+
params.addProperty(wxString("initializationOptions"), std::move(initializationOptions));
3428
}
3529
}
3630

CodeLite/LSP/json_rpc_params.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ JSONItem ExecuteCommandParams::ToJSON(const wxString& name) const
139139
// and add them
140140
JSON root{ m_arguments };
141141
if(root.isOk()) {
142-
json.addProperty("arguments", root.release());
142+
json.addProperty("arguments", std::move(root));
143143
}
144144
return json;
145145
}

CodeLite/cl_config.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ bool clConfig::Write(const wxString& name, std::function<JSONItem()> serialiser_
173173
} else {
174174
// add it to the global configuration file
175175
DoDeleteProperty(name);
176-
item.SetPropertyName(name);
177-
m_root->toElement().append(item);
176+
m_root->toElement().addProperty(name, item);
178177
return true;
179178
}
180179
}

0 commit comments

Comments
 (0)