~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxCrafterCB/wxcLib/json_node.h

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef ZJSONNODE_H
 
2
#define ZJSONNODE_H
 
3
 
 
4
#include <wx/string.h>
 
5
#include <wx/variant.h>
 
6
#include <wx/filename.h>
 
7
#include <wx/arrstr.h>
 
8
#include <wx/gdicmn.h>
 
9
#include <map>
 
10
#include "cJSON.h"
 
11
#include <wx/colour.h>
 
12
 
 
13
//////////////////////////////////////////////////////////////////////////
 
14
//////////////////////////////////////////////////////////////////////////
 
15
 
 
16
class JSONElement
 
17
{
 
18
protected:
 
19
    cJSON * _json;
 
20
    int     _type;
 
21
    wxString _name;
 
22
 
 
23
    // Values
 
24
    wxVariant _value;
 
25
    
 
26
public:
 
27
    typedef std::map<wxString, wxString> wxStringMap_t;
 
28
 
 
29
public:
 
30
    JSONElement(cJSON *json);
 
31
    JSONElement(const wxString &name, const wxVariant& val, int type);
 
32
    
 
33
    virtual ~JSONElement() {}
 
34
 
 
35
    // Setters
 
36
    ////////////////////////////////////////////////
 
37
    void setName(const wxString& _name) {
 
38
        this->_name = _name;
 
39
    }
 
40
    void setType(int _type) {
 
41
        this->_type = _type;
 
42
    }
 
43
    int getType() const {
 
44
        return _type;
 
45
    }
 
46
    const wxString& getName() const {
 
47
        return _name;
 
48
    }
 
49
    const wxVariant& getValue() const {
 
50
        return _value;
 
51
    }
 
52
    void setValue(const wxVariant& _value) {
 
53
        this->_value = _value;
 
54
    }
 
55
    // Readers
 
56
    ////////////////////////////////////////////////
 
57
    JSONElement   namedObject(const wxString& name) const ;
 
58
    bool          hasNamedObject(const wxString &name) const;
 
59
    
 
60
    bool          toBool(bool defaultValue = false) const ;
 
61
    wxString      toString(const wxString &defaultValue = wxEmptyString) const ;
 
62
    wxArrayString toArrayString()    const ;
 
63
    JSONElement   arrayItem(int pos) const ;
 
64
    bool          isNull()           const ;
 
65
    bool          isBool()           const ;
 
66
    bool          isString()         const ;
 
67
    wxString      format()           const ;
 
68
    int           arraySize()        const ;
 
69
    int           toInt(int defaultVal = -1) const ;
 
70
    size_t        toSize_t(size_t defaultVal = 0) const ;
 
71
    double        toDouble(double defaultVal = -1.0) const;
 
72
    wxSize        toSize() const;
 
73
    wxPoint       toPoint() const;
 
74
    wxColour toColour(const wxColour& defaultColour = wxNullColour) const;
 
75
    JSONElement::wxStringMap_t toStringMap() const;
 
76
    
 
77
    
 
78
    // Writers
 
79
    ////////////////////////////////////////////////
 
80
    /**
 
81
     * @brief create new named object and append it to this json element
 
82
     * @return the newly created object
 
83
     */
 
84
    static JSONElement createObject(const wxString &name = wxT(""));
 
85
    /**
 
86
     * @brief create new named array and append it to this json element
 
87
     * @return the newly created array
 
88
     */
 
89
    static JSONElement createArray(const wxString &name = wxT(""));
 
90
 
 
91
    /**
 
92
     * @brief append new element to this json element
 
93
     */
 
94
    void append(const JSONElement& element);
 
95
    
 
96
    JSONElement& addProperty(const wxString &name, const wxString &value);
 
97
    JSONElement& addProperty(const wxString& name, const wxChar* value);
 
98
    JSONElement& addProperty(const wxString &name, int value);
 
99
    JSONElement& addProperty(const wxString &name, size_t value);
 
100
    JSONElement& addProperty(const wxString &name, bool value);
 
101
    JSONElement& addProperty(const wxString &name, const wxSize& sz);
 
102
    JSONElement& addProperty(const wxString &name, const wxPoint& pt);
 
103
    JSONElement& addProperty(const wxString &name, const wxColour& colour);
 
104
    JSONElement& addProperty(const wxString &name, const wxArrayString &arr);
 
105
    JSONElement& addProperty(const wxString &name, const JSONElement::wxStringMap_t& stringMap);
 
106
    JSONElement& addProperty(const wxString &name, const JSONElement& element);
 
107
    
 
108
    /**
 
109
     * @brief delete property by name
 
110
     */
 
111
    void removeProperty(const wxString &name);
 
112
    
 
113
    //////////////////////////////////////////////////
 
114
    // Array operations
 
115
    //////////////////////////////////////////////////
 
116
 
 
117
    /**
 
118
     * @brief append new number
 
119
     * @return the newly added property
 
120
     */
 
121
    void arrayAppend(const JSONElement& element);
 
122
    void arrayAppend(const wxString &value);
 
123
 
 
124
    bool isOk() const {
 
125
        return _json != NULL;
 
126
    }
 
127
};
 
128
 
 
129
//////////////////////////////////////////////////////////////////////////
 
130
//////////////////////////////////////////////////////////////////////////
 
131
 
 
132
class JSONRoot
 
133
{
 
134
    cJSON *_json;
 
135
    wxString _errorString;
 
136
 
 
137
public:
 
138
    JSONRoot(int type);
 
139
    JSONRoot(const wxString& text);
 
140
    JSONRoot(const wxFileName& filename);
 
141
    virtual ~JSONRoot();
 
142
    
 
143
    void save(const wxFileName &fn) const;
 
144
    wxString errorString() const;
 
145
    bool isOk() const {
 
146
        return _json != NULL;
 
147
    }
 
148
 
 
149
    JSONElement toElement() const;
 
150
 
 
151
    void clear();
 
152
 
 
153
private:
 
154
    // Make this class not copyable
 
155
    JSONRoot(const JSONRoot& src);
 
156
    JSONRoot& operator=(const JSONRoot& src);
 
157
};
 
158
 
 
159
 
 
160
#endif // ZJSONNODE_H