OpenCPN Partial API docs
Loading...
Searching...
No Matches
jsonval.cpp
1
2// Name: jsonval.cpp
3// Purpose: the wxJSON class that holds a JSON value
4// Author: Luciano Cattani
5// Created: 2007/10/01
6// RCS-ID: $Id: jsonval.cpp,v 1.12 2008/03/06 10:25:18 luccat Exp $
7// Copyright: (c) 2007 Luciano Cattani
8// Licence: wxWidgets licence
10
11// #ifdef __GNUG__
12// #pragma implementation "jsonval.cpp"
13// #endif
14
15#ifdef NDEBUG
16// make wxLogTrace a noop if no debug set, it's really slow
17// must be defined before including wx/debug.h (also included by wx/wxprec.h)
18#define wxDEBUG_LEVEL 0
19#endif
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25#pragma hdrstop
26#endif
27
28#include <wx/log.h>
29#include <wx/arrimpl.cpp>
30
31#include <wx/jsonval.h>
32
33WX_DEFINE_OBJARRAY(wxJSONInternalArray);
34
35#if wxCHECK_VERSION(3, 0, 0)
36#define compatibleLongLongFmtSpec _T(wxLongLongFmtSpec)
37#else
38#define compatibleLongLongFmtSpec wxLongLongFmtSpec
39#endif
40
41#if wxDEBUG_LEVEL > 0
42// the trace mask used in wxLogTrace() function
43// static const wxChar* traceMask = _T("jsonval");
44static const wxChar* traceMask = _T("jsonval");
45static const wxChar* compareTraceMask = _T("sameas");
46static const wxChar* cowTraceMask = _T("traceCOW" );
47#endif
48
49/*******************************************************************
50
51 class wxJSONRefData
52
53*******************************************************************/
54
66#if defined(WXJSON_USE_VALUE_COUNTER)
67// The progressive counter (used for debugging only)
68int wxJSONRefData::sm_progr = 1;
69#endif
70
73 m_lineNo = -1;
74 m_refCount = 1;
75 m_memBuff = 0;
76
77#if defined(WXJSON_USE_VALUE_COUNTER)
78 m_progr = sm_progr;
79 ++sm_progr;
80 wxLogTrace(traceMask, _T("(%s) JSON refData ctor progr=%d"),
81 __PRETTY_FUNCTION__, m_progr);
82#endif
83}
84
85// Dtor
86wxJSONRefData::~wxJSONRefData() {
87 if (m_memBuff) {
88 delete m_memBuff;
89 }
90}
91
92// Return the number of objects that reference this data.
93int wxJSONRefData::GetRefCount() const { return m_refCount; }
94
95/*******************************************************************
96
97 class wxJSONValue
98
99*******************************************************************/
100
160#if defined(WXJSON_USE_VALUE_COUNTER)
161// The progressive counter (used for debugging only)
162int wxJSONValue::sm_progr = 1;
163#endif
164
166
184 m_refData = 0;
185 Init(wxJSONTYPE_NULL);
186}
187
189
196wxJSONRefData* wxJSONValue::Init(wxJSONType type) {
197 wxJSONRefData* data = GetRefData();
198 if (data != 0) {
199 UnRef();
200 }
201
202 // we allocate a new instance of the referenced data
203 data = new wxJSONRefData();
204 wxJSON_ASSERT(data);
205
206 // in release builds we do not have ASSERT so we check 'data' before
207 // using it
208 if (data) {
209 data->m_type = type;
210 data->m_commentPos = wxJSONVALUE_COMMENT_BEFORE;
211 }
212 SetRefData(data);
213
214#if defined(WXJSON_USE_VALUE_COUNTER)
215 m_progr = sm_progr;
216 ++sm_progr;
217 wxLogTrace(cowTraceMask, _T("(%s) Init a new object progr=%d"),
218 __PRETTY_FUNCTION__, m_progr);
219#endif
220 return data;
221}
222
224wxJSONValue::wxJSONValue(wxJSONType type) {
225 m_refData = 0;
226 Init(type);
227}
228
231 m_refData = 0;
232 wxJSONRefData* data = Init(wxJSONTYPE_INT);
233 wxJSON_ASSERT(data);
234 if (data != 0) {
235 // the 'VAL_INT' macro expands to 'm_valLong' or 'm_valInt64' depending
236 // on 64-bits integer support being enabled on not
237 data->m_value.VAL_INT = i;
238 }
239}
240
242wxJSONValue::wxJSONValue(unsigned int ui) {
243 m_refData = 0;
244 wxJSONRefData* data = Init(wxJSONTYPE_UINT);
245 wxJSON_ASSERT(data);
246 if (data != 0) {
247 // the 'VAL_UINT' macro expands to 'm_valULong' or 'm_valUInt64' depending
248 // on 64-bits integer support being enabled on not
249 data->m_value.VAL_UINT = ui;
250 }
251}
252
254wxJSONValue::wxJSONValue(short int i) {
255 m_refData = 0;
256 wxJSONRefData* data = Init(wxJSONTYPE_INT);
257 wxJSON_ASSERT(data);
258 if (data != 0) {
259 // the 'VAL_INT' macro expands to 'm_valLong' or 'm_valInt64' depending
260 // on 64-bits integer support being enabled on not
261 data->m_value.VAL_INT = i;
262 }
263}
264
266wxJSONValue::wxJSONValue(unsigned short ui) {
267 m_refData = 0;
268 wxJSONRefData* data = Init(wxJSONTYPE_UINT);
269 wxJSON_ASSERT(data);
270 if (data != 0) {
271 // the 'VAL_UINT' macro expands to 'm_valULong' or 'm_valUInt64' depending
272 // on 64-bits integer support being enabled on not
273 data->m_value.VAL_UINT = ui;
274 }
275}
276
279 m_refData = 0;
280 wxJSONRefData* data = Init(wxJSONTYPE_BOOL);
281 wxJSON_ASSERT(data);
282 if (data != 0) {
283 data->m_value.m_valBool = b;
284 }
285}
286
288wxJSONValue::wxJSONValue(double d) {
289 m_refData = 0;
290 wxJSONRefData* data = Init(wxJSONTYPE_DOUBLE);
291 wxJSON_ASSERT(data);
292 if (data != 0) {
293 data->m_value.m_valDouble = d;
294 }
295}
296
298wxJSONValue::wxJSONValue(const wxChar* str) {
299 m_refData = 0;
300 wxJSONRefData* data = Init(wxJSONTYPE_CSTRING);
301 wxJSON_ASSERT(data);
302 if (data != 0) {
303#if !defined(WXJSON_USE_CSTRING)
304 data->m_type = wxJSONTYPE_STRING;
305 data->m_valString.assign(str);
306#else
307 data->m_value.m_valCString = str;
308#endif
309 }
310}
311
313wxJSONValue::wxJSONValue(const wxString& str) {
314 m_refData = 0;
315 wxJSONRefData* data = Init(wxJSONTYPE_STRING);
316 wxJSON_ASSERT(data);
317 if (data != 0) {
318 data->m_valString.assign(str);
319 }
320}
321
323wxJSONValue::wxJSONValue(long int l) {
324 m_refData = 0;
325 wxJSONRefData* data = Init(wxJSONTYPE_INT);
326 wxJSON_ASSERT(data);
327 if (data != 0) {
328 data->m_value.VAL_INT = l;
329 }
330}
331
333wxJSONValue::wxJSONValue(unsigned long int ul) {
334 m_refData = 0;
335 wxJSONRefData* data = Init(wxJSONTYPE_UINT);
336 wxJSON_ASSERT(data);
337 if (data != 0) {
338 data->m_value.VAL_UINT = ul;
339 }
340}
341
343
348wxJSONValue::wxJSONValue(const wxMemoryBuffer& buff) {
349 m_refData = 0;
350 wxJSONRefData* data = Init(wxJSONTYPE_MEMORYBUFF);
351 wxJSON_ASSERT(data);
352 if (data != 0) {
353 data->m_memBuff = new wxMemoryBuffer();
354 const void* ptr = buff.GetData();
355 size_t buffLen = buff.GetDataLen();
356 if (buffLen > 0) {
357 data->m_memBuff->AppendData(ptr, buffLen);
358 }
359 }
360}
361
363
368wxJSONValue::wxJSONValue(const void* buff, size_t len) {
369 m_refData = 0;
370 wxJSONRefData* data = Init(wxJSONTYPE_MEMORYBUFF);
371 wxJSON_ASSERT(data);
372 if (data != 0 && len > 0) {
373 data->m_memBuff = new wxMemoryBuffer();
374 data->m_memBuff->AppendData(buff, len);
375 }
376}
377
379
387 m_refData = 0;
388 Ref(other);
389
390 // the progressive counter of the ctor is not copied from
391 // the other wxJSONValue object: only data is shared, the
392 // progressive counter is not shared because this object
393 // is a copy of 'other' and it has its own progressive
394#if defined(WXJSON_USE_VALUE_COUNTER)
395 m_progr = sm_progr;
396 ++sm_progr;
397 wxLogTrace(cowTraceMask, _T("(%s) Copy ctor - progr=%d other progr=%d"),
398 __PRETTY_FUNCTION__, m_progr, other.m_progr);
399#endif
400}
401
404
405// functions for retreiving the value type: they are all 'const'
406
408
458wxJSONType wxJSONValue::GetType() const {
459 wxJSONRefData* data = GetRefData();
460 wxJSONType type = wxJSONTYPE_INVALID;
461 if (data) {
462 type = data->m_type;
463
464 // for integers and unsigned ints check the storage requirements
465 // note that ints are stored as 'long' or as 'long long'
466 switch (type) {
467 case wxJSONTYPE_INT:
468 // check if the integer fits in a SHORT INT
469 if (data->m_value.VAL_INT >= SHORT_MIN &&
470 data->m_value.VAL_INT <= SHORT_MAX) {
471 type = wxJSONTYPE_SHORT;
472 }
473 // check if the value fits in LONG INT
474 else if (data->m_value.VAL_INT >= LONG_MIN &&
475 data->m_value.VAL_INT <= LONG_MAX) {
476 type = wxJSONTYPE_LONG;
477 } else {
478 type = wxJSONTYPE_INT64;
479 }
480 break;
481
482 case wxJSONTYPE_UINT:
483 if (data->m_value.VAL_UINT <= USHORT_MAX) {
484 type = wxJSONTYPE_USHORT;
485 } else if (data->m_value.VAL_UINT <= ULONG_MAX) {
486 type = wxJSONTYPE_ULONG;
487 } else {
488 type = wxJSONTYPE_UINT64;
489 }
490 break;
491
492 default:
493 break;
494 }
495 }
496 return type;
497}
498
500bool wxJSONValue::IsNull() const {
501 wxJSONType type = GetType();
502 bool r = false;
503 if (type == wxJSONTYPE_NULL) {
504 r = true;
505 }
506 return r;
507}
508
510
521bool wxJSONValue::IsValid() const {
522 wxJSONType type = GetType();
523 bool r = false;
524 if (type != wxJSONTYPE_INVALID) {
525 r = true;
526 }
527 return r;
528}
529
531
552bool wxJSONValue::IsInt() const {
553 wxJSONType type = GetType();
554 bool r = false;
555 // if the type is SHORT the value fits into an INT, too
556 if (type == wxJSONTYPE_SHORT) {
557 r = true;
558 } else if (type == wxJSONTYPE_LONG) {
559 // in case of LONG, check if the bit width is the same
560 if (INT_MAX == LONG_MAX) {
561 r = true;
562 }
563 }
564 return r;
565}
566
568
581bool wxJSONValue::IsShort() const {
582 wxJSONType type = GetType();
583 bool r = false;
584 if (type == wxJSONTYPE_SHORT) {
585 r = true;
586 }
587 return r;
588}
589
591
613bool wxJSONValue::IsUInt() const {
614 wxJSONType type = GetType();
615 bool r = false;
616 if (type == wxJSONTYPE_USHORT) {
617 r = true;
618 } else if (type == wxJSONTYPE_ULONG) {
619 if (INT_MAX == LONG_MAX) {
620 r = true;
621 }
622 }
623 return r;
624}
625
627
640bool wxJSONValue::IsUShort() const {
641 wxJSONType type = GetType();
642 bool r = false;
643 if (type == wxJSONTYPE_USHORT) {
644 r = true;
645 }
646 return r;
647}
648
650
663bool wxJSONValue::IsLong() const {
664 wxJSONType type = GetType();
665 bool r = false;
666 if (type == wxJSONTYPE_LONG || type == wxJSONTYPE_SHORT) {
667 r = true;
668 }
669 return r;
670}
671
674
687bool wxJSONValue::IsULong() const {
688 wxJSONType type = GetType();
689 bool r = false;
690 if (type == wxJSONTYPE_ULONG || type == wxJSONTYPE_USHORT) {
691 r = true;
692 }
693 return r;
694}
695
697bool wxJSONValue::IsBool() const {
698 wxJSONType type = GetType();
699 bool r = false;
700 if (type == wxJSONTYPE_BOOL) {
701 r = true;
702 }
703 return r;
704}
705
707bool wxJSONValue::IsDouble() const {
708 wxJSONType type = GetType();
709 bool r = false;
710 if (type == wxJSONTYPE_DOUBLE) {
711 r = true;
712 }
713 return r;
714}
715
717bool wxJSONValue::IsString() const {
718 wxJSONType type = GetType();
719 bool r = false;
720 if (type == wxJSONTYPE_STRING) {
721 r = true;
722 }
723 return r;
724}
725
728
736bool wxJSONValue::IsCString() const {
737 wxJSONType type = GetType();
738 bool r = false;
739 if (type == wxJSONTYPE_CSTRING) {
740 r = true;
741 }
742 return r;
743}
744
746bool wxJSONValue::IsArray() const {
747 wxJSONType type = GetType();
748 bool r = false;
749 if (type == wxJSONTYPE_ARRAY) {
750 r = true;
751 }
752 return r;
753}
754
756bool wxJSONValue::IsObject() const {
757 wxJSONType type = GetType();
758 bool r = false;
759 if (type == wxJSONTYPE_OBJECT) {
760 r = true;
761 }
762 return r;
763}
764
766bool wxJSONValue::IsMemoryBuff() const {
767 wxJSONType type = GetType();
768 bool r = false;
769 if (type == wxJSONTYPE_MEMORYBUFF) {
770 r = true;
771 }
772 return r;
773}
774
775// get the stored value; all these functions are 'const'
776
778
789int wxJSONValue::AsInt() const {
790 wxJSONRefData* data = GetRefData();
791 wxJSON_ASSERT(data);
792 int i = (int)data->m_value.VAL_INT;
793
794 wxJSON_ASSERT(IsInt());
795 return i;
796}
797
799
809bool wxJSONValue::AsBool() const {
810 wxJSONRefData* data = GetRefData();
811 wxJSON_ASSERT(data);
812 wxJSON_ASSERT(data->m_type == wxJSONTYPE_BOOL);
813 return data->m_value.m_valBool;
814}
815
817
827double wxJSONValue::AsDouble() const {
828 wxJSONRefData* data = GetRefData();
829 wxJSON_ASSERT(data);
830 double d = data->m_value.m_valDouble;
831 wxJSON_ASSERT(IsDouble());
832 return d;
833}
834
836
872wxString wxJSONValue::AsString() const {
873 wxJSONRefData* data = GetRefData();
874 wxJSON_ASSERT(data);
875 wxString s;
876 int size = Size();
877 switch (data->m_type) {
878 case wxJSONTYPE_STRING:
879 s.assign(data->m_valString);
880 break;
881 case wxJSONTYPE_CSTRING:
882 s.assign(data->m_value.m_valCString);
883 break;
884 case wxJSONTYPE_INT:
885#if defined(wxJSON_64BIT_INT)
886 s.Printf(_T("%") compatibleLongLongFmtSpec _T("i"),
887 data->m_value.m_valInt64);
888#else
889 s.Printf(_T("%ld"), data->m_value.m_valLong);
890#endif
891 break;
892 case wxJSONTYPE_UINT:
893#if defined(wxJSON_64BIT_INT)
894 s.Printf(_T("%") compatibleLongLongFmtSpec _T("u"),
895 data->m_value.m_valUInt64);
896#else
897 s.Printf(_T("%lu"), data->m_value.m_valULong);
898#endif
899 break;
900 case wxJSONTYPE_DOUBLE:
901 s.Printf(_T("%.10g"), data->m_value.m_valDouble);
902 break;
903 case wxJSONTYPE_BOOL:
904 s.assign((data->m_value.m_valBool ? _T("true") : _T("false")));
905 break;
906 case wxJSONTYPE_NULL:
907 s.assign(_T( "null"));
908 break;
909 case wxJSONTYPE_INVALID:
910 s.assign(_T( "<invalid>"));
911 break;
912 case wxJSONTYPE_ARRAY:
913 s.Printf(_T("[%d]"), size);
914 break;
915 case wxJSONTYPE_OBJECT:
916 s.Printf(_T("{%d}"), size);
917 break;
918 case wxJSONTYPE_MEMORYBUFF:
919 s = MemoryBuffToString(*(data->m_memBuff), 5);
920 break;
921 default:
922 s.assign(_T( "wxJSONValue::AsString(): Unknown JSON type \'"));
923 s.append(TypeToString(data->m_type));
924 s.append(_T( "\'" ));
925 wxFAIL_MSG(s);
926 break;
927 }
928 return s;
929}
930
932
947const wxChar* wxJSONValue::AsCString() const {
948 const wxChar* s = 0;
949 wxJSONRefData* data = GetRefData();
950 wxJSON_ASSERT(data);
951 switch (data->m_type) {
952 case wxJSONTYPE_CSTRING:
953 s = data->m_value.m_valCString;
954 break;
955 case wxJSONTYPE_STRING:
956 s = data->m_valString.c_str();
957 break;
958 default:
959 break;
960 }
961 return s;
962}
963
965
976unsigned int wxJSONValue::AsUInt() const {
977 wxJSONRefData* data = GetRefData();
978 wxJSON_ASSERT(data);
979 unsigned int ui = (unsigned)data->m_value.VAL_UINT;
980
981 wxJSON_ASSERT(IsUInt());
982 return ui;
983}
984
986
997long int wxJSONValue::AsLong() const {
998 long int l;
999 wxJSONRefData* data = GetRefData();
1000 wxJSON_ASSERT(data);
1001 l = (long)data->m_value.VAL_INT;
1002
1003 wxJSON_ASSERT(IsLong());
1004 return l;
1005}
1006
1008
1019unsigned long int wxJSONValue::AsULong() const {
1020 wxJSONRefData* data = GetRefData();
1021 wxJSON_ASSERT(data);
1022 unsigned long int ul = (unsigned long)data->m_value.VAL_UINT;
1023
1024 wxJSON_ASSERT(IsULong()); // expands only in debug builds
1025 return ul;
1026}
1027
1029
1040short int wxJSONValue::AsShort() const {
1041 short int i;
1042 wxJSONRefData* data = GetRefData();
1043 wxJSON_ASSERT(data);
1044 i = (short)data->m_value.VAL_INT;
1045
1046 wxJSON_ASSERT(IsShort());
1047 return i;
1048}
1049
1051
1062unsigned short wxJSONValue::AsUShort() const {
1063 unsigned short ui;
1064 wxJSONRefData* data = GetRefData();
1065 wxJSON_ASSERT(data);
1066 ui = (unsigned short)data->m_value.VAL_UINT;
1067
1068 wxJSON_ASSERT(IsUShort());
1069 return ui;
1070}
1071
1073
1098bool wxJSONValue::AsInt(int& i) const {
1099 bool r = false;
1100 if (IsInt()) {
1101 i = AsInt();
1102 r = true;
1103 }
1104 return r;
1105}
1106
1107bool wxJSONValue::AsUInt(unsigned int& ui) const {
1108 bool r = false;
1109 if (IsUInt()) {
1110 ui = AsUInt();
1111 r = true;
1112 }
1113 return r;
1114}
1115
1116bool wxJSONValue::AsShort(short int& s) const {
1117 bool r = false;
1118 if (IsShort()) {
1119 s = AsShort();
1120 r = true;
1121 }
1122 return r;
1123}
1124
1125bool wxJSONValue::AsUShort(unsigned short& us) const {
1126 bool r = false;
1127 if (IsUShort()) {
1128 us = AsUShort();
1129 r = true;
1130 }
1131 return r;
1132}
1133
1134bool wxJSONValue::AsLong(long int& l) const {
1135 bool r = false;
1136 if (IsLong()) {
1137 l = AsLong();
1138 r = true;
1139 }
1140 return r;
1141}
1142
1143bool wxJSONValue::AsULong(unsigned long& ul) const {
1144 bool r = false;
1145 if (IsULong()) {
1146 ul = AsULong();
1147 r = true;
1148 }
1149 return r;
1150}
1151
1152bool wxJSONValue::AsBool(bool& b) const {
1153 bool r = false;
1154 if (IsBool()) {
1155 b = AsBool();
1156 r = true;
1157 }
1158 return r;
1159}
1160
1161bool wxJSONValue::AsDouble(double& d) const {
1162 bool r = false;
1163 if (IsDouble()) {
1164 d = AsDouble();
1165 r = true;
1166 }
1167 return r;
1168}
1169
1171
1180bool wxJSONValue::AsString(wxString& str) const {
1181 bool r = IsString();
1182 if (r) {
1183 str = AsString();
1184 }
1185 return r;
1186}
1187
1188bool wxJSONValue::AsCString(wxChar* ch) const {
1189 bool r = IsCString();
1190 if (r) {
1191 ch = (wxChar*)AsCString();
1192 }
1193 return r;
1194}
1195
1197
1215wxMemoryBuffer wxJSONValue::AsMemoryBuff() const {
1216 wxJSONRefData* data = GetRefData();
1217 wxJSON_ASSERT(data);
1218 wxMemoryBuffer buff;
1219 if (data->m_memBuff) {
1220 buff = *(data->m_memBuff);
1221 }
1222
1223 wxJSON_ASSERT(IsMemoryBuff());
1224 return buff;
1225}
1226
1228
1246bool wxJSONValue::AsMemoryBuff(wxMemoryBuffer& buff) const {
1247 bool r = IsMemoryBuff();
1248 if (r) {
1249 buff = AsMemoryBuff();
1250 }
1251 return r;
1252}
1253
1254// internal use
1255
1257
1263const wxJSONInternalMap* wxJSONValue::AsMap() const {
1264 wxJSONRefData* data = GetRefData();
1265 wxJSON_ASSERT(data);
1266
1267 const wxJSONInternalMap* v = 0;
1268 if (data->m_type == wxJSONTYPE_OBJECT) {
1269 v = &(data->m_valMap);
1270 }
1271 return v;
1272}
1273
1275
1281const wxJSONInternalArray* wxJSONValue::AsArray() const {
1282 wxJSONRefData* data = GetRefData();
1283 wxJSON_ASSERT(data);
1284
1285 const wxJSONInternalArray* v = 0;
1286 if (data->m_type == wxJSONTYPE_ARRAY) {
1287 v = &(data->m_valArray);
1288 }
1289 return v;
1290}
1291
1292// retrieve the members and other info
1293
1295
1298bool wxJSONValue::HasMember(unsigned index) const {
1299 bool r = false;
1300 int size = Size();
1301 if (index < (unsigned)size) {
1302 r = true;
1303 }
1304 return r;
1305}
1306
1308
1311bool wxJSONValue::HasMember(const wxString& key) const {
1312 bool r = false;
1313 wxJSONRefData* data = GetRefData();
1314 wxJSON_ASSERT(data);
1315
1316 if (data && data->m_type == wxJSONTYPE_OBJECT) {
1317 wxJSONInternalMap::iterator it = data->m_valMap.find(key);
1318 if (it != data->m_valMap.end()) {
1319 r = true;
1320 }
1321 }
1322 return r;
1323}
1324
1326
1332int wxJSONValue::Size() const {
1333 wxJSONRefData* data = GetRefData();
1334 wxJSON_ASSERT(data);
1335
1336 int size = -1;
1337 if (data->m_type == wxJSONTYPE_ARRAY) {
1338 size = (int)data->m_valArray.GetCount();
1339 }
1340 if (data->m_type == wxJSONTYPE_OBJECT) {
1341 size = (int)data->m_valMap.size();
1342 }
1343 return size;
1344}
1345
1347
1357wxArrayString wxJSONValue::GetMemberNames() const {
1358 wxJSONRefData* data = GetRefData();
1359 wxJSON_ASSERT(data);
1360 wxJSON_ASSERT(data->m_type == wxJSONTYPE_OBJECT);
1361
1362 wxArrayString arr;
1363 if (data->m_type == wxJSONTYPE_OBJECT) {
1364 wxJSONInternalMap::iterator it;
1365 for (it = data->m_valMap.begin(); it != data->m_valMap.end(); it++) {
1366 arr.Add(it->first);
1367 }
1368 }
1369 return arr;
1370}
1371
1372// appending items, resizing and deleting items
1373// NOTE: these functions are not 'const' so we have to call
1374// the COW() function before accessing data
1375
1377
1386 wxJSONRefData* data = COW();
1387 wxJSON_ASSERT(data);
1388 if (data->m_type != wxJSONTYPE_ARRAY) {
1389 // we have to change the type of the actual object to the array type
1390 SetType(wxJSONTYPE_ARRAY);
1391 }
1392 // we add the wxJSONValue object to the wxObjArray: note that the
1393 // array makes a copy of the JSON-value object by calling its
1394 // copy ctor thus using reference count
1395 data->m_valArray.Add(value);
1396 wxJSONValue& v = data->m_valArray.Last();
1397 return v;
1398}
1399
1402 wxJSONValue v(i);
1403 wxJSONValue& r = Append(v);
1404 return r;
1405}
1406
1408wxJSONValue& wxJSONValue::Append(short int i) {
1409 wxJSONValue v(i);
1410 wxJSONValue& r = Append(v);
1411 return r;
1412}
1413
1415wxJSONValue& wxJSONValue::Append(long int l) {
1416 wxJSONValue v(l);
1417 wxJSONValue& r = Append(v);
1418 return r;
1419}
1420
1423 wxJSONValue v(b);
1424 wxJSONValue& r = Append(v);
1425 return r;
1426}
1427
1429wxJSONValue& wxJSONValue::Append(unsigned int ui) {
1430 wxJSONValue v(ui);
1431 wxJSONValue& r = Append(v);
1432 return r;
1433}
1434
1436wxJSONValue& wxJSONValue::Append(unsigned short ui) {
1437 wxJSONValue v(ui);
1438 wxJSONValue& r = Append(v);
1439 return r;
1440}
1441
1443wxJSONValue& wxJSONValue::Append(unsigned long ul) {
1444 wxJSONValue v(ul);
1445 wxJSONValue& r = Append(v);
1446 return r;
1447}
1448
1451 wxJSONValue v(d);
1452 wxJSONValue& r = Append(v);
1453 return r;
1454}
1455
1457wxJSONValue& wxJSONValue::Append(const wxChar* str) {
1458 wxJSONValue v(str);
1459 wxJSONValue& r = Append(v);
1460 return r;
1461}
1462
1464wxJSONValue& wxJSONValue::Append(const wxString& str) {
1465 wxJSONValue v(str);
1466 wxJSONValue& r = Append(v);
1467 return r;
1468}
1469
1471wxJSONValue& wxJSONValue::Append(const wxMemoryBuffer& buff) {
1472 wxJSONValue v(buff);
1473 wxJSONValue& r = Append(v);
1474 return r;
1475}
1476
1478wxJSONValue& wxJSONValue::Append(const void* buff, size_t len) {
1479 wxJSONValue v(buff, len);
1480 wxJSONValue& r = Append(v);
1481 return r;
1482}
1483
1485
1493bool wxJSONValue::Cat(const wxString& str) {
1494 wxJSONRefData* data = GetRefData();
1495 wxJSON_ASSERT(data);
1496
1497 bool r = false;
1498 if (data->m_type == wxJSONTYPE_STRING) {
1499 wxJSONRefData* data = COW();
1500 wxJSON_ASSERT(data);
1501 data->m_valString.append(str);
1502 r = true;
1503 }
1504 return r;
1505}
1506
1508
1514bool wxJSONValue::Cat(const wxMemoryBuffer& buff) {
1515 wxJSONRefData* data = GetRefData();
1516 wxJSON_ASSERT(data);
1517
1518 bool r = false;
1519 if (data->m_type == wxJSONTYPE_MEMORYBUFF) {
1520 wxJSONRefData* data = COW();
1521 wxJSON_ASSERT(data);
1522 data->m_memBuff->AppendData(buff.GetData(), buff.GetDataLen());
1523 r = true;
1524 }
1525 return r;
1526}
1527
1529bool wxJSONValue::Cat(const wxChar* str) {
1530 wxJSONRefData* data = GetRefData();
1531 wxJSON_ASSERT(data);
1532
1533 bool r = false;
1534 if (data->m_type == wxJSONTYPE_STRING) {
1535 wxJSONRefData* data = COW();
1536 wxJSON_ASSERT(data);
1537 data->m_valString.append(str);
1538 r = true;
1539 }
1540 return r;
1541}
1542
1544
1551bool wxJSONValue::Remove(int index) {
1552 wxJSONRefData* data = COW();
1553 wxJSON_ASSERT(data);
1554
1555 bool r = false;
1556 if (data->m_type == wxJSONTYPE_ARRAY) {
1557 data->m_valArray.RemoveAt(index);
1558 r = true;
1559 }
1560 return r;
1561}
1562
1564bool wxJSONValue::Remove(const wxString& key) {
1565 wxJSONRefData* data = COW();
1566 wxJSON_ASSERT(data);
1567
1568 bool r = false;
1569 if (data->m_type == wxJSONTYPE_OBJECT) {
1570 wxJSONInternalMap::size_type count = data->m_valMap.erase(key);
1571 if (count > 0) {
1572 r = true;
1573 }
1574 }
1575 return r;
1576}
1577
1579
1584void wxJSONValue::Clear() {
1585 UnRef();
1586 SetType(wxJSONTYPE_INVALID);
1587}
1588
1589// retrieve an item
1590
1592
1602wxJSONValue& wxJSONValue::Item(unsigned index) {
1603 wxJSONRefData* data = COW();
1604 wxJSON_ASSERT(data);
1605
1606 if (data->m_type != wxJSONTYPE_ARRAY) {
1607 data = SetType(wxJSONTYPE_ARRAY);
1608 }
1609 int size = Size();
1610 wxJSON_ASSERT(size >= 0);
1611 // if the desired element does not yet exist, we create as many
1612 // elements as needed; the new values will be 'null' values
1613 if (index >= (unsigned)size) {
1614 wxJSONValue v(wxJSONTYPE_NULL);
1615 int missing = index - size + 1;
1616 data->m_valArray.Add(v, missing);
1617 }
1618 return data->m_valArray.Item(index);
1619}
1620
1622
1630wxJSONValue& wxJSONValue::Item(const wxString& key) {
1631 wxLogTrace(traceMask, _T("(%s) searched key=\'%s\'"), __PRETTY_FUNCTION__,
1632 key.c_str());
1633#if !wxCHECK_VERSION(2, 9, 0)
1634 wxLogTrace(traceMask, _T("(%s) actual object: %s"), __PRETTY_FUNCTION__,
1635 GetInfo().c_str());
1636#endif
1637
1638 wxJSONRefData* data = COW();
1639 wxJSON_ASSERT(data);
1640
1641 if (data->m_type != wxJSONTYPE_OBJECT) {
1642 // deletes the contained value;
1643 data = SetType(wxJSONTYPE_OBJECT);
1644 return data->m_valMap[key];
1645 }
1646 wxLogTrace(traceMask, _T("(%s) searching key \'%s' in the actual object"),
1647 __PRETTY_FUNCTION__, key.c_str());
1648 return data->m_valMap[key];
1649}
1650
1652
1657wxJSONValue wxJSONValue::ItemAt(unsigned index) const {
1658 wxJSONRefData* data = GetRefData();
1659 wxJSON_ASSERT(data);
1660
1661 wxJSONValue v(wxJSONTYPE_INVALID);
1662 if (data->m_type == wxJSONTYPE_ARRAY) {
1663 int size = Size();
1664 wxJSON_ASSERT(size >= 0);
1665 if (index < (unsigned)size) {
1666 v = data->m_valArray.Item(index);
1667 }
1668 }
1669 return v;
1670}
1671
1673
1678wxJSONValue wxJSONValue::ItemAt(const wxString& key) const {
1679 wxLogTrace(traceMask, _T("(%s) searched key=\'%s\'"), __PRETTY_FUNCTION__,
1680 key.c_str());
1681#ifndef __WXOSX__
1682 wxLogTrace(traceMask, _T("(%s) actual object: %s"), __PRETTY_FUNCTION__,
1683 GetInfo().c_str());
1684#endif
1685
1686 wxJSONRefData* data = GetRefData();
1687 wxJSON_ASSERT(data);
1688
1689 wxJSONValue v(wxJSONTYPE_INVALID);
1690 if (data->m_type == wxJSONTYPE_OBJECT) {
1691 wxJSONInternalMap::const_iterator it = data->m_valMap.find(key);
1692 if (it != data->m_valMap.end()) {
1693 v = it->second;
1694 }
1695 }
1696 return v;
1697}
1698
1700
1709wxJSONValue& wxJSONValue::operator[](unsigned index) {
1710 wxJSONValue& v = Item(index);
1711 return v;
1712}
1713
1715
1723wxJSONValue& wxJSONValue::operator[](const wxString& key) {
1724 wxJSONValue& v = Item(key);
1725 return v;
1726}
1727
1728//
1729// assignment operators
1730// note that reference counting is only used if the original
1731// value is a wxJSONValue object
1732// in all other cases, the operator= function deletes the old
1733// content and assigns the new one
1734
1736
1750 wxJSONRefData* data = SetType(wxJSONTYPE_INT);
1751 data->m_value.VAL_INT = i;
1752 return *this;
1753}
1754
1757 wxJSONRefData* data = SetType(wxJSONTYPE_BOOL);
1758 data->m_value.m_valBool = b;
1759 return *this;
1760}
1761
1763wxJSONValue& wxJSONValue::operator=(unsigned int ui) {
1764 wxJSONRefData* data = SetType(wxJSONTYPE_UINT);
1765 data->m_value.VAL_UINT = ui;
1766 return *this;
1767}
1768
1771 wxJSONRefData* data = SetType(wxJSONTYPE_INT);
1772 data->m_value.VAL_INT = l;
1773 return *this;
1774}
1775
1777wxJSONValue& wxJSONValue::operator=(unsigned long ul) {
1778 wxJSONRefData* data = SetType(wxJSONTYPE_UINT);
1779 data->m_value.VAL_UINT = ul;
1780 return *this;
1781}
1782
1785 wxJSONRefData* data = SetType(wxJSONTYPE_INT);
1786 data->m_value.VAL_INT = i;
1787 return *this;
1788}
1789
1791wxJSONValue& wxJSONValue::operator=(unsigned short ui) {
1792 wxJSONRefData* data = SetType(wxJSONTYPE_UINT);
1793 data->m_value.VAL_UINT = ui;
1794 return *this;
1795}
1796
1799 wxJSONRefData* data = SetType(wxJSONTYPE_DOUBLE);
1800 data->m_value.m_valDouble = d;
1801 return *this;
1802}
1803
1805wxJSONValue& wxJSONValue::operator=(const wxChar* str) {
1806 wxJSONRefData* data = SetType(wxJSONTYPE_CSTRING);
1807 data->m_value.m_valCString = str;
1808#if !defined(WXJSON_USE_CSTRING)
1809 data->m_type = wxJSONTYPE_STRING;
1810 data->m_valString.assign(str);
1811#endif
1812 return *this;
1813}
1814
1816wxJSONValue& wxJSONValue::operator=(const wxString& str) {
1817 wxJSONRefData* data = SetType(wxJSONTYPE_STRING);
1818 data->m_valString.assign(str);
1819 return *this;
1820}
1821
1823
1828wxJSONValue& wxJSONValue::operator=(const wxMemoryBuffer& buff) {
1829 wxJSONRefData* data = SetType(wxJSONTYPE_MEMORYBUFF);
1830 data->m_memBuff = new wxMemoryBuffer();
1831 const void* ptr = buff.GetData();
1832 size_t len = buff.GetDataLen();
1833 if (data->m_memBuff && len) {
1834 data->m_memBuff->AppendData(ptr, len);
1835 }
1836 return *this;
1837}
1838
1840
1848 Ref(other);
1849 return *this;
1850}
1851
1852// finding elements
1853
1855
1880wxJSONValue wxJSONValue::Get(const wxString& key,
1881 const wxJSONValue& defaultValue) const {
1882 // NOTE: this function does many wxJSONValue copies.
1883 // so implementing COW is a good thing
1884
1885 // this is the first copy (the default value)
1886 wxJSONValue v(defaultValue);
1887
1888 wxJSONRefData* data = GetRefData();
1889 wxJSON_ASSERT(data);
1890 if (data->m_type == wxJSONTYPE_OBJECT) {
1891 wxJSONInternalMap::iterator it = data->m_valMap.find(key);
1892 if (it != data->m_valMap.end()) {
1893 v = it->second;
1894 }
1895 }
1896 return v;
1897}
1898
1899// protected functions
1900
1902
1908wxJSONValue* wxJSONValue::Find(unsigned index) const {
1909 wxJSONRefData* data = GetRefData();
1910 wxJSON_ASSERT(data);
1911
1912 wxJSONValue* vp = 0;
1913
1914 if (data->m_type == wxJSONTYPE_ARRAY) {
1915 size_t size = data->m_valArray.GetCount();
1916 if (index < size) {
1917 vp = &(data->m_valArray.Item(index));
1918 }
1919 }
1920 return vp;
1921}
1922
1924
1930wxJSONValue* wxJSONValue::Find(const wxString& key) const {
1931 wxJSONRefData* data = GetRefData();
1932 wxJSON_ASSERT(data);
1933
1934 wxJSONValue* vp = 0;
1935
1936 if (data->m_type == wxJSONTYPE_OBJECT) {
1937 wxJSONInternalMap::iterator it = data->m_valMap.find(key);
1938 if (it != data->m_valMap.end()) {
1939 vp = &(it->second);
1940 }
1941 }
1942 return vp;
1943}
1944
1946
1956wxString wxJSONValue::TypeToString(wxJSONType type) {
1957 static const wxChar* str[] = {
1958 _T( "wxJSONTYPE_INVALID" ), // 0
1959 _T( "wxJSONTYPE_NULL" ), // 1
1960 _T( "wxJSONTYPE_INT" ), // 2
1961 _T( "wxJSONTYPE_UINT" ), // 3
1962 _T( "wxJSONTYPE_DOUBLE" ), // 4
1963 _T( "wxJSONTYPE_STRING" ), // 5
1964 _T( "wxJSONTYPE_CSTRING" ), // 6
1965 _T( "wxJSONTYPE_BOOL" ), // 7
1966 _T( "wxJSONTYPE_ARRAY" ), // 8
1967 _T( "wxJSONTYPE_OBJECT" ), // 9
1968 _T( "wxJSONTYPE_LONG" ), // 10
1969 _T( "wxJSONTYPE_INT64" ), // 11
1970 _T( "wxJSONTYPE_ULONG" ), // 12
1971 _T( "wxJSONTYPE_UINT64" ), // 13
1972 _T( "wxJSONTYPE_SHORT" ), // 14
1973 _T( "wxJSONTYPE_USHORT" ), // 15
1974 _T( "wxJSONTYPE_MEMORYBUFF" ), // 16
1975 };
1976
1977 wxString s;
1978 int idx = (int)type;
1979 if (idx >= 0 && idx < 17) {
1980 s = str[idx];
1981 }
1982 return s;
1983}
1984
1986
2005wxString wxJSONValue::Dump(bool deep, int indent) const {
2006 wxJSONRefData* data = GetRefData();
2007 wxJSON_ASSERT(data);
2008
2009 wxJSONType type = GetType();
2010
2011 wxString s;
2012 if (indent > 0) {
2013 s.append(indent, ' ');
2014 }
2015
2016 wxString s1;
2017 wxString s2;
2018#if defined(WXJSON_USE_VALUE_COUNTER)
2019 s1.Printf(_T("Object: Progr=%d Type=%s Size=%d comments=%d\n"), m_progr,
2020 TypeToString(type).c_str(), Size(), data->m_comments.GetCount());
2021 s2.Printf(_T(" : RefData=%p Progr=%d Num shares=%d\n"), data,
2022 data->m_progr, data->GetRefCount());
2023#else
2024 s1.Printf(_T("Object: Type=%s Size=%d comments=%d\n"),
2025 TypeToString(type).c_str(), Size(), data->m_comments.GetCount());
2026 s2.Printf(_T(" : RefData=%p Num shares=%d\n"), data,
2027 data->GetRefCount());
2028#endif
2029 s.append(s1);
2030 if (indent > 0) {
2031 s.append(indent, ' ');
2032 }
2033 s.append(s2);
2034
2035 wxString sub;
2036
2037 // if we have to do a deep dump, we call the Dump() function for
2038 // every sub-item
2039 if (deep) {
2040 indent += 3;
2041 const wxJSONInternalMap* map;
2042 int size;
2043 ;
2044 wxJSONInternalMap::const_iterator it;
2045 switch (type) {
2046 case wxJSONTYPE_OBJECT:
2047 map = AsMap();
2048 size = Size();
2049 for (it = map->begin(); it != map->end(); ++it) {
2050 const wxJSONValue& v = it->second;
2051 sub = v.Dump(true, indent);
2052 s.append(sub);
2053 }
2054 break;
2055 case wxJSONTYPE_ARRAY:
2056 size = Size();
2057 for (int i = 0; i < size; i++) {
2058 const wxJSONValue* v = Find(i);
2059 wxJSON_ASSERT(v);
2060 sub = v->Dump(true, indent);
2061 s.append(sub);
2062 }
2063 break;
2064 default:
2065 break;
2066 }
2067 }
2068 return s;
2069}
2070
2072
2077wxString wxJSONValue::GetInfo() const {
2078 wxJSONRefData* data = GetRefData();
2079 wxJSON_ASSERT(data);
2080
2081 wxString s;
2082#if defined(WXJSON_USE_VALUE_CONTER)
2083 s.Printf(_T("Object: Progr=%d Type=%s Size=%d comments=%d\n"), data->m_progr,
2084 wxJSONValue::TypeToString(data->m_type).c_str(), Size(),
2085 data->m_comments.GetCount());
2086#else
2087 s.Printf(_T("Object: Type=%s Size=%d comments=%d\n"),
2088 wxJSONValue::TypeToString(data->m_type).c_str(), Size(),
2089 data->m_comments.GetCount());
2090#endif
2091 if (data->m_type == wxJSONTYPE_OBJECT) {
2092 wxArrayString arr = GetMemberNames();
2093 for (unsigned int i = 0; i < arr.size(); i++) {
2094 s.append(_T(" Member name: "));
2095 s.append(arr[i]);
2096 s.append(_T("\n"));
2097 }
2098 }
2099 return s;
2100}
2101
2103
2128bool wxJSONValue::IsSameAs(const wxJSONValue& other) const {
2129 // this is a recursive function: it calls itself
2130 // for every 'value' object in an array or map
2131 bool r = false;
2132
2133 // some variables used in the switch statement
2134 int size;
2135 wxJSONInternalMap::const_iterator it;
2136
2137 // get the referenced data for the two objects
2138 wxJSONRefData* data = GetRefData();
2139 wxJSONRefData* otherData = other.GetRefData();
2140
2141 if (data == otherData) {
2142 wxLogTrace(compareTraceMask,
2143 _T("(%s) objects share the same referenced data - r=TRUE"),
2144 __PRETTY_FUNCTION__);
2145 return true;
2146 }
2147
2148 // if the type does not match the function compares the values if
2149 // they are of compatible types such as INT, UINT and DOUBLE
2150 if (data->m_type != otherData->m_type) {
2151 // if the types are not compatible, returns false
2152 // otherwise compares the compatible types: INT, UINT and DOUBLE
2153 double val;
2154 switch (data->m_type) {
2155 case wxJSONTYPE_INT:
2156 if (otherData->m_type == wxJSONTYPE_UINT) {
2157 // compare the bits and returns true if value is between 0 and
2158 // LLONG_MAX
2159 if ((data->m_value.VAL_UINT <= LLONG_MAX) &&
2160 (data->m_value.VAL_UINT == otherData->m_value.VAL_UINT)) {
2161 r = true;
2162 }
2163 } else if (otherData->m_type == wxJSONTYPE_DOUBLE) {
2164 val = data->m_value.VAL_INT;
2165 if (val == otherData->m_value.m_valDouble) {
2166 r = true;
2167 }
2168 } else {
2169 r = false;
2170 }
2171 break;
2172 case wxJSONTYPE_UINT:
2173 if (otherData->m_type == wxJSONTYPE_INT) {
2174 // compare the bits and returns true if value is between 0 and
2175 // LLONG_MAX
2176 if ((data->m_value.VAL_UINT <= LLONG_MAX) &&
2177 (data->m_value.VAL_UINT == otherData->m_value.VAL_UINT)) {
2178 r = true;
2179 }
2180 } else if (otherData->m_type == wxJSONTYPE_DOUBLE) {
2181 val = data->m_value.VAL_UINT;
2182 if (val == otherData->m_value.m_valDouble) {
2183 r = true;
2184 }
2185 } else {
2186 r = false;
2187 }
2188 break;
2189 case wxJSONTYPE_DOUBLE:
2190 if (otherData->m_type == wxJSONTYPE_INT) {
2191 val = otherData->m_value.VAL_INT;
2192 if (val == data->m_value.m_valDouble) {
2193 r = true;
2194 }
2195 } else if (otherData->m_type == wxJSONTYPE_UINT) {
2196 val = otherData->m_value.VAL_UINT;
2197 if (val == data->m_value.m_valDouble) {
2198 r = true;
2199 }
2200 } else {
2201 r = false;
2202 }
2203 break;
2204 default:
2205 r = false;
2206 break;
2207 }
2208 return r;
2209 }
2210
2211 // the two objects have the same 'm_type'
2212
2213 // for comparing wxJSONTYPE_CSTRING we use two temporary wxString
2214 // objects: this is to avoid using strcmp() and wcscmp() which
2215 // may not be available on all platforms
2216 wxString s1, s2;
2217 r = true;
2218 int r1;
2219
2220 switch (data->m_type) {
2221 case wxJSONTYPE_INVALID:
2222 case wxJSONTYPE_NULL:
2223 // there is no need to compare the values
2224 break;
2225 case wxJSONTYPE_INT:
2226 if (data->m_value.VAL_INT != otherData->m_value.VAL_INT) {
2227 r = false;
2228 }
2229 break;
2230 case wxJSONTYPE_UINT:
2231 if (data->m_value.VAL_UINT != otherData->m_value.VAL_UINT) {
2232 r = false;
2233 }
2234 break;
2235 case wxJSONTYPE_DOUBLE:
2236 if (data->m_value.m_valDouble != otherData->m_value.m_valDouble) {
2237 r = false;
2238 }
2239 break;
2240 case wxJSONTYPE_CSTRING:
2241 s1 = wxString(data->m_value.m_valCString);
2242 s2 = wxString(otherData->m_value.m_valCString);
2243 if (s1 != s2) {
2244 r = false;
2245 }
2246 break;
2247 case wxJSONTYPE_BOOL:
2248 if (data->m_value.m_valBool != otherData->m_value.m_valBool) {
2249 r = false;
2250 }
2251 break;
2252 case wxJSONTYPE_STRING:
2253 if (data->m_valString != otherData->m_valString) {
2254 r = false;
2255 }
2256 break;
2257 case wxJSONTYPE_MEMORYBUFF:
2258 // we cannot simply use the operator ==; we need a deep comparison
2259 r1 = CompareMemoryBuff(*(data->m_memBuff), *(otherData->m_memBuff));
2260 if (r1 != 0) {
2261 r = false;
2262 }
2263 break;
2264 case wxJSONTYPE_ARRAY:
2265 size = Size();
2266 wxLogTrace(compareTraceMask,
2267 _T("(%s) Comparing an array object - size=%d"),
2268 __PRETTY_FUNCTION__, size);
2269
2270 if (size != other.Size()) {
2271 wxLogTrace(compareTraceMask, _T("(%s) Sizes does not match"),
2272 __PRETTY_FUNCTION__);
2273 return false;
2274 }
2275 // compares every element in this object with the element of
2276 // the same index in the 'other' object
2277 for (int i = 0; i < size; i++) {
2278 wxLogTrace(compareTraceMask, _T("(%s) Comparing array element=%d"),
2279 __PRETTY_FUNCTION__, i);
2280 wxJSONValue v1 = ItemAt(i);
2281 wxJSONValue v2 = other.ItemAt(i);
2282
2283 if (!v1.IsSameAs(v2)) {
2284 return false;
2285 }
2286 }
2287 break;
2288 case wxJSONTYPE_OBJECT:
2289 size = Size();
2290 wxLogTrace(compareTraceMask, _T("(%s) Comparing a map obejct - size=%d"),
2291 __PRETTY_FUNCTION__, size);
2292
2293 if (size != other.Size()) {
2294 wxLogTrace(compareTraceMask,
2295 _T("(%s) Comparison failed - sizes does not match"),
2296 __PRETTY_FUNCTION__);
2297 return false;
2298 }
2299 // for every key calls itself on the value found in
2300 // the other object. if 'key' does no exist, returns FALSE
2301 for (it = data->m_valMap.begin(); it != data->m_valMap.end(); it++) {
2302 wxString key = it->first;
2303 wxLogTrace(compareTraceMask, _T("(%s) Comparing map object - key=%s"),
2304 __PRETTY_FUNCTION__, key.c_str());
2305 wxJSONValue otherVal = other.ItemAt(key);
2306 bool isSame = it->second.IsSameAs(otherVal);
2307 if (!isSame) {
2308 wxLogTrace(compareTraceMask,
2309 _T("(%s) Comparison failed for the last object"),
2310 __PRETTY_FUNCTION__);
2311 return false;
2312 }
2313 }
2314 break;
2315 default:
2316 // should never happen
2317 wxFAIL_MSG(_T("wxJSONValue::IsSameAs() unexpected wxJSONType"));
2318 break;
2319 }
2320 return r;
2321}
2322
2324
2348int wxJSONValue::AddComment(const wxString& str, int position) {
2349 wxJSONRefData* data = COW();
2350 wxJSON_ASSERT(data);
2351
2352 wxLogTrace(traceMask, _T("(%s) comment=%s"), __PRETTY_FUNCTION__,
2353 str.c_str());
2354 int r = -1;
2355 int len = str.length();
2356 if (len < 2) {
2357 wxLogTrace(traceMask, _T(" error: len < 2"));
2358 return -1;
2359 }
2360 if (str[0] != '/') {
2361 wxLogTrace(traceMask, _T(" error: does not start with\'/\'"));
2362 return -1;
2363 }
2364 if (str[1] == '/') { // a C++ comment: check that it ends with '\n'
2365 wxLogTrace(traceMask, _T(" C++ comment" ));
2366 if (str.GetChar(len - 1) != '\n') {
2367 wxString temp(str);
2368 temp.append(1, '\n');
2369 data->m_comments.Add(temp);
2370 wxLogTrace(traceMask, _T(" C++ comment: LF added"));
2371 } else {
2372 data->m_comments.Add(str);
2373 }
2374 r = data->m_comments.size();
2375 } else if (str[1] ==
2376 '*') { // a C-style comment: check that it ends with '*/'
2377 wxLogTrace(traceMask, _T(" C-style comment"));
2378 int lastPos = len - 1;
2379 wxChar ch = str.GetChar(lastPos);
2380 // skip leading whitespaces
2381 while (ch == ' ' || ch == '\n' || ch == '\t') {
2382 --lastPos;
2383 ch = str.GetChar(lastPos);
2384 }
2385 if (str.GetChar(lastPos) == '/' && str.GetChar(lastPos - 1) == '*') {
2386 data->m_comments.Add(str);
2387 r = data->m_comments.size();
2388 }
2389 } else {
2390 wxLogTrace(traceMask, _T(" error: is not a valid comment string"));
2391 r = -1;
2392 }
2393 // if the comment was stored, store the position
2394 if (r >= 0 && position != wxJSONVALUE_COMMENT_DEFAULT) {
2395 data->m_commentPos = position;
2396 }
2397 return r;
2398}
2399
2401
2407int wxJSONValue::AddComment(const wxArrayString& comments, int position) {
2408 int siz = comments.GetCount();
2409 int r = 0;
2410 for (int i = 0; i < siz; i++) {
2411 int r2 = AddComment(comments[i], position);
2412 if (r2 >= 0) {
2413 ++r;
2414 }
2415 }
2416 return r;
2417}
2418
2420
2426wxString wxJSONValue::GetComment(int idx) const {
2427 wxJSONRefData* data = GetRefData();
2428 wxJSON_ASSERT(data);
2429
2430 wxString s;
2431 int size = data->m_comments.GetCount();
2432 if (idx < 0) {
2433 for (int i = 0; i < size; i++) {
2434 s.append(data->m_comments[i]);
2435 }
2436 } else if (idx < size) {
2437 s = data->m_comments[idx];
2438 }
2439 return s;
2440}
2441
2443int wxJSONValue::GetCommentCount() const {
2444 wxJSONRefData* data = GetRefData();
2445 wxJSON_ASSERT(data);
2446
2447 int d = data->m_comments.GetCount();
2448 wxLogTrace(traceMask, _T("(%s) comment count=%d"), __PRETTY_FUNCTION__, d);
2449 return d;
2450}
2451
2453int wxJSONValue::GetCommentPos() const {
2454 wxJSONRefData* data = GetRefData();
2455 wxJSON_ASSERT(data);
2456 return data->m_commentPos;
2457}
2458
2460const wxArrayString& wxJSONValue::GetCommentArray() const {
2461 wxJSONRefData* data = GetRefData();
2462 wxJSON_ASSERT(data);
2463
2464 return data->m_comments;
2465}
2466
2469 wxJSONRefData* data = COW();
2470 wxJSON_ASSERT(data);
2471
2472 data->m_comments.clear();
2473}
2474
2476
2537wxJSONRefData* wxJSONValue::SetType(wxJSONType type) {
2538 wxJSONRefData* data = GetRefData();
2539 wxJSONType oldType = GetType();
2540
2541 // check that type is within the allowed range
2542 wxJSON_ASSERT((type >= wxJSONTYPE_INVALID) &&
2543 (type <= wxJSONTYPE_MEMORYBUFF));
2544 if ((type < wxJSONTYPE_INVALID) || (type > wxJSONTYPE_MEMORYBUFF)) {
2545 type = wxJSONTYPE_INVALID;
2546 }
2547
2548 // the function unshares the referenced data but does not delete the
2549 // structure. This is because the wxJSON reader stores comments
2550 // that apear before the value in a temporary value of type wxJSONTYPE_INVALID
2551 // which is invalid and, next, it stores the JSON value in the same
2552 // wxJSONValue object.
2553 // If we would delete the structure using 'Unref()' we loose the
2554 // comments
2555 data = COW();
2556
2557 // do nothing if the actual type is the same as 'type'
2558 if (type == oldType) {
2559 return data;
2560 }
2561
2562 // change the type of the referened structure
2563 // NOTE: integer types are always stored as the generic integer types
2564 if (type == wxJSONTYPE_LONG || type == wxJSONTYPE_INT64 ||
2565 type == wxJSONTYPE_SHORT) {
2566 type = wxJSONTYPE_INT;
2567 }
2568 if (type == wxJSONTYPE_ULONG || type == wxJSONTYPE_UINT64 ||
2569 type == wxJSONTYPE_USHORT) {
2570 type = wxJSONTYPE_UINT;
2571 }
2572
2573 wxJSON_ASSERT(data);
2574 data->m_type = type;
2575
2576 // clears complex objects of the old type
2577 switch (oldType) {
2578 case wxJSONTYPE_STRING:
2579 data->m_valString.clear();
2580 break;
2581 case wxJSONTYPE_ARRAY:
2582 data->m_valArray.Clear();
2583 break;
2584 case wxJSONTYPE_OBJECT:
2585 data->m_valMap.clear();
2586 break;
2587 case wxJSONTYPE_MEMORYBUFF:
2588 // we first have to delete the actual memory buffer, if any
2589 if (data->m_memBuff) {
2590 delete data->m_memBuff;
2591 data->m_memBuff = 0;
2592 }
2593 break;
2594 default:
2595 // there is not need to clear primitive types
2596 break;
2597 }
2598
2599 // if the WXJSON_USE_CSTRING macro is not defined, the class forces
2600 // C-string to be stored as wxString objects
2601#if !defined(WXJSON_USE_CSTRING)
2602 if (data->m_type == wxJSONTYPE_CSTRING) {
2603 data->m_type = wxJSONTYPE_STRING;
2604 }
2605#endif
2606 return data;
2607}
2608
2610
2619int wxJSONValue::GetLineNo() const {
2620 // return ZERO if there is not a referenced data structure
2621 int n = 0;
2622 wxJSONRefData* data = GetRefData();
2623 if (data != 0) {
2624 n = data->m_lineNo;
2625 }
2626 return n;
2627}
2628
2630void wxJSONValue::SetLineNo(int num) {
2631 wxJSONRefData* data = COW();
2632 wxJSON_ASSERT(data);
2633 data->m_lineNo = num;
2634}
2635
2637void wxJSONValue::SetRefData(wxJSONRefData* data) { m_refData = data; }
2638
2640void wxJSONValue::Ref(const wxJSONValue& clone) {
2641 // nothing to be done
2642 if (m_refData == clone.m_refData) return;
2643
2644 // delete reference to old data
2645 UnRef();
2646
2647 // reference new data
2648 if (clone.m_refData) {
2649 m_refData = clone.m_refData;
2650 ++(m_refData->m_refCount);
2651 }
2652}
2653
2655
2660void wxJSONValue::UnRef() {
2661 if (m_refData) {
2662 wxASSERT_MSG(m_refData->m_refCount > 0, _T("invalid ref data count"));
2663
2664 if (--m_refData->m_refCount == 0) {
2665 delete m_refData;
2666 m_refData = NULL;
2667 }
2668 }
2669}
2670
2673
2675
2679void wxJSONValue::DeepCopy(const wxJSONValue& other) {
2680 UnRef();
2681 wxJSONRefData* data = CloneRefData(other.m_refData);
2682 SetRefData(data);
2683}
2684
2687 wxJSONRefData* data = m_refData;
2688 return data;
2689}
2690
2692
2700wxJSONRefData* wxJSONValue::CloneRefData(const wxJSONRefData* otherData) const {
2701 wxJSON_ASSERT(otherData);
2702
2703 // make a static cast to pointer-to-wxJSONRefData
2704 const wxJSONRefData* other = otherData;
2705
2706 // allocate a new instance of wxJSONRefData using the default
2707 // ctor; we cannot use the copy ctor of a wxJSONRefData
2708 wxJSONRefData* data = new wxJSONRefData();
2709
2710 // copy the referenced data structure's data members
2711 data->m_type = other->m_type;
2712 data->m_value = other->m_value;
2713 data->m_commentPos = other->m_commentPos;
2714 data->m_comments = other->m_comments;
2715 data->m_lineNo = other->m_lineNo;
2716 data->m_valString = other->m_valString;
2717 data->m_valArray = other->m_valArray;
2718 data->m_valMap = other->m_valMap;
2719
2720 // if the data contains a wxMemoryBuffer object, then we have
2721 // to make a deep copy of the buffer by allocating a new one because
2722 // wxMemoryBuffer is not a copy-on-write structure
2723 if (other->m_memBuff) {
2724 data->m_memBuff = new wxMemoryBuffer();
2725 const void* ptr = data->m_memBuff->GetData();
2726 size_t len = data->m_memBuff->GetDataLen();
2727 if (data->m_memBuff && len) {
2728 data->m_memBuff->AppendData(ptr, len);
2729 }
2730 }
2731
2732 wxLogTrace(cowTraceMask, _T("(%s) CloneRefData() PROGR: other=%d data=%d"),
2733 __PRETTY_FUNCTION__, other->GetRefCount(), data->GetRefCount());
2734
2735 return data;
2736}
2737
2739
2746 wxJSONRefData* data = new wxJSONRefData();
2747 data->m_type = wxJSONTYPE_INVALID;
2748 return data;
2749}
2750
2752
2759 wxJSONRefData* data = GetRefData();
2760 wxLogTrace(cowTraceMask, _T("(%s) COW() START data=%p data->m_count=%d"),
2761 __PRETTY_FUNCTION__, data, data->GetRefCount());
2762 UnShare();
2763 data = GetRefData();
2764 wxLogTrace(cowTraceMask, _T("(%s) COW() END data=%p data->m_count=%d"),
2765 __PRETTY_FUNCTION__, data, data->GetRefCount());
2766 return GetRefData();
2767}
2768
2771 if (!m_refData) {
2773 } else if (m_refData->GetRefCount() > 1) {
2774 // note that ref is not going to be destroyed in this case
2775 const wxJSONRefData* ref = m_refData;
2776 UnRef();
2777
2778 // ... so we can still access it
2779 m_refData = CloneRefData(ref);
2780 }
2781 // else: ref count is 1, we are exclusive owners of m_refData anyhow
2782
2783 wxASSERT_MSG(m_refData && m_refData->GetRefCount() == 1,
2784 _T("wxObject::AllocExclusive() failed."));
2785}
2786
2788/*/
2789 The fucntion returns a string representation of the data contained in the
2790 memory buffer object \c buff.
2791 The string is conposed of two hexadecimal digits for every byte contained
2792 in the memory buffer; bytes are separated by a space character.
2793 The string starts with the actual lenght of the data enclosed in parenthesis.
2794 The string will contain \c len bytes if \c len is less than the length
2795 of the actual data in \c buff.
2796 Note that the (len) printed in the output referes to the length of the buffer
2797 which may be greater than the length that has to be printed.
2798
2799 \b Example:
2800 This is an example of printing a memory buffer object that contains 10 bytes:
2801 \code
2802 0x80974653 (10) 00 01 02 03 04 05 06 07 08 09
2803 \endcode
2804*/
2805wxString wxJSONValue::MemoryBuffToString(const wxMemoryBuffer& buff,
2806 size_t len) {
2807 size_t buffLen = buff.GetDataLen();
2808 void* ptr = buff.GetData();
2809 wxString s = MemoryBuffToString(ptr, MIN(buffLen, len), buffLen);
2810 return s;
2811}
2812
2814/*/
2815 The function returns a string representation of the data contained in the
2816 binary memory buffer pointed to by \c buff for \c len bytes.
2817 The string is composed of two hexadecimal digits for every byte contained
2818 in the memory buffer; bytes are separated by a space character.
2819 The string starts with pointer to binary data followed by the lenght of the
2820 data enclosed in parenthesis.
2821
2822 \b Example:
2823 This is an example of printing ten bytes from a memory buffer:
2824 \code
2825 0x80974653 (10) 00 01 02 03 04 05 06 07 08 09
2826 \endcode
2827
2828 @param buff the pointer to the memory buffer data
2829 @len the length of the data that has to be printed
2830 @actualLen the real lenght of the memory buffer that has to be printed
2831 just afetr the pointer; may be greater than \c len. If this parameter
2832 is -1 then it is equal to \c len
2833*/
2834wxString wxJSONValue::MemoryBuffToString(const void* buff, size_t len,
2835 size_t actualLen) {
2836 wxString s;
2837 size_t buffLen = actualLen;
2838 if (buffLen == (size_t)-1) {
2839 buffLen = len;
2840 }
2841 s.Printf(_T("%p (%u) "), buff, buffLen);
2842 unsigned char* ptr = (unsigned char*)buff;
2843 for (unsigned int i = 0; i < len; i++) {
2844 unsigned char c = *ptr;
2845 ++ptr;
2846 // now convert the character
2847 char c1 = c / 16;
2848 char c2 = c % 16;
2849 c1 += '0';
2850 c2 += '0';
2851 if (c1 > '9') {
2852 c1 += 7;
2853 }
2854 if (c2 > '9') {
2855 c2 += 7;
2856 }
2857 s.Append(c1, 1);
2858 s.Append(c2, 1);
2859 s.Append(' ', 1); // a space separates the bytes
2860 }
2861 return s;
2862}
2863
2865
2889int wxJSONValue::CompareMemoryBuff(const wxMemoryBuffer& buff1,
2890 const wxMemoryBuffer& buff2) {
2891 int r;
2892 size_t buff1Len = buff1.GetDataLen();
2893 size_t buff2Len = buff2.GetDataLen();
2894 if (buff1Len > buff2Len) {
2895 r = 1;
2896 } else if (buff1Len < buff2Len) {
2897 r = -1;
2898 } else {
2899 r = memcmp(buff1.GetData(), buff2.GetData(), buff1Len);
2900 }
2901 return r;
2902}
2903
2905
2916int wxJSONValue::CompareMemoryBuff(const wxMemoryBuffer& buff1,
2917 const void* buff2) {
2918 int r;
2919 size_t buff1Len = buff1.GetDataLen();
2920 r = memcmp(buff1.GetData(), buff2, buff1Len);
2921 return r;
2922}
2923
2925
2945wxMemoryBuffer wxJSONValue::ArrayToMemoryBuff(const wxJSONValue& value) {
2946 wxMemoryBuffer buff;
2947 if (value.IsArray()) {
2948 int len = value.Size();
2949 for (int i = 0; i < len; i++) {
2950 short int byte;
2951 unsigned char c;
2952 // we do not use opertaor [] because it is not const
2953 // bool r = value[i].AsShort( byte );
2954 bool r = value.ItemAt(i).AsShort(byte);
2955 if (r && (byte >= 0 && byte <= 255)) {
2956 c = (unsigned char)byte;
2957 buff.AppendByte(c);
2958 }
2959 }
2960 }
2961 return buff;
2962}
2963
2964/*************************************************************************
2965
2966 64-bits integer support
2967
2968*************************************************************************/
2969
2970#if defined(wxJSON_64BIT_INT)
2971
2973wxJSONValue::wxJSONValue(wxInt64 i) {
2974 m_refData = 0;
2975 wxJSONRefData* data = Init(wxJSONTYPE_INT);
2976 wxJSON_ASSERT(data);
2977 if (data != 0) {
2978 data->m_value.VAL_INT = i;
2979 }
2980}
2981
2983wxJSONValue::wxJSONValue(wxUint64 ui) {
2984 m_refData = 0;
2985 wxJSONRefData* data = Init(wxJSONTYPE_UINT);
2986 wxJSON_ASSERT(data);
2987 if (data != 0) {
2988 data->m_value.VAL_UINT = ui;
2989 }
2990}
2991
2993
3001bool wxJSONValue::IsInt32() const {
3002 bool r = IsLong();
3003 return r;
3004}
3005
3007
3015bool wxJSONValue::IsUInt32() const {
3016 bool r = IsULong();
3017 return r;
3018}
3019
3021
3030bool wxJSONValue::IsInt64() const {
3031 wxJSONRefData* data = GetRefData();
3032 wxJSON_ASSERT(data);
3033 bool r = false;
3034 if (data->m_type == wxJSONTYPE_INT) {
3035 r = true;
3036 }
3037 return r;
3038}
3039
3041
3050bool wxJSONValue::IsUInt64() const {
3051 wxJSONRefData* data = GetRefData();
3052 wxJSON_ASSERT(data);
3053 bool r = false;
3054 if (data->m_type == wxJSONTYPE_UINT) {
3055 r = true;
3056 }
3057 return r;
3058}
3059
3061
3072wxInt32 wxJSONValue::AsInt32() const {
3073 wxInt32 i;
3074 i = (wxInt32)AsLong();
3075 return i;
3076}
3077
3079
3090wxUint32 wxJSONValue::AsUInt32() const {
3091 wxUint32 ui;
3092 ui = (wxUint32)AsULong();
3093 return ui;
3094}
3095
3097
3110wxInt64 wxJSONValue::AsInt64() const {
3111 wxJSONRefData* data = GetRefData();
3112 wxJSON_ASSERT(data);
3113 wxInt64 i64 = data->m_value.m_valInt64;
3114
3115 wxJSON_ASSERT(IsInt64()); // exapnds only in debug builds
3116 return i64;
3117}
3118
3120
3133wxUint64 wxJSONValue::AsUInt64() const {
3134 wxJSONRefData* data = GetRefData();
3135 wxJSON_ASSERT(data);
3136 wxUint64 ui64 = data->m_value.m_valUInt64;
3137
3138 wxJSON_ASSERT(IsUInt64()); // exapnds only in debug builds
3139 return ui64;
3140}
3141
3142bool wxJSONValue::AsInt32(wxInt32& i32) const {
3143 bool r = IsInt32();
3144 if (r) {
3145 i32 = AsInt32();
3146 }
3147 return r;
3148}
3149
3150bool wxJSONValue::AsUInt32(wxUint32& ui32) const {
3151 bool r = IsUInt32();
3152 if (r) {
3153 ui32 = AsUInt32();
3154 }
3155 return r;
3156}
3157
3158bool wxJSONValue::AsInt64(wxInt64& i64) const {
3159 bool r = IsInt64();
3160 if (r) {
3161 i64 = AsInt64();
3162 }
3163 return r;
3164}
3165
3166bool wxJSONValue::AsUInt64(wxUint64& ui64) const {
3167 bool r = IsUInt64();
3168 if (r) {
3169 ui64 = AsUInt64();
3170 }
3171 return r;
3172}
3173
3175wxJSONValue& wxJSONValue::Append(wxInt64 i) {
3176 wxJSONValue v(i);
3177 wxJSONValue& r = Append(v);
3178 return r;
3179}
3180
3182wxJSONValue& wxJSONValue::Append(wxUint64 ui) {
3183 wxJSONValue v(ui);
3184 wxJSONValue& r = Append(v);
3185 return r;
3186}
3187
3190 wxJSONRefData* data = SetType(wxJSONTYPE_INT);
3191 data->m_value.VAL_INT = i;
3192 return *this;
3193}
3194
3196wxJSONValue& wxJSONValue::operator=(wxUint64 ui) {
3197 wxJSONRefData* data = SetType(wxJSONTYPE_UINT);
3198 data->m_value.VAL_UINT = ui;
3199 return *this;
3200}
3201
3202#endif // defined( wxJSON_64BIT_INT )
3203
3204/*
3205{
3206}
3207*/
The reference counted JSON value data (internal use).
Definition jsonval.h:350
wxMemoryBuffer * m_memBuff
The pointer to the memory buffer object.
Definition jsonval.h:415
wxJSONInternalMap m_valMap
The JSON object value.
Definition jsonval.h:385
int m_commentPos
The position of the comment line(s), if any.
Definition jsonval.h:394
int m_refCount
the references count
Definition jsonval.h:364
wxJSONInternalArray m_valArray
The JSON array value.
Definition jsonval.h:382
wxJSONType m_type
The actual type of the value held by this object.
Definition jsonval.h:367
int m_lineNo
The line number when this value was read.
Definition jsonval.h:407
wxJSONRefData()
Constructor.
Definition jsonval.cpp:72
wxString m_valString
The JSON string value.
Definition jsonval.h:379
wxJSONValueHolder m_value
The JSON value held by this object.
Definition jsonval.h:376
wxArrayString m_comments
The array of comment lines; may be empty.
Definition jsonval.h:397
The JSON value class implementation.
Definition jsonval.h:84
bool IsSameAs(const wxJSONValue &other) const
The comparison function.
Definition jsonval.cpp:2126
void Ref(const wxJSONValue &clone)
Increments the referenced data counter.
Definition jsonval.cpp:2638
wxArrayString GetMemberNames() const
Return the array of keys of this JSON object.
Definition jsonval.cpp:1357
bool IsArray() const
Return TRUE if the type of the value stored is an array type.
Definition jsonval.cpp:746
bool IsCString() const
Return TRUE if the type of the value stored is a pointer to a static C string.
Definition jsonval.cpp:736
void UnShare()
Makes an exclusive copy of shared data.
Definition jsonval.cpp:2670
bool Remove(int index)
Remove the item at the specified index or key.
Definition jsonval.cpp:1551
int AddComment(const wxString &str, int position=wxJSONVALUE_COMMENT_DEFAULT)
Add a comment to this JSON value object.
Definition jsonval.cpp:2346
virtual wxJSONRefData * CloneRefData(const wxJSONRefData *data) const
Make a copy of the referenced data.
Definition jsonval.cpp:2698
bool IsUInt() const
Return TRUE if the type of the value stored is a unsigned int.
Definition jsonval.cpp:613
wxString Dump(bool deep=false, int mode=0) const
Returns informations about the object.
Definition jsonval.cpp:2003
wxJSONRefData * Init(wxJSONType type)
Initialize the JSON value class.
Definition jsonval.cpp:196
long int AsLong() const
Returns the value as a long integer.
Definition jsonval.cpp:997
bool IsLong() const
Return TRUE if the stored value is an integer which fits in a long int.
Definition jsonval.cpp:663
int Size() const
Return the size of the array or map stored in this value.
Definition jsonval.cpp:1332
int GetCommentCount() const
Return the number of comment strings.
Definition jsonval.cpp:2441
bool HasMember(unsigned index) const
Return TRUE if the object contains an element at the specified index.
Definition jsonval.cpp:1298
bool IsDouble() const
Return TRUE if the type of the value stored is a double.
Definition jsonval.cpp:707
bool IsInt() const
Return TRUE if the type of the value stored is integer.
Definition jsonval.cpp:552
static wxString MemoryBuffToString(const wxMemoryBuffer &buff, size_t len=-1)
Convert memory buffer object to a string representation.
Definition jsonval.cpp:2803
bool IsShort() const
Return TRUE if the type of the value stored is 16-bit integer.
Definition jsonval.cpp:581
wxJSONValue Get(const wxString &key, const wxJSONValue &defaultValue) const
Return a value or a default value.
Definition jsonval.cpp:1878
double AsDouble() const
Return the stored value as a double.
Definition jsonval.cpp:827
virtual wxJSONRefData * CreateRefData() const
Create a new data structure.
Definition jsonval.cpp:2743
wxJSONType GetType() const
Return the type of the value stored in the object.
Definition jsonval.cpp:458
void UnRef()
Unreferences the shared data.
Definition jsonval.cpp:2658
wxJSONValue * Find(unsigned index) const
Find an element.
Definition jsonval.cpp:1906
void DeepCopy(const wxJSONValue &other)
Do a deep copy of the other object.
Definition jsonval.cpp:2677
static wxMemoryBuffer ArrayToMemoryBuff(const wxJSONValue &value)
Converts an array of INTs to a memory buffer.
Definition jsonval.cpp:2943
wxJSONValue ItemAt(unsigned index) const
Return the item at the specified index.
Definition jsonval.cpp:1657
wxJSONRefData * COW()
Make sure the referenced data is unique.
Definition jsonval.cpp:2756
wxString GetComment(int idx=-1) const
Return a comment string.
Definition jsonval.cpp:2424
void SetRefData(wxJSONRefData *data)
Set the pointer to the referenced data.
Definition jsonval.cpp:2635
bool IsString() const
Return TRUE if the type of the value stored is a wxString object.
Definition jsonval.cpp:717
void ClearComments()
Clear all comment strings.
Definition jsonval.cpp:2466
bool IsUShort() const
Return TRUE if the type of the value stored is a unsigned short.
Definition jsonval.cpp:640
const wxJSONInternalMap * AsMap() const
Return the stored value as a map object.
Definition jsonval.cpp:1263
const wxChar * AsCString() const
Return the stored value as a pointer to a static C string.
Definition jsonval.cpp:947
wxJSONValue & operator[](unsigned index)
Return the item at the specified index.
Definition jsonval.cpp:1707
static int CompareMemoryBuff(const wxMemoryBuffer &buff1, const wxMemoryBuffer &buff2)
Compares two memory buffer objects.
Definition jsonval.cpp:2887
wxJSONValue & Append(const wxJSONValue &value)
Append the specified value in the array.
Definition jsonval.cpp:1385
bool IsBool() const
Return TRUE if the type of the value stored is a boolean.
Definition jsonval.cpp:697
unsigned int AsUInt() const
Return the stored value as a unsigned int.
Definition jsonval.cpp:976
unsigned short AsUShort() const
Returns the value as a unsigned short integer.
Definition jsonval.cpp:1062
void AllocExclusive()
Makes a private copy of the referenced data.
Definition jsonval.cpp:2768
wxJSONValue()
Constructors.
Definition jsonval.cpp:183
bool IsULong() const
Return TRUE if the stored value is an integer which fits in a unsigned long int.
Definition jsonval.cpp:687
wxString AsString() const
Return the stored value as a wxWidget's string.
Definition jsonval.cpp:872
void Clear()
Clear the object value.
Definition jsonval.cpp:1584
int GetLineNo() const
Return the line number of this JSON value object.
Definition jsonval.cpp:2617
bool IsValid() const
Return TRUE if the value stored is valid.
Definition jsonval.cpp:521
bool IsMemoryBuff() const
Return TRUE if the type of this value is a binary memory buffer.
Definition jsonval.cpp:766
short AsShort() const
Returns the value as a short integer.
Definition jsonval.cpp:1040
wxString GetInfo() const
Returns informations about the object.
Definition jsonval.cpp:2075
bool IsNull() const
Return TRUE if the type of the value is wxJSONTYPE_NULL.
Definition jsonval.cpp:500
void SetLineNo(int num)
Set the line number of this JSON value object.
Definition jsonval.cpp:2628
bool IsObject() const
Return TRUE if the type of this value is a key/value map.
Definition jsonval.cpp:756
unsigned long AsULong() const
Returns the value as a unsigned long integer.
Definition jsonval.cpp:1019
bool AsBool() const
Return the stored value as a boolean.
Definition jsonval.cpp:809
wxJSONRefData * SetType(wxJSONType type)
Set the type of the stored value.
Definition jsonval.cpp:2535
int GetCommentPos() const
Return the comment position.
Definition jsonval.cpp:2451
wxJSONRefData * m_refData
the referenced data
Definition jsonval.h:287
virtual ~wxJSONValue()
Dtor - calls UnRef().
Definition jsonval.cpp:403
wxJSONValue & operator=(int i)
Assign the specified value to this object replacing the old value.
Definition jsonval.cpp:1747
wxJSONValue & Item(unsigned index)
Return the item at the specified index.
Definition jsonval.cpp:1602
int AsInt() const
Return the stored value as an integer.
Definition jsonval.cpp:789
const wxJSONInternalArray * AsArray() const
Return the stored value as an array object.
Definition jsonval.cpp:1281
wxMemoryBuffer AsMemoryBuff() const
Returns the value as a memory buffer.
Definition jsonval.cpp:1215
const wxArrayString & GetCommentArray() const
Get the comment string's array.
Definition jsonval.cpp:2458
wxJSONRefData * GetRefData() const
Return the pointer to the referenced data structure.
Definition jsonval.cpp:2684
static wxString TypeToString(wxJSONType type)
Return a string description of the type.
Definition jsonval.cpp:1954