Skip to content

Commit 502fd25

Browse files
committed
[cleanup] Remove unused methods from JSON
Hide `cJSON` from public API
1 parent 0c4a5ea commit 502fd25

File tree

4 files changed

+38
-115
lines changed

4 files changed

+38
-115
lines changed

CodeLite/JSON.cpp

Lines changed: 18 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,16 @@
2525

2626
#include "JSON.h"
2727

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

3130
#include <wx/filename.h>
3231

3332
JSON::JSON(const wxString& text)
34-
: m_json(NULL)
3533
{
3634
m_json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
3735
}
3836

39-
JSON::JSON(JSONItem item)
40-
: m_json(item.release())
41-
{
42-
}
43-
4437
JSON::JSON(JsonType type)
45-
: m_json(NULL)
4638
{
4739
if (type == JsonType::Array)
4840
m_json = cJSON_CreateArray();
@@ -53,7 +45,6 @@ JSON::JSON(JsonType type)
5345
}
5446

5547
JSON::JSON(const wxFileName& filename)
56-
: m_json(NULL)
5748
{
5849
wxString content;
5950
if (!FileUtils::ReadFileContent(filename, content)) {
@@ -68,7 +59,7 @@ JSON::~JSON()
6859
{
6960
if (m_json) {
7061
cJSON_Delete(m_json);
71-
m_json = NULL;
62+
m_json = nullptr;
7263
}
7364
}
7465

@@ -84,42 +75,28 @@ void JSON::save(const wxFileName& fn) const
8475
JSONItem JSON::toElement() const
8576
{
8677
if (!m_json) {
87-
return JSONItem(NULL);
78+
return JSONItem(nullptr);
8879
}
8980
return JSONItem(m_json);
9081
}
9182

9283
JSONItem JSONItem::namedObject(const wxString& name) const
9384
{
9485
if (!m_json) {
95-
return JSONItem(NULL);
86+
return JSONItem(nullptr);
9687
}
9788

9889
cJSON* obj = cJSON_GetObjectItem(m_json, name.mb_str(wxConvUTF8).data());
9990
if (!obj) {
100-
return JSONItem(NULL);
91+
return JSONItem(nullptr);
10192
}
10293
return JSONItem(obj);
10394
}
10495

105-
void JSON::clear()
106-
{
107-
int type = cJSON_Object;
108-
if (m_json) {
109-
type = m_json->type;
110-
cJSON_Delete(m_json);
111-
m_json = NULL;
112-
}
113-
if (type == cJSON_Array)
114-
m_json = cJSON_CreateArray();
115-
else
116-
m_json = cJSON_CreateObject();
117-
}
118-
11996
cJSON* JSON::release()
12097
{
12198
cJSON* p = m_json;
122-
m_json = NULL;
99+
m_json = nullptr;
123100
return p;
124101
}
125102

@@ -135,7 +112,7 @@ JSONItem JSONItem::operator[](int index) const
135112
if (isArray()) {
136113
return arrayItem(index);
137114
}
138-
return JSONItem(NULL);
115+
return JSONItem(nullptr);
139116
}
140117

141118
std::unordered_map<std::string_view, JSONItem> JSONItem::GetAsMap() const
@@ -175,15 +152,15 @@ JSONItem JSONItem::operator[](const wxString& name) const { return namedObject(n
175152
JSONItem JSONItem::arrayItem(int pos) const
176153
{
177154
if (!m_json) {
178-
return JSONItem(NULL);
155+
return JSONItem(nullptr);
179156
}
180157

181158
if (m_json->type != cJSON_Array)
182-
return JSONItem(NULL);
159+
return JSONItem(nullptr);
183160

184161
int size = cJSON_GetArraySize(m_json);
185162
if (pos >= size)
186-
return JSONItem(NULL);
163+
return JSONItem(nullptr);
187164

188165
return JSONItem(cJSON_GetArrayItem(m_json, pos));
189166
}
@@ -249,7 +226,7 @@ void JSONItem::arrayAppend(const char* value)
249226
cJSON_AddItemToArray(m_json, p);
250227
}
251228

252-
void JSONItem::arrayAppend(const std::string& value) { arrayAppend((const char*)value.c_str()); }
229+
void JSONItem::arrayAppend(const std::string& value) { arrayAppend(value.c_str()); }
253230

254231
void JSONItem::arrayAppend(double number)
255232
{
@@ -262,7 +239,7 @@ void JSONItem::arrayAppend(double number)
262239

263240
void JSONItem::arrayAppend(int number) { arrayAppend((double)number); }
264241

265-
void JSONItem::arrayAppend(const wxString& value) { arrayAppend((const char*)value.mb_str(wxConvUTF8).data()); }
242+
void JSONItem::arrayAppend(const wxString& value) { arrayAppend(value.mb_str(wxConvUTF8).data()); }
266243

267244
void JSONItem::arrayAppend(JSONItem&& element)
268245
{
@@ -289,7 +266,7 @@ JSONItem JSONItem::createObject()
289266
char* JSONItem::FormatRawString(bool formatted) const
290267
{
291268
if (!m_json) {
292-
return NULL;
269+
return nullptr;
293270
}
294271

295272
if (formatted) {
@@ -443,15 +420,9 @@ bool JSONItem::hasNamedObject(const wxString& name) const
443420
}
444421

445422
cJSON* obj = cJSON_GetObjectItem(m_json, name.mb_str(wxConvUTF8).data());
446-
return obj != NULL;
423+
return obj != nullptr;
447424
}
448425
#if wxUSE_GUI
449-
JSONItem& JSONItem::addProperty(const wxString& name, const wxPoint& pt)
450-
{
451-
wxString szStr;
452-
szStr << pt.x << "," << pt.y;
453-
return addProperty(name, szStr);
454-
}
455426

456427
JSONItem& JSONItem::addProperty(const wxString& name, const wxSize& sz)
457428
{
@@ -460,14 +431,14 @@ JSONItem& JSONItem::addProperty(const wxString& name, const wxSize& sz)
460431
return addProperty(name, szStr);
461432
}
462433

463-
wxPoint JSONItem::toPoint() const
434+
wxSize JSONItem::toSize() const
464435
{
465436
if (!m_json) {
466-
return wxDefaultPosition;
437+
return wxDefaultSize;
467438
}
468439

469440
if (m_json->type != cJSON_String) {
470-
return wxDefaultPosition;
441+
return wxDefaultSize;
471442
}
472443

473444
wxString str = m_json->valuestring;
@@ -476,28 +447,11 @@ wxPoint JSONItem::toPoint() const
476447

477448
long nX(-1), nY(-1);
478449
if (!x.ToLong(&nX) || !y.ToLong(&nY))
479-
return wxDefaultPosition;
480-
481-
return wxPoint(nX, nY);
482-
}
483-
484-
wxColour JSONItem::toColour(const wxColour& defaultColour) const
485-
{
486-
if (!m_json) {
487-
return defaultColour;
488-
}
450+
return wxDefaultSize;
489451

490-
if (m_json->type != cJSON_String) {
491-
return defaultColour;
492-
}
493-
return wxColour(m_json->valuestring);
452+
return wxSize(nX, nY);
494453
}
495454

496-
wxSize JSONItem::toSize() const
497-
{
498-
wxPoint pt = toPoint();
499-
return wxSize(pt.x, pt.y);
500-
}
501455
#endif
502456

503457
JSONItem& JSONItem::addProperty(const wxString& name, const JSONItem& element)
@@ -551,17 +505,6 @@ wxStringMap_t JSONItem::toStringMap(const wxStringMap_t& default_map) const
551505

552506
JSONItem& JSONItem::addProperty(const wxString& name, size_t value) { return addProperty(name, (int)value); }
553507

554-
#if wxUSE_GUI
555-
JSONItem& JSONItem::addProperty(const wxString& name, const wxColour& colour)
556-
{
557-
wxString colourValue;
558-
if (colour.IsOk()) {
559-
colourValue = colour.GetAsString(wxC2S_HTML_SYNTAX);
560-
}
561-
return addProperty(name, colourValue);
562-
}
563-
#endif
564-
565508
JSONItem& JSONItem::addNull(const wxString& name)
566509
{
567510
if (m_json) {
@@ -575,22 +518,6 @@ JSONItem& JSONItem::addProperty(const wxString& name, const char* value, const w
575518
return addProperty(name, wxString(value, conv));
576519
}
577520

578-
#if wxUSE_GUI
579-
JSONItem& JSONItem::addProperty(const wxString& name, const wxFont& font)
580-
{
581-
return addProperty(name, clFontHelper::ToString(font));
582-
}
583-
584-
wxFont JSONItem::toFont(const wxFont& defaultFont) const
585-
{
586-
wxString str = toString();
587-
if (str.IsEmpty())
588-
return defaultFont;
589-
wxFont f = clFontHelper::FromString(str);
590-
return f;
591-
}
592-
#endif
593-
594521
bool JSONItem::isArray() const
595522
{
596523
if (!m_json) {

CodeLite/JSON.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,17 @@
3030
#include "macros.h"
3131

3232
#include <cJSON.h>
33-
#include <map>
3433
#include <string_view>
3534
#include <type_traits>
35+
#include <unordered_map>
3636
#include <vector>
3737
#include <wx/filename.h>
3838
#include <wx/gdicmn.h>
3939
#include <wx/string.h>
40-
#include <wx/variant.h>
4140
#include <wx/vector.h>
4241

4342
#if wxUSE_GUI
4443
#include <wx/arrstr.h>
45-
#include <wx/colour.h>
46-
#include <wx/font.h>
4744
#endif
4845

4946
//////////////////////////////////////////////////////////////////////////
@@ -56,7 +53,7 @@ class WXDLLIMPEXP_CL JSONItem
5653
friend JSON;
5754

5855
public:
59-
explicit JSONItem(cJSON* json);
56+
explicit JSONItem(std::nullptr_t) {}
6057
JSONItem() = default;
6158
virtual ~JSONItem() = default;
6259

@@ -104,11 +101,12 @@ class WXDLLIMPEXP_CL JSONItem
104101
int arraySize() const;
105102
int toInt(int defaultVal = -1) const;
106103

107-
/// Convert the value into `T` from
108-
template <typename T>
109-
T fromNumber(T default_value) const
104+
/// Convert the value into `E` from
105+
template <typename E>
106+
requires(std::is_enum_v<E>)
107+
E fromNumber(E default_value) const
110108
{
111-
return static_cast<T>(toInt((int)default_value));
109+
return static_cast<E>(toInt(static_cast<int>(default_value)));
112110
}
113111

114112
template <typename T>
@@ -135,10 +133,7 @@ class WXDLLIMPEXP_CL JSONItem
135133
size_t toSize_t(size_t defaultVal = 0) const;
136134
double toDouble(double defaultVal = -1.0) const;
137135

138-
wxColour toColour(const wxColour& defaultColour = wxNullColour) const;
139-
wxFont toFont(const wxFont& defaultFont = wxNullFont) const;
140136
wxSize toSize() const;
141-
wxPoint toPoint() const;
142137

143138
wxStringMap_t toStringMap(const wxStringMap_t& default_map = {}) const;
144139

@@ -165,6 +160,8 @@ class WXDLLIMPEXP_CL JSONItem
165160
*/
166161
JSONItem AddObject(const wxString& name);
167162

163+
JSONItem& addNull(const wxString& name);
164+
168165
JSONItem& addProperty(const wxString& name, const wxString& value);
169166
JSONItem& addProperty(const wxString& name, const std::string& value);
170167
JSONItem& addProperty(const wxString& name, const wxChar* value);
@@ -183,15 +180,11 @@ class WXDLLIMPEXP_CL JSONItem
183180
}
184181

185182
JSONItem& addProperty(const wxString& name, const wxSize& sz);
186-
JSONItem& addProperty(const wxString& name, const wxPoint& pt);
187-
JSONItem& addProperty(const wxString& name, const wxColour& colour);
188-
JSONItem& addProperty(const wxString& name, const wxFont& font);
189183

190184
JSONItem& addProperty(const wxString& name, const wxArrayString& arr);
191185
JSONItem& addProperty(const wxString& name, const wxStringMap_t& stringMap);
192186
JSONItem& addProperty(const wxString& name, const JSONItem& element);
193187
JSONItem& addProperty(const wxString& name, const char* value, const wxMBConv& conv = wxConvUTF8);
194-
JSONItem& addNull(const wxString& name);
195188

196189
/**
197190
* @brief delete property by name
@@ -213,9 +206,11 @@ class WXDLLIMPEXP_CL JSONItem
213206
void arrayAppend(int number);
214207
void arrayAppend(double number);
215208

216-
bool isOk() const { return m_json != NULL; }
209+
bool isOk() const { return m_json != nullptr; }
217210

218211
private:
212+
explicit JSONItem(cJSON* json);
213+
219214
/**
220215
* @brief release the internal pointer
221216
*/
@@ -240,26 +235,27 @@ enum class JsonType {
240235

241236
class WXDLLIMPEXP_CL JSON
242237
{
238+
friend JSONItem;
239+
243240
public:
244241
explicit JSON(JsonType type);
245242
explicit JSON(const wxString& text);
246243
explicit JSON(const wxFileName& filename);
247-
explicit JSON(JSONItem item);
248244

249245
// Make this class not copyable
250246
JSON(const JSON&) = delete;
251247
JSON& operator=(const JSON&) = delete;
252-
virtual ~JSON();
248+
~JSON();
253249

254250
void save(const wxFileName& fn) const;
255-
bool isOk() const { return m_json != NULL; }
251+
bool isOk() const { return m_json != nullptr; }
256252

257253
JSONItem toElement() const;
258254

259-
void clear();
255+
private:
260256
cJSON* release();
261257

262-
protected:
258+
private:
263259
cJSON* m_json = nullptr;
264260
};
265261

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 { return JSONItem(NULL); }
3+
JSONItem LSP::CompletionItem::ToJSON() const { return JSONItem(nullptr); }
44

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

wxcrafter/Properties/category_property.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ PropertyeType CategoryProperty::GetType() { return PT_CATEGORY; }
1414

1515
wxString CategoryProperty::GetValue() const { return m_value; }
1616

17-
JSONItem CategoryProperty::Serialize() const { return JSONItem(NULL); }
17+
JSONItem CategoryProperty::Serialize() const { return JSONItem(nullptr); }
1818

1919
void CategoryProperty::SetValue(const wxString& value) { m_value = value; }
2020

0 commit comments

Comments
 (0)