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