Debian Codegraph @

CodeLite/JSON.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
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.h"
27
28 #include "StringUtils.h"
29 #include "clFontHelper.h"
30 #include "fileutils.h"
31 #include "wx/dynarray.h"
32
33 #include <stdlib.h>
34 #include <wx/ffile.h>
35 #include <wx/filename.h>
36
37 JSON::JSON(const wxString& text)
38 : m_json(NULL)
39 {
40 m_json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
41 }
42
43 JSON::JSON(cJSON* json)
44 : m_json(json)
45 {
46 }
47
48 JSON::JSON(JSONItem item)
49 : m_json(item.release())
50 {
51 }
52
53 JSON::JSON(int type)
54 : m_json(NULL)
55 {
56 if(type == cJSON_Array)
57 m_json = cJSON_CreateArray();
58 else if(type == cJSON_NULL)
59 m_json = cJSON_CreateNull();
60 else
61 m_json = cJSON_CreateObject();
62 }
63
64 JSON::JSON(const wxFileName& filename)
65 : m_json(NULL)
66 {
67 wxString content;
68 if(!FileUtils::ReadFileContent(filename, content)) {
69 return;
70 }
71 m_json = cJSON_Parse(content.mb_str(wxConvUTF8).data());
72 }
73
74 JSON::~JSON()
75 {
76 if(m_json) {
77 cJSON_Delete(m_json);
78 m_json = NULL;
79 }
80 }
81
82 void JSON::save(const wxFileName& fn) const
83 {
84 if(!isOk()) {
85 FileUtils::WriteFileContent(fn, "{}");
86 } else {
87 FileUtils::WriteFileContent(fn, toElement().format(), wxConvUTF8);
88 }
89 }
90
91 JSONItem JSON::toElement() const
92 {
93 if(!m_json) {
94 return JSONItem(NULL);
95 }
96 return JSONItem(m_json);
97 }
98
99 wxString JSON::errorString() const { return _errorString; }
100
101 JSONItem JSONItem::namedObject(const wxString& name) const
102 {
103 if(!m_json) {
104 return JSONItem(NULL);
105 }
106
107 cJSON* obj = cJSON_GetObjectItem(m_json, name.mb_str(wxConvUTF8).data());
108 if(!obj) {
109 return JSONItem(NULL);
110 }
111 return JSONItem(obj);
112 }
113
114 void JSON::clear()
115 {
116 int type = cJSON_Object;
117 if(m_json) {
118 type = m_json->type;
119 cJSON_Delete(m_json);
120 m_json = NULL;
121 }
122 if(type == cJSON_Array)
123 m_json = cJSON_CreateArray();
124 else
125 m_json = cJSON_CreateObject();
126 }
127
128 cJSON* JSON::release()
129 {
130 cJSON* p = m_json;
131 m_json = NULL;
132 return p;
133 }
134
135 ///////////////////////////////////////////////////////////
136 ///////////////////////////////////////////////////////////
137 JSONItem::JSONItem(cJSON* json)
138 : m_json(json)
139 {
140 if(m_json) {
141 m_name = wxString(m_json->string, wxConvUTF8);
142 m_type = m_json->type;
143 }
144 }
145
146 JSONItem::JSONItem(const wxString& name, double val)
147 : m_name(name)
148 , m_type(cJSON_Number)
149 , m_valueNumer(val)
150 {
151 }
152
153 JSONItem::JSONItem(const wxString& name, const std::string& val)
154 : m_name(name)
155 , m_type(cJSON_String)
156 , m_valueString(val)
157 {
158 }
159
160 JSONItem::JSONItem(const wxString& name, const char* pval, size_t len)
161 : m_name(name)
162 , m_type(cJSON_String)
163 , m_valueString(pval, len)
164 {
165 }
166
167 JSONItem::JSONItem(const wxString& name, bool val)
168 : m_name(name)
169 , m_type(val ? cJSON_True : cJSON_False)
170 {
171 }
172
173 JSONItem JSONItem::operator[](int index) const
174 {
175 if(isArray()) {
176 return arrayItem(index);
177 }
178 return JSONItem(NULL);
179 }
180
181 JSONItem JSONItem::operator[](const wxString& name) const { return namedObject(name); }
182
183 JSONItem JSONItem::arrayItem(int pos) const
184 {
185 if(!m_json) {
186 return JSONItem(NULL);
187 }
188
189 if(m_json->type != cJSON_Array)
190 return JSONItem(NULL);
191
192 int size = cJSON_GetArraySize(m_json);
193 if(pos >= size)
194 return JSONItem(NULL);
195
196 return JSONItem(cJSON_GetArrayItem(m_json, pos));
197 }
198
199 bool JSONItem::isNull() const
200 {
201 if(!m_json) {
202 return false;
203 }
204 return m_json->type == cJSON_NULL;
205 }
206
207 bool JSONItem::toBool(bool defaultValue) const
208 {
209 if(!m_json) {
210 return defaultValue;
211 }
212
213 if(!isBool()) {
214 return defaultValue;
215 }
216
217 return m_json->type == cJSON_True;
218 }
219
220 wxString JSONItem::toString(const wxString& defaultValue) const
221 {
222 if(!m_json) {
223 return defaultValue;
224 }
225
226 if(m_json->type != cJSON_String) {
227 return defaultValue;
228 }
229
230 return wxString(m_json->valuestring, wxConvUTF8);
231 }
232
233 bool JSONItem::isBool() const
234 {
235 if(!m_json) {
236 return false;
237 }
238
239 return m_json->type == cJSON_True || m_json->type == cJSON_False;
240 }
241
242 bool JSONItem::isString() const
243 {
244 if(!m_json) {
245 return false;
246 }
247
248 return m_json->type == cJSON_String;
249 }
250
251 void JSONItem::append(const JSONItem& element)
252 {
253 if(!m_json) {
254 return;
255 }
256
257 switch(element.getType()) {
258 case cJSON_False:
259 cJSON_AddFalseToObject(m_json, element.getName().mb_str(wxConvUTF8).data());
260 break;
261
262 case cJSON_True:
263 cJSON_AddTrueToObject(m_json, element.getName().mb_str(wxConvUTF8).data());
264 break;
265
266 case cJSON_NULL:
267 cJSON_AddNullToObject(m_json, element.getName().mb_str(wxConvUTF8).data());
268 break;
269
270 case cJSON_Number:
271 cJSON_AddNumberToObject(m_json, element.m_name.c_str(), element.m_valueNumer);
272 break;
273
274 case cJSON_String:
275 cJSON_AddStringToObject(m_json, element.m_name.c_str(), element.m_valueString.c_str());
276 break;
277
278 case cJSON_Array:
279 case cJSON_Object:
280 cJSON_AddItemToObject(m_json, element.m_name.c_str(), element.m_json);
281 break;
282 }
283 }
284
285 void JSONItem::arrayAppend(const char* value)
286 {
287 if(!m_json) {
288 return;
289 }
290 cJSON* p = cJSON_CreateString(value);
291 cJSON_AddItemToArray(m_json, p);
292 }
293
294 void JSONItem::arrayAppend(const std::string& value) { arrayAppend((const char*)value.c_str()); }
295
296 void JSONItem::arrayAppend(double number)
297 {
298 if(!m_json) {
299 return;
300 }
301 cJSON* p = cJSON_CreateNumber(number);
302 cJSON_AddItemToArray(m_json, p);
303 }
304
305 void JSONItem::arrayAppend(int number) { arrayAppend((double)number); }
306
307 void JSONItem::arrayAppend(const wxString& value) { arrayAppend((const char*)value.mb_str(wxConvUTF8).data()); }
308
309 void JSONItem::arrayAppend(const JSONItem& element)
310 {
311 if(!m_json) {
312 return;
313 }
314
315 cJSON* p = NULL;
316 switch(element.getType()) {
317 case cJSON_False:
318 p = cJSON_CreateFalse();
319 break;
320
321 case cJSON_True:
322 p = cJSON_CreateTrue();
323 break;
324
325 case cJSON_NULL:
326 p = cJSON_CreateNull();
327 break;
328
329 case cJSON_Number:
330 p = cJSON_CreateNumber(element.m_valueNumer);
331 break;
332
333 case cJSON_String:
334 p = cJSON_CreateString(element.m_valueString.c_str());
335 break;
336 case cJSON_Array:
337 case cJSON_Object:
338 p = element.m_json;
339 break;
340 }
341 if(p) {
342 cJSON_AddItemToArray(m_json, p);
343 }
344 }
345
346 JSONItem JSONItem::createArray(const wxString& name)
347 {
348 JSONItem arr(cJSON_CreateArray());
349 arr.setName(name);
350 arr.setType(cJSON_Array);
351 return arr;
352 }
353
354 JSONItem JSONItem::createObject(const wxString& name)
355 {
356 JSONItem obj(cJSON_CreateObject());
357 obj.setName(name);
358 obj.setType(cJSON_Object);
359 return obj;
360 }
361
362 char* JSONItem::FormatRawString(bool formatted) const
363 {
364 if(!m_json) {
365 return NULL;
366 }
367
368 if(formatted) {
369 return cJSON_Print(m_json);
370
371 } else {
372 return cJSON_PrintUnformatted(m_json);
373 }
374 }
375
376 wxString JSONItem::format(bool formatted) const
377 {
378 if(!m_json) {
379 return wxT("");
380 }
381
382 char* p = formatted ? cJSON_Print(m_json) : cJSON_PrintUnformatted(m_json);
383 wxString s(p, wxConvUTF8);
384 free(p);
385 return s;
386 }
387
388 int JSONItem::toInt(int defaultVal) const
389 {
390 if(!m_json) {
391 return defaultVal;
392 }
393
394 if(m_json->type != cJSON_Number) {
395 return defaultVal;
396 }
397
398 return m_json->valueint;
399 }
400
401 size_t JSONItem::toSize_t(size_t defaultVal) const
402 {
403 if(!m_json) {
404 return defaultVal;
405 }
406
407 if(m_json->type != cJSON_Number) {
408 return defaultVal;
409 }
410
411 return (size_t)m_json->valueint;
412 }
413
414 double JSONItem::toDouble(double defaultVal) const
415 {
416 if(!m_json) {
417 return defaultVal;
418 }
419
420 if(m_json->type != cJSON_Number) {
421 return defaultVal;
422 }
423
424 return m_json->valuedouble;
425 }
426
427 int JSONItem::arraySize() const
428 {
429 if(!m_json) {
430 return 0;
431 }
432
433 if(m_json->type != cJSON_Array)
434 return 0;
435
436 return cJSON_GetArraySize(m_json);
437 }
438
439 JSONItem& JSONItem::addProperty(const wxString& name, bool value)
440 {
441 append(JSONItem(name, value));
442 return *this;
443 }
444
445 JSONItem& JSONItem::addProperty(const wxString& name, const wxString& value)
446 {
447 const wxCharBuffer cb = value.mb_str(wxConvUTF8);
448 append(JSONItem(name, cb.data(), cb.length()));
449 return *this;
450 }
451
452 JSONItem& JSONItem::addProperty(const wxString& name, const std::string& value)
453 {
454 append(JSONItem(name, value));
455 return *this;
456 }
457
458 JSONItem& JSONItem::addProperty(const wxString& name, const wxChar* value)
459 {
460 return addProperty(name, wxString(value));
461 }
462
463 JSONItem& JSONItem::addProperty(const wxString& name, long value)
464 {
465 append(JSONItem(name, (double)value));
466 return *this;
467 }
468
469 JSONItem& JSONItem::addProperty(const wxString& name, const wxArrayString& arr)
470 {
471 JSONItem arrEle = JSONItem::createArray(name);
472 for(size_t i = 0; i < arr.GetCount(); i++) {
473 arrEle.arrayAppend(arr.Item(i));
474 }
475 append(arrEle);
476 return *this;
477 }
478
479 wxArrayString JSONItem::toArrayString(const wxArrayString& defaultValue) const
480 {
481 if(!m_json) {
482 return defaultValue;
483 }
484
485 if(m_json->type != cJSON_Array) {
486 return defaultValue;
487 }
488
489 int arr_size = arraySize();
490 if(arr_size == 0) {
491 return defaultValue;
492 }
493
494 wxArrayString arr;
495 arr.reserve(arr_size);
496 auto child = m_json->child;
497 while(child) {
498 arr.push_back(wxString(child->valuestring, wxConvUTF8));
499 child = child->next;
500 }
501 return arr;
502 }
503
504 bool JSONItem::hasNamedObject(const wxString& name) const
505 {
506 if(!m_json) {
507 return false;
508 }
509
510 cJSON* obj = cJSON_GetObjectItem(m_json, name.mb_str(wxConvUTF8).data());
511 return obj != NULL;
512 }
513 #if wxUSE_GUI
514 JSONItem& JSONItem::addProperty(const wxString& name, const wxPoint& pt)
515 {
516 wxString szStr;
517 szStr << pt.x << "," << pt.y;
518 return addProperty(name, szStr);
519 }
520
521 JSONItem& JSONItem::addProperty(const wxString& name, const wxSize& sz)
522 {
523 wxString szStr;
524 szStr << sz.x << "," << sz.y;
525 return addProperty(name, szStr);
526 }
527
528 wxPoint JSONItem::toPoint() const
529 {
530 if(!m_json) {
531 return wxDefaultPosition;
532 }
533
534 if(m_json->type != cJSON_String) {
535 return wxDefaultPosition;
536 }
537
538 wxString str = m_json->valuestring;
539 wxString x = str.BeforeFirst(',');
540 wxString y = str.AfterFirst(',');
541
542 long nX(-1), nY(-1);
543 if(!x.ToLong(&nX) || !y.ToLong(&nY))
544 return wxDefaultPosition;
545
546 return wxPoint(nX, nY);
547 }
548
549 wxColour JSONItem::toColour(const wxColour& defaultColour) const
550 {
551 if(!m_json) {
552 return defaultColour;
553 }
554
555 if(m_json->type != cJSON_String) {
556 return defaultColour;
557 }
558 return wxColour(m_json->valuestring);
559 }
560
561 wxSize JSONItem::toSize() const
562 {
563 wxPoint pt = toPoint();
564 return wxSize(pt.x, pt.y);
565 }
566 #endif
567
568 JSONItem& JSONItem::addProperty(const wxString& name, const JSONItem& element)
569 {
570 if(!m_json) {
571 return *this;
572 }
573 cJSON_AddItemToObject(m_json, name.mb_str(wxConvUTF8).data(), element.m_json);
574 return *this;
575 }
576
577 void JSONItem::removeProperty(const wxString& name)
578 {
579 // delete child property
580 if(!m_json) {
581 return;
582 }
583 cJSON_DeleteItemFromObject(m_json, name.mb_str(wxConvUTF8).data());
584 }
585 #if wxUSE_GUI
586 JSONItem& JSONItem::addProperty(const wxString& name, const wxStringMap_t& stringMap)
587 {
588 if(!m_json)
589 return *this;
590
591 JSONItem arr = JSONItem::createArray(name);
592 wxStringMap_t::const_iterator iter = stringMap.begin();
593 for(; iter != stringMap.end(); ++iter) {
594 JSONItem obj = JSONItem::createObject();
595 obj.addProperty("key", iter->first);
596 obj.addProperty("value", iter->second);
597 arr.arrayAppend(obj);
598 }
599 append(arr);
600 return *this;
601 }
602 #endif
603 wxStringMap_t JSONItem::toStringMap() const
604 {
605 wxStringMap_t res;
606 if(!m_json) {
607 return res;
608 }
609
610 if(m_json->type != cJSON_Array) {
611 return res;
612 }
613
614 for(int i = 0; i < arraySize(); ++i) {
615 wxString key = arrayItem(i).namedObject("key").toString();
616 wxString val = arrayItem(i).namedObject("value").toString();
617 res.insert(std::make_pair(key, val));
618 }
619 return res;
620 }
621 JSONItem& JSONItem::addProperty(const wxString& name, size_t value) { return addProperty(name, (int)value); }
622
623 #if wxUSE_GUI
624 JSONItem& JSONItem::addProperty(const wxString& name, const wxColour& colour)
625 {
626 wxString colourValue;
627 if(colour.IsOk()) {
628 colourValue = colour.GetAsString(wxC2S_HTML_SYNTAX);
629 }
630 return addProperty(name, colourValue);
631 }
632 #endif
633 JSONItem JSONItem::firstChild()
634 {
635 m_walker = NULL;
636 if(!m_json) {
637 return JSONItem(NULL);
638 }
639
640 if(!m_json->child) {
641 return JSONItem(NULL);
642 }
643
644 m_walker = m_json->child;
645 return JSONItem(m_walker);
646 }
647
648 JSONItem JSONItem::nextChild()
649 {
650 if(!m_walker) {
651 return JSONItem(NULL);
652 }
653
654 JSONItem element(m_walker->next);
655 m_walker = m_walker->next;
656 return element;
657 }
658 JSONItem& JSONItem::addProperty(const wxString& name, const char* value, const wxMBConv& conv)
659 {
660 return addProperty(name, wxString(value, conv));
661 }
662
663 #if wxUSE_GUI
664 JSONItem& JSONItem::addProperty(const wxString& name, const wxFont& font)
665 {
666 return addProperty(name, clFontHelper::ToString(font));
667 }
668
669 wxFont JSONItem::toFont(const wxFont& defaultFont) const
670 {
671 wxString str = toString();
672 if(str.IsEmpty())
673 return defaultFont;
674 wxFont f = clFontHelper::FromString(str);
675 return f;
676 }
677 #endif
678
679 bool JSONItem::isArray() const
680 {
681 if(!m_json) {
682 return false;
683 }
684 return m_json->type == cJSON_Array;
685 }
686
687 bool JSONItem::isNumber() const
688 {
689 if(!m_json) {
690 return false;
691 }
692 return m_json->type == cJSON_Number;
693 }
694
695 JSONItem JSONItem::detachProperty(const wxString& name)
696 {
697 if(!m_json) {
698 return JSONItem(NULL);
699 }
700 cJSON* j = cJSON_DetachItemFromObject(m_json, name.c_str());
701 return JSONItem(j);
702 }
703
704 wxFileName JSONItem::toFileName() const
705 {
706 if(!m_json) {
707 return wxFileName();
708 }
709 return wxFileName(m_valueString);
710 }
711
712 JSONItem& JSONItem::addProperty(const wxString& name, const wxFileName& filename)
713 {
714 return addProperty(name, filename.GetFullPath());
715 }
716
717 JSONItem JSONItem::AddArray(const wxString& name)
718 {
719 JSONItem json = createArray(name);
720 append(json);
721 return json;
722 }
723
724 JSONItem JSONItem::AddObject(const wxString& name)
725 {
726 JSONItem json = createObject(name);
727 append(json);
728 return json;
729 }
730
731 JSONItem& JSONItem::addProperty(const wxString& name, cJSON* pjson)
732 {
733 if(!m_json) {
734 return *this;
735 }
736 cJSON_AddItemToObject(m_json, name.mb_str(wxConvUTF8).data(), pjson);
737 return *this;
738 }
739
740 std::vector<double> JSONItem::toDoubleArray(const std::vector<double>& defaultValue) const
741 {
742 if(!m_json) {
743 return defaultValue;
744 }
745
746 if(m_json->type != cJSON_Array) {
747 return defaultValue;
748 }
749
750 int arr_size = arraySize();
751 if(arr_size == 0) {
752 return defaultValue;
753 }
754
755 std::vector<double> arr;
756 arr.reserve(arr_size);
757 auto child = m_json->child;
758 while(child) {
759 arr.push_back(child->valuedouble);
760 child = child->next;
761 }
762 return arr;
763 }
764
765 std::vector<int> JSONItem::toIntArray(const std::vector<int>& defaultValue) const
766 {
767 if(!m_json) {
768 return defaultValue;
769 }
770
771 if(m_json->type != cJSON_Array) {
772 return defaultValue;
773 }
774
775 int arr_size = arraySize();
776 if(arr_size == 0) {
777 return defaultValue;
778 }
779
780 std::vector<int> arr;
781 arr.reserve(arr_size);
782 auto child = m_json->child;
783 while(child) {
784 arr.push_back(child->valueint);
785 child = child->next;
786 }
787 return arr;
788 }
789
790 JSONItem& JSONItem::addProperty(const wxString& name, const std::vector<int>& arr_int)
791 {
792 if(!m_json) {
793 return *this;
794 }
795
796 if(m_type != cJSON_Object) {
797 return *this;
798 }
799
800 // create array
801 JSONItem arr = AddArray(name);
802 for(size_t i = 0; i < arr_int.size(); ++i) {
803 cJSON_AddItemToArray(arr.m_json, cJSON_CreateNumber(arr_int[i]));
804 }
805 return *this;
806 }