3
#include <wx/filename.h>
6
JSONRoot::JSONRoot(const wxString& text)
9
_json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
12
JSONRoot::JSONRoot(int type)
15
if(type == cJSON_Array)
16
_json = cJSON_CreateArray();
18
_json = cJSON_CreateObject();
22
JSONRoot::JSONRoot(const wxFileName& filename)
26
wxFFile fp(filename.GetFullPath(), wxT("r+b"));
28
if( fp.ReadAll( &content, wxConvUTF8 ) ) {
29
_json = cJSON_Parse( content.mb_str(wxConvUTF8).data() );
34
_json = cJSON_CreateObject();
46
void JSONRoot::save(const wxFileName& fn) const
48
wxFFile fp( fn.GetFullPath(), wxT("w+b") );
50
fp.Write( toElement().format(), wxConvUTF8 );
55
JSONElement JSONRoot::toElement() const
58
return JSONElement(NULL);
60
return JSONElement(_json);
63
wxString JSONRoot::errorString() const
68
JSONElement JSONElement::namedObject(const wxString& name) const
71
return JSONElement(NULL);
74
cJSON *obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data());
76
return JSONElement(NULL);
78
return JSONElement(obj);
81
void JSONRoot::clear()
83
int type = cJSON_Object;
89
if(type == cJSON_Array)
90
_json = cJSON_CreateArray();
92
_json = cJSON_CreateObject();
95
///////////////////////////////////////////////////////////
96
///////////////////////////////////////////////////////////
97
JSONElement::JSONElement(cJSON* json)
102
_name = wxString(_json->string, wxConvUTF8);
107
JSONElement::JSONElement(const wxString &name, const wxVariant& val, int type)
115
JSONElement JSONElement::arrayItem(int pos) const
118
return JSONElement(NULL);
121
if(_json->type != cJSON_Array)
122
return JSONElement(NULL);
124
int size = cJSON_GetArraySize(_json);
126
return JSONElement(NULL);
128
return JSONElement(cJSON_GetArrayItem(_json, pos));
131
bool JSONElement::isNull() const
136
return _json->type == cJSON_NULL;
139
bool JSONElement::toBool(bool defaultValue) const
149
return _json->type == cJSON_True;
152
wxString JSONElement::toString(const wxString& defaultValue) const
158
if(_json->type != cJSON_String) {
162
return wxString(_json->valuestring, wxConvUTF8);
165
bool JSONElement::isBool() const
171
return _json->type == cJSON_True || _json->type == cJSON_False;
174
bool JSONElement::isString() const
180
return _json->type == cJSON_String;
183
void JSONElement::append(const JSONElement& element)
189
switch(element.getType()) {
191
cJSON_AddFalseToObject(_json, element.getName().mb_str(wxConvUTF8).data());
195
cJSON_AddTrueToObject(_json, element.getName().mb_str(wxConvUTF8).data());
199
cJSON_AddNullToObject(_json, element.getName().mb_str(wxConvUTF8).data());
203
cJSON_AddNumberToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetLong());
207
cJSON_AddStringToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetString().mb_str(wxConvUTF8).data());
212
cJSON_AddItemToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element._json);
217
void JSONElement::arrayAppend(const JSONElement& element)
224
switch(element.getType()) {
226
p = cJSON_CreateFalse();
230
p = cJSON_CreateTrue();
234
p = cJSON_CreateNull();
238
p = cJSON_CreateNumber(element.getValue().GetDouble());
242
p = cJSON_CreateString(element.getValue().GetString().mb_str(wxConvUTF8).data());
250
cJSON_AddItemToArray(_json, p);
255
JSONElement JSONElement::createArray(const wxString &name)
257
JSONElement arr(cJSON_CreateArray());
259
arr.setType(cJSON_Array);
263
JSONElement JSONElement::createObject(const wxString &name)
265
JSONElement obj(cJSON_CreateObject());
267
obj.setType(cJSON_Object);
271
wxString JSONElement::format() const
277
char *p = cJSON_Print(_json);
278
wxString output(p, wxConvUTF8);
283
int JSONElement::toInt(int defaultVal) const
289
if(_json->type != cJSON_Number) {
293
return _json->valueint;
296
size_t JSONElement::toSize_t(size_t defaultVal) const
302
if(_json->type != cJSON_Number) {
306
return (size_t)_json->valueint;
309
double JSONElement::toDouble(double defaultVal) const
315
if(_json->type != cJSON_Number) {
319
return _json->valuedouble;
322
int JSONElement::arraySize() const
328
if(_json->type != cJSON_Array)
331
return cJSON_GetArraySize(_json);
334
JSONElement& JSONElement::addProperty(const wxString& name, bool value)
337
append(JSONElement(name, value, cJSON_True));
340
append(JSONElement(name, value, cJSON_False));
345
JSONElement& JSONElement::addProperty(const wxString& name, const wxString& value)
347
append(JSONElement(name, value, cJSON_String));
351
JSONElement& JSONElement::addProperty(const wxString& name, const wxChar* value)
353
append(JSONElement(name, wxString(value), cJSON_String));
357
JSONElement& JSONElement::addProperty(const wxString& name, int value)
359
append(JSONElement(name, value, cJSON_Number));
363
JSONElement& JSONElement::addProperty(const wxString& name, const wxArrayString& arr)
365
JSONElement arrEle = JSONElement::createArray(name);
366
for(size_t i=0; i<arr.GetCount(); i++) {
367
arrEle.arrayAppend(arr.Item(i));
373
void JSONElement::arrayAppend(const wxString& value)
375
arrayAppend(JSONElement(wxT(""), value, cJSON_String));
378
wxArrayString JSONElement::toArrayString() const
385
if(_json->type != cJSON_Array) {
389
for(int i=0; i<arraySize(); i++) {
390
arr.Add(arrayItem(i).toString());
395
bool JSONElement::hasNamedObject(const wxString& name) const
401
cJSON *obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data());
405
JSONElement& JSONElement::addProperty(const wxString& name, const wxPoint& pt)
408
szStr << pt.x << wxT(",") << pt.y;
409
return addProperty(name, szStr);
412
JSONElement& JSONElement::addProperty(const wxString& name, const wxSize& sz)
415
szStr << sz.x << wxT(",") << sz.y;
416
return addProperty(name, szStr);
420
JSONElement& JSONElement::addProperty(const wxString& name, const JSONElement& element)
425
cJSON_AddItemToObject(_json, name.mb_str(wxConvUTF8).data(), element._json);
429
wxPoint JSONElement::toPoint() const
432
return wxDefaultPosition;
435
if(_json->type != cJSON_String) {
436
return wxDefaultPosition;
439
wxString str = wxString(_json->valuestring, wxConvUTF8);
440
wxString x = str.BeforeFirst(',');
441
wxString y = str.AfterFirst(',');
444
if ( !x.ToLong(&nX) || !y.ToLong(&nY) )
445
return wxDefaultPosition;
447
return wxPoint(nX, nY);
450
wxColour JSONElement::toColour(const wxColour& defaultColour) const
453
return defaultColour;
456
if(_json->type != cJSON_String) {
457
return defaultColour;
460
return wxColour( wxString(_json->valuestring, wxConvUTF8) );
463
wxSize JSONElement::toSize() const
465
wxPoint pt = toPoint();
466
return wxSize(pt.x, pt.y);
469
void JSONElement::removeProperty(const wxString& name)
471
// delete child property
475
cJSON_DeleteItemFromObject(_json, name.mb_str(wxConvUTF8).data());
478
JSONElement& JSONElement::addProperty(const wxString& name, const JSONElement::wxStringMap_t& stringMap)
483
JSONElement arr = JSONElement::createArray(name);
484
JSONElement::wxStringMap_t::const_iterator iter = stringMap.begin();
485
for( ; iter != stringMap.end(); ++iter ) {
486
JSONElement obj = JSONElement::createObject();
487
obj.addProperty(wxT("key"), iter->first);
488
obj.addProperty(wxT("value"), iter->second);
489
arr.arrayAppend(obj);
495
JSONElement::wxStringMap_t JSONElement::toStringMap() const
497
JSONElement::wxStringMap_t res;
502
if(_json->type != cJSON_Array) {
506
for(int i=0; i<arraySize(); ++i) {
507
wxString key = arrayItem(i).namedObject(wxT("key")).toString();
508
wxString val = arrayItem(i).namedObject(wxT("value")).toString();
509
res.insert(std::make_pair(key, val));
514
JSONElement& JSONElement::addProperty(const wxString& name, size_t value)
516
return addProperty(name, (int)value);
519
JSONElement& JSONElement::addProperty(const wxString& name, const wxColour& colour)
521
wxString colourValue;
522
if ( colour.IsOk() ) {
523
colourValue = colour.GetAsString(wxC2S_HTML_SYNTAX);
525
return addProperty(name, colourValue);