| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : json_node.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef ZJSONNODE_H |
| 27 |
#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 <wx/gdicmn.h> |
| 34 |
#include "codelite_exports.h" |
| 35 |
#include <map> |
| 36 |
#include "cJSON.h" |
| 37 |
#if wxUSE_GUI |
| 38 |
#include <wx/arrstr.h> |
| 39 |
#include <wx/colour.h> |
| 40 |
#include <wx/font.h> |
| 41 |
#endif |
| 42 |
#include "macros.h" |
| 43 |
#include <vector> |
| 44 |
#include <type_traits> |
| 45 |
// clang-format on |
| 46 |
|
| 47 |
////////////////////////////////////////////////////////////////////////// |
| 48 |
////////////////////////////////////////////////////////////////////////// |
| 49 |
|
| 50 |
class WXDLLIMPEXP_CL JSONItem |
| 51 |
{ |
| 52 |
protected: |
| 53 |
cJSON* m_json = nullptr; |
| 54 |
cJSON* m_walker = nullptr; |
| 55 |
std::string m_name; |
| 56 |
int m_type = wxNOT_FOUND; |
| 57 |
|
| 58 |
// Values |
| 59 |
std::string m_valueString; |
| 60 |
double m_valueNumer = 0; |
| 61 |
|
| 62 |
public: |
| 63 |
JSONItem(cJSON* json); |
| 64 |
JSONItem(const wxString& name, double val); |
| 65 |
JSONItem(const wxString& name, const std::string& val); |
| 66 |
JSONItem(const wxString& name, const char* pval, size_t len); |
| 67 |
JSONItem(const wxString& name, bool val); |
| 68 |
virtual ~JSONItem() {} |
| 69 |
|
| 70 |
// Walkers |
| 71 |
JSONItem firstChild(); |
| 72 |
JSONItem nextChild(); |
| 73 |
|
| 74 |
void SetValueNumer(double valueNumer) { this->m_valueNumer = valueNumer; } |
| 75 |
double GetValueNumer() const { return m_valueNumer; } |
| 76 |
|
| 77 |
void SetValueString(const std::string& valueString) { this->m_valueString = valueString; } |
| 78 |
const std::string& GetValueString() const { return m_valueString; } |
| 79 |
|
| 80 |
// Setters |
| 81 |
//////////////////////////////////////////////// |
| 82 |
void setName(const wxString& m_name) { this->m_name = m_name; } |
| 83 |
void setType(int m_type) { this->m_type = m_type; } |
| 84 |
int getType() const { return m_type; } |
| 85 |
wxString getName() const { return m_name; } |
| 86 |
|
| 87 |
// Readers |
| 88 |
//////////////////////////////////////////////// |
| 89 |
JSONItem namedObject(const wxString& name) const; |
| 90 |
bool hasNamedObject(const wxString& name) const; |
| 91 |
|
| 92 |
JSONItem operator[](int index) const; |
| 93 |
JSONItem operator[](const wxString& name) const; |
| 94 |
|
| 95 |
bool toBool(bool defaultValue = false) const; |
| 96 |
wxString toString(const wxString& defaultValue = wxEmptyString) const; |
| 97 |
wxArrayString toArrayString(const wxArrayString& defaultValue = wxArrayString()) const; |
| 98 |
std::vector<double> toDoubleArray(const std::vector<double>& defaultValue = {}) const; |
| 99 |
std::vector<int> toIntArray(const std::vector<int>& defaultValue = {}) const; |
| 100 |
JSONItem arrayItem(int pos) const; |
| 101 |
|
| 102 |
// Retuen the object type |
| 103 |
bool isNull() const; |
| 104 |
bool isBool() const; |
| 105 |
bool isString() const; |
| 106 |
bool isNumber() const; |
| 107 |
bool isArray() const; |
| 108 |
|
| 109 |
wxString format(bool formatted = true) const; |
| 110 |
/** |
| 111 |
* @brief format the JSON into a raw c string |
| 112 |
* The caller should free the pointer (using free()) |
| 113 |
*/ |
| 114 |
char* FormatRawString(bool formatted = true) const; |
| 115 |
int arraySize() const; |
| 116 |
int toInt(int defaultVal = -1) const; |
| 117 |
size_t toSize_t(size_t defaultVal = 0) const; |
| 118 |
double toDouble(double defaultVal = -1.0) const; |
| 119 |
wxFileName toFileName() const; |
| 120 |
|
| 121 |
#if wxUSE_GUI |
| 122 |
wxColour toColour(const wxColour& defaultColour = wxNullColour) const; |
| 123 |
wxFont toFont(const wxFont& defaultFont = wxNullFont) const; |
| 124 |
wxSize toSize() const; |
| 125 |
wxPoint toPoint() const; |
| 126 |
#endif |
| 127 |
|
| 128 |
wxStringMap_t toStringMap() const; |
| 129 |
|
| 130 |
// Writers |
| 131 |
//////////////////////////////////////////////// |
| 132 |
/** |
| 133 |
* @brief create new named object and append it to this json element |
| 134 |
* @return the newly created object |
| 135 |
*/ |
| 136 |
static JSONItem createObject(const wxString& name = wxT("")); |
| 137 |
/** |
| 138 |
* @brief create new named array and append it to this json element |
| 139 |
* @return the newly created array |
| 140 |
*/ |
| 141 |
static JSONItem createArray(const wxString& name = wxT("")); |
| 142 |
|
| 143 |
/** |
| 144 |
* @brief add array to this json and return a referece to the newly added array |
| 145 |
*/ |
| 146 |
JSONItem AddArray(const wxString& name); |
| 147 |
|
| 148 |
/** |
| 149 |
* @brief add object to this json and return a referece to the newly added object |
| 150 |
*/ |
| 151 |
JSONItem AddObject(const wxString& name); |
| 152 |
|
| 153 |
/** |
| 154 |
* @brief append new element to this json element |
| 155 |
*/ |
| 156 |
void append(const JSONItem& element); |
| 157 |
|
| 158 |
JSONItem& addProperty(const wxString& name, const wxString& value); |
| 159 |
JSONItem& addProperty(const wxString& name, const std::string& value); |
| 160 |
JSONItem& addProperty(const wxString& name, const wxChar* value); |
| 161 |
JSONItem& addProperty(const wxString& name, int value) { return addProperty(name, (long)value); } |
| 162 |
JSONItem& addProperty(const wxString& name, long value); |
| 163 |
JSONItem& addProperty(const wxString& name, size_t value); |
| 164 |
JSONItem& addProperty(const wxString& name, bool value); |
| 165 |
JSONItem& addProperty(const wxString& name, cJSON* pjson); |
| 166 |
JSONItem& addProperty(const wxString& name, const wxFileName& filename); |
| 167 |
JSONItem& addProperty(const wxString& name, const std::vector<int>& arr_int); |
| 168 |
template<class T = int> |
| 169 |
typename std::enable_if<!std::is_same<wxVector<T>, std::vector<T>>::value, |
| 170 |
JSONItem&>::type addProperty(const wxString& name, const wxVector<T>& arr_int) { |
| 171 |
return addProperty(name, std::vector<T>(arr_int.begin(), arr_int.end())); |
| 172 |
} |
| 173 |
|
| 174 |
#if wxUSE_GUI |
| 175 |
JSONItem& addProperty(const wxString& name, const wxSize& sz); |
| 176 |
JSONItem& addProperty(const wxString& name, const wxPoint& pt); |
| 177 |
JSONItem& addProperty(const wxString& name, const wxColour& colour); |
| 178 |
JSONItem& addProperty(const wxString& name, const wxFont& font); |
| 179 |
#endif |
| 180 |
JSONItem& addProperty(const wxString& name, const wxArrayString& arr); |
| 181 |
JSONItem& addProperty(const wxString& name, const wxStringMap_t& stringMap); |
| 182 |
JSONItem& addProperty(const wxString& name, const JSONItem& element); |
| 183 |
JSONItem& addProperty(const wxString& name, const char* value, const wxMBConv& conv = wxConvUTF8); |
| 184 |
|
| 185 |
/** |
| 186 |
* @brief delete property by name |
| 187 |
*/ |
| 188 |
void removeProperty(const wxString& name); |
| 189 |
|
| 190 |
/** |
| 191 |
* @brief detach element from json. Return the detached element |
| 192 |
*/ |
| 193 |
JSONItem detachProperty(const wxString& name); |
| 194 |
|
| 195 |
////////////////////////////////////////////////// |
| 196 |
// Array operations |
| 197 |
////////////////////////////////////////////////// |
| 198 |
|
| 199 |
/** |
| 200 |
* @brief append new number |
| 201 |
* @return the newly added property |
| 202 |
*/ |
| 203 |
void arrayAppend(const JSONItem& element); |
| 204 |
void arrayAppend(const wxString& value); |
| 205 |
void arrayAppend(const char* value); |
| 206 |
void arrayAppend(const std::string& value); |
| 207 |
void arrayAppend(int number); |
| 208 |
void arrayAppend(double number); |
| 209 |
|
| 210 |
bool isOk() const { return m_json != NULL; } |
| 211 |
|
| 212 |
/** |
| 213 |
* @brief release the internal pointer |
| 214 |
*/ |
| 215 |
cJSON* release() |
| 216 |
{ |
| 217 |
cJSON* temp = m_json; |
| 218 |
m_json = nullptr; |
| 219 |
return temp; |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
////////////////////////////////////////////////////////////////////////// |
| 224 |
////////////////////////////////////////////////////////////////////////// |
| 225 |
|
| 226 |
class WXDLLIMPEXP_CL JSON |
| 227 |
{ |
| 228 |
protected: |
| 229 |
cJSON* m_json; |
| 230 |
wxString _errorString; |
| 231 |
|
| 232 |
public: |
| 233 |
JSON(int type); |
| 234 |
JSON(const wxString& text); |
| 235 |
JSON(const wxFileName& filename); |
| 236 |
JSON(JSONItem item); |
| 237 |
JSON(cJSON* json); |
| 238 |
virtual ~JSON(); |
| 239 |
|
| 240 |
void save(const wxFileName& fn) const; |
| 241 |
wxString errorString() const; |
| 242 |
bool isOk() const { return m_json != NULL; } |
| 243 |
|
| 244 |
JSONItem toElement() const; |
| 245 |
|
| 246 |
void clear(); |
| 247 |
cJSON* release(); |
| 248 |
|
| 249 |
private: |
| 250 |
// Make this class not copyable |
| 251 |
JSON(const JSON& src); |
| 252 |
JSON& operator=(const JSON& src); |
| 253 |
}; |
| 254 |
|
| 255 |
#endif // ZJSONNODE_H |