| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 The CodeLite Team |
| 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 WXC_JSONNODE_H |
| 27 |
#define WXC_JSONNODE_H |
| 28 |
|
| 29 |
#include <wx/string.h> |
| 30 |
#include <wx/variant.h> |
| 31 |
#include <wx/filename.h> |
| 32 |
#include <wx/arrstr.h> |
| 33 |
#include <wx/gdicmn.h> |
| 34 |
#include <map> |
| 35 |
#include "cJSON.h" |
| 36 |
#include <wx/colour.h> |
| 37 |
#include "../CodeLite/macros.h" |
| 38 |
|
| 39 |
////////////////////////////////////////////////////////////////////////// |
| 40 |
////////////////////////////////////////////////////////////////////////// |
| 41 |
|
| 42 |
class JSONElement |
| 43 |
{ |
| 44 |
protected: |
| 45 |
cJSON* _json; |
| 46 |
int _type; |
| 47 |
wxString _name; |
| 48 |
|
| 49 |
// Values |
| 50 |
wxVariant _value; |
| 51 |
cJSON* _walker; |
| 52 |
|
| 53 |
public: |
| 54 |
JSONElement(cJSON* json); |
| 55 |
JSONElement(const wxString& name, const wxVariant& val, int type); |
| 56 |
|
| 57 |
virtual ~JSONElement() {} |
| 58 |
|
| 59 |
// Walkers |
| 60 |
JSONElement firstChild(); |
| 61 |
JSONElement nextChild(); |
| 62 |
|
| 63 |
// Setters |
| 64 |
//////////////////////////////////////////////// |
| 65 |
void setName(const wxString& _name) { this->_name = _name; } |
| 66 |
void setType(int _type) { this->_type = _type; } |
| 67 |
int getType() const { return _type; } |
| 68 |
const wxString& getName() const { return _name; } |
| 69 |
const wxVariant& getValue() const { return _value; } |
| 70 |
void setValue(const wxVariant& _value) { this->_value = _value; } |
| 71 |
// Readers |
| 72 |
//////////////////////////////////////////////// |
| 73 |
JSONElement namedObject(const wxString& name) const; |
| 74 |
bool hasNamedObject(const wxString& name) const; |
| 75 |
|
| 76 |
bool toBool(bool defaultValue = false) const; |
| 77 |
wxString toString(const wxString& defaultValue = wxEmptyString) const; |
| 78 |
wxArrayString toArrayString() const; |
| 79 |
JSONElement arrayItem(int pos) const; |
| 80 |
bool isNull() const; |
| 81 |
bool isBool() const; |
| 82 |
bool isString() const; |
| 83 |
wxString format() const; |
| 84 |
/** |
| 85 |
* @brief format the JSON into a raw c string |
| 86 |
* The caller should free the pointer (using free()) |
| 87 |
*/ |
| 88 |
char* FormatRawString(bool formatted = true) const; |
| 89 |
int arraySize() const; |
| 90 |
int toInt(int defaultVal = -1) const; |
| 91 |
size_t toSize_t(size_t defaultVal = 0) const; |
| 92 |
double toDouble(double defaultVal = -1.0) const; |
| 93 |
wxSize toSize() const; |
| 94 |
wxPoint toPoint() const; |
| 95 |
wxColour toColour(const wxColour& defaultColour = wxNullColour) const; |
| 96 |
wxStringMap_t toStringMap() const; |
| 97 |
|
| 98 |
// Writers |
| 99 |
//////////////////////////////////////////////// |
| 100 |
/** |
| 101 |
* @brief create new named object and append it to this json element |
| 102 |
* @return the newly created object |
| 103 |
*/ |
| 104 |
static JSONElement createObject(const wxString& name = wxT("")); |
| 105 |
/** |
| 106 |
* @brief create new named array and append it to this json element |
| 107 |
* @return the newly created array |
| 108 |
*/ |
| 109 |
static JSONElement createArray(const wxString& name = wxT("")); |
| 110 |
|
| 111 |
/** |
| 112 |
* @brief append new element to this json element |
| 113 |
*/ |
| 114 |
void append(const JSONElement& element); |
| 115 |
|
| 116 |
JSONElement& addProperty(const wxString& name, const wxString& value); |
| 117 |
JSONElement& addProperty(const wxString& name, const wxChar* value); |
| 118 |
JSONElement& addProperty(const wxString& name, int value) { return addProperty(name, (long)value); } |
| 119 |
JSONElement& addProperty(const wxString& name, long value); |
| 120 |
JSONElement& addProperty(const wxString& name, size_t value); |
| 121 |
JSONElement& addProperty(const wxString& name, bool value); |
| 122 |
JSONElement& addProperty(const wxString& name, const wxSize& sz); |
| 123 |
JSONElement& addProperty(const wxString& name, const wxPoint& pt); |
| 124 |
JSONElement& addProperty(const wxString& name, const wxColour& colour); |
| 125 |
JSONElement& addProperty(const wxString& name, const wxArrayString& arr); |
| 126 |
JSONElement& addProperty(const wxString& name, const wxStringMap_t& stringMap); |
| 127 |
JSONElement& addProperty(const wxString& name, const JSONElement& element); |
| 128 |
JSONElement& addProperty(const wxString& name, const char* value, const wxMBConv& conv = wxConvUTF8); |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief delete property by name |
| 132 |
*/ |
| 133 |
void removeProperty(const wxString& name); |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief detach element from json. Return the detached element |
| 137 |
*/ |
| 138 |
JSONElement detachProperty(const wxString& name); |
| 139 |
|
| 140 |
////////////////////////////////////////////////// |
| 141 |
// Array operations |
| 142 |
////////////////////////////////////////////////// |
| 143 |
|
| 144 |
/** |
| 145 |
* @brief append new number |
| 146 |
* @return the newly added property |
| 147 |
*/ |
| 148 |
void arrayAppend(const JSONElement& element); |
| 149 |
void arrayAppend(const wxString& value); |
| 150 |
|
| 151 |
bool isOk() const { return _json != NULL; } |
| 152 |
}; |
| 153 |
|
| 154 |
////////////////////////////////////////////////////////////////////////// |
| 155 |
////////////////////////////////////////////////////////////////////////// |
| 156 |
|
| 157 |
class JSONRoot |
| 158 |
{ |
| 159 |
protected: |
| 160 |
cJSON* _json; |
| 161 |
wxString _errorString; |
| 162 |
|
| 163 |
public: |
| 164 |
JSONRoot(int type); |
| 165 |
JSONRoot(const wxString& text); |
| 166 |
JSONRoot(const wxFileName& filename); |
| 167 |
virtual ~JSONRoot(); |
| 168 |
|
| 169 |
void save(const wxFileName& fn) const; |
| 170 |
wxString errorString() const; |
| 171 |
bool isOk() const { return _json != NULL; } |
| 172 |
|
| 173 |
JSONElement toElement() const; |
| 174 |
|
| 175 |
void clear(); |
| 176 |
/** |
| 177 |
* @brief release the internal pointer and return it wrapped by JSONElement |
| 178 |
*/ |
| 179 |
JSONElement release(); |
| 180 |
|
| 181 |
private: |
| 182 |
// Make this class not copyable |
| 183 |
JSONRoot(const JSONRoot& src); |
| 184 |
JSONRoot& operator=(const JSONRoot& src); |
| 185 |
}; |
| 186 |
|
| 187 |
#endif // WXC_JSONNODE_H |