| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 The CodeLite Team |
| 5 |
// file name : json_node.cpp |
| 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 |
#include "json_node.h" |
| 27 |
#include <stdlib.h> |
| 28 |
#include <wx/filename.h> |
| 29 |
#include <wx/ffile.h> |
| 30 |
|
| 31 |
JSONRoot::JSONRoot(const wxString& text) |
| 32 |
: _json(NULL) |
| 33 |
{ |
| 34 |
_json = cJSON_Parse(text.mb_str(wxConvUTF8).data()); |
| 35 |
} |
| 36 |
|
| 37 |
JSONRoot::JSONRoot(int type) |
| 38 |
: _json(NULL) |
| 39 |
{ |
| 40 |
if(type == cJSON_Array) |
| 41 |
_json = cJSON_CreateArray(); |
| 42 |
else |
| 43 |
_json = cJSON_CreateObject(); |
| 44 |
} |
| 45 |
|
| 46 |
JSONRoot::JSONRoot(const wxFileName& filename) |
| 47 |
: _json(NULL) |
| 48 |
{ |
| 49 |
wxString content; |
| 50 |
wxFFile fp(filename.GetFullPath(), wxT("rb")); |
| 51 |
if(fp.IsOpened()) { |
| 52 |
if(fp.ReadAll(&content, wxConvUTF8)) { _json = cJSON_Parse(content.mb_str(wxConvUTF8).data()); } |
| 53 |
} |
| 54 |
|
| 55 |
if(!_json) { _json = cJSON_CreateObject(); } |
| 56 |
} |
| 57 |
|
| 58 |
JSONRoot::~JSONRoot() |
| 59 |
{ |
| 60 |
if(_json) { |
| 61 |
cJSON_Delete(_json); |
| 62 |
_json = NULL; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void JSONRoot::save(const wxFileName& fn) const |
| 67 |
{ |
| 68 |
wxFFile fp(fn.GetFullPath(), wxT("w+b")); |
| 69 |
if(fp.IsOpened()) { |
| 70 |
fp.Write(toElement().format(), wxConvUTF8); |
| 71 |
fp.Close(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
JSONElement JSONRoot::toElement() const |
| 76 |
{ |
| 77 |
if(!_json) { return JSONElement(NULL); } |
| 78 |
return JSONElement(_json); |
| 79 |
} |
| 80 |
|
| 81 |
wxString JSONRoot::errorString() const { return _errorString; } |
| 82 |
|
| 83 |
JSONElement JSONElement::namedObject(const wxString& name) const |
| 84 |
{ |
| 85 |
if(!_json) { return JSONElement(NULL); } |
| 86 |
|
| 87 |
cJSON* obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data()); |
| 88 |
if(!obj) { return JSONElement(NULL); } |
| 89 |
return JSONElement(obj); |
| 90 |
} |
| 91 |
|
| 92 |
void JSONRoot::clear() |
| 93 |
{ |
| 94 |
int type = cJSON_Object; |
| 95 |
if(_json) { |
| 96 |
type = _json->type; |
| 97 |
cJSON_Delete(_json); |
| 98 |
_json = NULL; |
| 99 |
} |
| 100 |
if(type == cJSON_Array) |
| 101 |
_json = cJSON_CreateArray(); |
| 102 |
else |
| 103 |
_json = cJSON_CreateObject(); |
| 104 |
} |
| 105 |
|
| 106 |
JSONElement JSONRoot::release() |
| 107 |
{ |
| 108 |
cJSON* p = _json; |
| 109 |
_json = NULL; |
| 110 |
return JSONElement(p); |
| 111 |
} |
| 112 |
|
| 113 |
/////////////////////////////////////////////////////////// |
| 114 |
/////////////////////////////////////////////////////////// |
| 115 |
JSONElement::JSONElement(cJSON* json) |
| 116 |
: _json(json) |
| 117 |
, _type(-1) |
| 118 |
, _walker(NULL) |
| 119 |
{ |
| 120 |
if(_json) { |
| 121 |
_name = wxString(_json->string, wxConvUTF8); |
| 122 |
_type = _json->type; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
JSONElement::JSONElement(const wxString& name, const wxVariant& val, int type) |
| 127 |
: _json(NULL) |
| 128 |
, _type(type) |
| 129 |
, _walker(NULL) |
| 130 |
{ |
| 131 |
_value = val; |
| 132 |
_name = name; |
| 133 |
} |
| 134 |
|
| 135 |
JSONElement JSONElement::arrayItem(int pos) const |
| 136 |
{ |
| 137 |
if(!_json) { return JSONElement(NULL); } |
| 138 |
|
| 139 |
if(_json->type != cJSON_Array) return JSONElement(NULL); |
| 140 |
|
| 141 |
int size = cJSON_GetArraySize(_json); |
| 142 |
if(pos >= size) return JSONElement(NULL); |
| 143 |
|
| 144 |
return JSONElement(cJSON_GetArrayItem(_json, pos)); |
| 145 |
} |
| 146 |
|
| 147 |
bool JSONElement::isNull() const |
| 148 |
{ |
| 149 |
if(!_json) { return false; } |
| 150 |
return _json->type == cJSON_NULL; |
| 151 |
} |
| 152 |
|
| 153 |
bool JSONElement::toBool(bool defaultValue) const |
| 154 |
{ |
| 155 |
if(!_json) { return defaultValue; } |
| 156 |
|
| 157 |
if(!isBool()) { return defaultValue; } |
| 158 |
|
| 159 |
return _json->type == cJSON_True; |
| 160 |
} |
| 161 |
|
| 162 |
wxString JSONElement::toString(const wxString& defaultValue) const |
| 163 |
{ |
| 164 |
if(!_json) { return defaultValue; } |
| 165 |
|
| 166 |
if(_json->type != cJSON_String) { return defaultValue; } |
| 167 |
|
| 168 |
return wxString(_json->valuestring, wxConvUTF8); |
| 169 |
} |
| 170 |
|
| 171 |
bool JSONElement::isBool() const |
| 172 |
{ |
| 173 |
if(!_json) { return false; } |
| 174 |
|
| 175 |
return _json->type == cJSON_True || _json->type == cJSON_False; |
| 176 |
} |
| 177 |
|
| 178 |
bool JSONElement::isString() const |
| 179 |
{ |
| 180 |
if(!_json) { return false; } |
| 181 |
|
| 182 |
return _json->type == cJSON_String; |
| 183 |
} |
| 184 |
|
| 185 |
void JSONElement::append(const JSONElement& element) |
| 186 |
{ |
| 187 |
if(!_json) { return; } |
| 188 |
|
| 189 |
switch(element.getType()) { |
| 190 |
case cJSON_False: |
| 191 |
cJSON_AddFalseToObject(_json, element.getName().mb_str(wxConvUTF8).data()); |
| 192 |
break; |
| 193 |
|
| 194 |
case cJSON_True: |
| 195 |
cJSON_AddTrueToObject(_json, element.getName().mb_str(wxConvUTF8).data()); |
| 196 |
break; |
| 197 |
|
| 198 |
case cJSON_NULL: |
| 199 |
cJSON_AddNullToObject(_json, element.getName().mb_str(wxConvUTF8).data()); |
| 200 |
break; |
| 201 |
|
| 202 |
case cJSON_Number: |
| 203 |
cJSON_AddNumberToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetLong()); |
| 204 |
break; |
| 205 |
|
| 206 |
case cJSON_String: |
| 207 |
cJSON_AddStringToObject(_json, element.getName().mb_str(wxConvUTF8).data(), |
| 208 |
element.getValue().GetString().mb_str(wxConvUTF8).data()); |
| 209 |
break; |
| 210 |
|
| 211 |
case cJSON_Array: |
| 212 |
case cJSON_Object: |
| 213 |
cJSON_AddItemToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element._json); |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void JSONElement::arrayAppend(const JSONElement& element) |
| 219 |
{ |
| 220 |
if(!_json) { return; } |
| 221 |
|
| 222 |
cJSON* p = NULL; |
| 223 |
switch(element.getType()) { |
| 224 |
case cJSON_False: |
| 225 |
p = cJSON_CreateFalse(); |
| 226 |
break; |
| 227 |
|
| 228 |
case cJSON_True: |
| 229 |
p = cJSON_CreateTrue(); |
| 230 |
break; |
| 231 |
|
| 232 |
case cJSON_NULL: |
| 233 |
p = cJSON_CreateNull(); |
| 234 |
break; |
| 235 |
|
| 236 |
case cJSON_Number: |
| 237 |
p = cJSON_CreateNumber(element.getValue().GetDouble()); |
| 238 |
break; |
| 239 |
|
| 240 |
case cJSON_String: |
| 241 |
p = cJSON_CreateString(element.getValue().GetString().mb_str(wxConvUTF8).data()); |
| 242 |
break; |
| 243 |
case cJSON_Array: |
| 244 |
case cJSON_Object: |
| 245 |
p = element._json; |
| 246 |
break; |
| 247 |
} |
| 248 |
if(p) { cJSON_AddItemToArray(_json, p); } |
| 249 |
} |
| 250 |
|
| 251 |
JSONElement JSONElement::createArray(const wxString& name) |
| 252 |
{ |
| 253 |
JSONElement arr(cJSON_CreateArray()); |
| 254 |
arr.setName(name); |
| 255 |
arr.setType(cJSON_Array); |
| 256 |
return arr; |
| 257 |
} |
| 258 |
|
| 259 |
JSONElement JSONElement::createObject(const wxString& name) |
| 260 |
{ |
| 261 |
JSONElement obj(cJSON_CreateObject()); |
| 262 |
obj.setName(name); |
| 263 |
obj.setType(cJSON_Object); |
| 264 |
return obj; |
| 265 |
} |
| 266 |
|
| 267 |
char* JSONElement::FormatRawString(bool formatted) const |
| 268 |
{ |
| 269 |
if(!_json) { return NULL; } |
| 270 |
|
| 271 |
if(formatted) { |
| 272 |
return cJSON_Print(_json); |
| 273 |
|
| 274 |
} else { |
| 275 |
return cJSON_PrintUnformatted(_json); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
wxString JSONElement::format() const |
| 280 |
{ |
| 281 |
if(!_json) { return wxT(""); } |
| 282 |
|
| 283 |
char* p = cJSON_Print(_json); |
| 284 |
wxString output(p, wxConvUTF8); |
| 285 |
free(p); |
| 286 |
return output; |
| 287 |
} |
| 288 |
|
| 289 |
int JSONElement::toInt(int defaultVal) const |
| 290 |
{ |
| 291 |
if(!_json) { return defaultVal; } |
| 292 |
|
| 293 |
if(_json->type != cJSON_Number) { return defaultVal; } |
| 294 |
|
| 295 |
return _json->valueint; |
| 296 |
} |
| 297 |
|
| 298 |
size_t JSONElement::toSize_t(size_t defaultVal) const |
| 299 |
{ |
| 300 |
if(!_json) { return defaultVal; } |
| 301 |
|
| 302 |
if(_json->type != cJSON_Number) { return defaultVal; } |
| 303 |
|
| 304 |
return (size_t)_json->valueint; |
| 305 |
} |
| 306 |
|
| 307 |
double JSONElement::toDouble(double defaultVal) const |
| 308 |
{ |
| 309 |
if(!_json) { return defaultVal; } |
| 310 |
|
| 311 |
if(_json->type != cJSON_Number) { return defaultVal; } |
| 312 |
|
| 313 |
return _json->valuedouble; |
| 314 |
} |
| 315 |
|
| 316 |
int JSONElement::arraySize() const |
| 317 |
{ |
| 318 |
if(!_json) { return 0; } |
| 319 |
|
| 320 |
if(_json->type != cJSON_Array) return 0; |
| 321 |
|
| 322 |
return cJSON_GetArraySize(_json); |
| 323 |
} |
| 324 |
|
| 325 |
JSONElement& JSONElement::addProperty(const wxString& name, bool value) |
| 326 |
{ |
| 327 |
if(value) { |
| 328 |
append(JSONElement(name, value, cJSON_True)); |
| 329 |
|
| 330 |
} else { |
| 331 |
append(JSONElement(name, value, cJSON_False)); |
| 332 |
} |
| 333 |
return *this; |
| 334 |
} |
| 335 |
|
| 336 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxString& value) |
| 337 |
{ |
| 338 |
append(JSONElement(name, value, cJSON_String)); |
| 339 |
return *this; |
| 340 |
} |
| 341 |
|
| 342 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxChar* value) |
| 343 |
{ |
| 344 |
append(JSONElement(name, wxString(value), cJSON_String)); |
| 345 |
return *this; |
| 346 |
} |
| 347 |
|
| 348 |
JSONElement& JSONElement::addProperty(const wxString& name, long value) |
| 349 |
{ |
| 350 |
append(JSONElement(name, value, cJSON_Number)); |
| 351 |
return *this; |
| 352 |
} |
| 353 |
|
| 354 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxArrayString& arr) |
| 355 |
{ |
| 356 |
JSONElement arrEle = JSONElement::createArray(name); |
| 357 |
for(size_t i = 0; i < arr.GetCount(); i++) { |
| 358 |
arrEle.arrayAppend(arr.Item(i)); |
| 359 |
} |
| 360 |
append(arrEle); |
| 361 |
return *this; |
| 362 |
} |
| 363 |
|
| 364 |
void JSONElement::arrayAppend(const wxString& value) { arrayAppend(JSONElement(wxT(""), value, cJSON_String)); } |
| 365 |
|
| 366 |
wxArrayString JSONElement::toArrayString() const |
| 367 |
{ |
| 368 |
wxArrayString arr; |
| 369 |
if(!_json) { return arr; } |
| 370 |
|
| 371 |
if(_json->type != cJSON_Array) { return arr; } |
| 372 |
|
| 373 |
for(int i = 0; i < arraySize(); i++) { |
| 374 |
arr.Add(arrayItem(i).toString()); |
| 375 |
} |
| 376 |
return arr; |
| 377 |
} |
| 378 |
|
| 379 |
bool JSONElement::hasNamedObject(const wxString& name) const |
| 380 |
{ |
| 381 |
if(!_json) { return false; } |
| 382 |
|
| 383 |
cJSON* obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data()); |
| 384 |
return obj != NULL; |
| 385 |
} |
| 386 |
|
| 387 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxPoint& pt) |
| 388 |
{ |
| 389 |
wxString szStr; |
| 390 |
szStr << pt.x << "," << pt.y; |
| 391 |
return addProperty(name, szStr); |
| 392 |
} |
| 393 |
|
| 394 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxSize& sz) |
| 395 |
{ |
| 396 |
wxString szStr; |
| 397 |
szStr << sz.x << "," << sz.y; |
| 398 |
return addProperty(name, szStr); |
| 399 |
} |
| 400 |
|
| 401 |
JSONElement& JSONElement::addProperty(const wxString& name, const JSONElement& element) |
| 402 |
{ |
| 403 |
if(!_json) { return *this; } |
| 404 |
cJSON_AddItemToObject(_json, name.mb_str(wxConvUTF8).data(), element._json); |
| 405 |
return *this; |
| 406 |
} |
| 407 |
|
| 408 |
wxPoint JSONElement::toPoint() const |
| 409 |
{ |
| 410 |
if(!_json) { return wxDefaultPosition; } |
| 411 |
|
| 412 |
if(_json->type != cJSON_String) { return wxDefaultPosition; } |
| 413 |
|
| 414 |
wxString str = _json->valuestring; |
| 415 |
wxString x = str.BeforeFirst(','); |
| 416 |
wxString y = str.AfterFirst(','); |
| 417 |
|
| 418 |
long nX(-1), nY(-1); |
| 419 |
if(!x.ToLong(&nX) || !y.ToLong(&nY)) return wxDefaultPosition; |
| 420 |
|
| 421 |
return wxPoint(nX, nY); |
| 422 |
} |
| 423 |
|
| 424 |
wxColour JSONElement::toColour(const wxColour& defaultColour) const |
| 425 |
{ |
| 426 |
if(!_json) { return defaultColour; } |
| 427 |
|
| 428 |
if(_json->type != cJSON_String) { return defaultColour; } |
| 429 |
|
| 430 |
return wxColour(_json->valuestring); |
| 431 |
} |
| 432 |
|
| 433 |
wxSize JSONElement::toSize() const |
| 434 |
{ |
| 435 |
wxPoint pt = toPoint(); |
| 436 |
return wxSize(pt.x, pt.y); |
| 437 |
} |
| 438 |
|
| 439 |
void JSONElement::removeProperty(const wxString& name) |
| 440 |
{ |
| 441 |
// delete child property |
| 442 |
if(!_json) { return; } |
| 443 |
cJSON_DeleteItemFromObject(_json, name.mb_str(wxConvUTF8).data()); |
| 444 |
} |
| 445 |
|
| 446 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxStringMap_t& stringMap) |
| 447 |
{ |
| 448 |
if(!_json) return *this; |
| 449 |
|
| 450 |
JSONElement arr = JSONElement::createArray(name); |
| 451 |
wxStringMap_t::const_iterator iter = stringMap.begin(); |
| 452 |
for(; iter != stringMap.end(); ++iter) { |
| 453 |
JSONElement obj = JSONElement::createObject(); |
| 454 |
obj.addProperty("key", iter->first); |
| 455 |
obj.addProperty("value", iter->second); |
| 456 |
arr.arrayAppend(obj); |
| 457 |
} |
| 458 |
append(arr); |
| 459 |
return *this; |
| 460 |
} |
| 461 |
|
| 462 |
wxStringMap_t JSONElement::toStringMap() const |
| 463 |
{ |
| 464 |
wxStringMap_t res; |
| 465 |
if(!_json) { return res; } |
| 466 |
|
| 467 |
if(_json->type != cJSON_Array) { return res; } |
| 468 |
|
| 469 |
for(int i = 0; i < arraySize(); ++i) { |
| 470 |
wxString key = arrayItem(i).namedObject("key").toString(); |
| 471 |
wxString val = arrayItem(i).namedObject("value").toString(); |
| 472 |
res.insert(std::make_pair(key, val)); |
| 473 |
} |
| 474 |
return res; |
| 475 |
} |
| 476 |
|
| 477 |
JSONElement& JSONElement::addProperty(const wxString& name, size_t value) { return addProperty(name, (int)value); } |
| 478 |
|
| 479 |
JSONElement& JSONElement::addProperty(const wxString& name, const wxColour& colour) |
| 480 |
{ |
| 481 |
wxString colourValue; |
| 482 |
if(colour.IsOk()) { colourValue = colour.GetAsString(wxC2S_HTML_SYNTAX); } |
| 483 |
return addProperty(name, colourValue); |
| 484 |
} |
| 485 |
|
| 486 |
JSONElement JSONElement::firstChild() |
| 487 |
{ |
| 488 |
_walker = NULL; |
| 489 |
if(!_json) { return JSONElement(NULL); } |
| 490 |
|
| 491 |
if(!_json->child) { return JSONElement(NULL); } |
| 492 |
|
| 493 |
_walker = _json->child; |
| 494 |
return JSONElement(_walker); |
| 495 |
} |
| 496 |
|
| 497 |
JSONElement JSONElement::nextChild() |
| 498 |
{ |
| 499 |
if(!_walker) { return JSONElement(NULL); } |
| 500 |
|
| 501 |
JSONElement element(_walker->next); |
| 502 |
_walker = _walker->next; |
| 503 |
return element; |
| 504 |
} |
| 505 |
|
| 506 |
JSONElement& JSONElement::addProperty(const wxString& name, const char* value, const wxMBConv& conv) |
| 507 |
{ |
| 508 |
return addProperty(name, wxString(value, conv)); |
| 509 |
} |
| 510 |
|
| 511 |
JSONElement JSONElement::detachProperty(const wxString& name) |
| 512 |
{ |
| 513 |
if(!_json) { return JSONElement(NULL); } |
| 514 |
cJSON* j = cJSON_DetachItemFromObject(_json, name.mb_str().data()); |
| 515 |
return JSONElement(j); |
| 516 |
} |