OpenCPN Partial API docs
Loading...
Searching...
No Matches
jsonval.h
1
2// Name: jsonval.h
3// Purpose: the wxJSONValue class: it holds a JSON value
4// Author: Luciano Cattani
5// Created: 2007/09/15
6// Copyright: (c) 2007 Luciano Cattani
7// Licence: wxWidgets licence
9
10#ifndef WX_JSONVAL_H
11#define WX_JSONVAL_H
12
13// For compilers that support precompilation, includes "wx/wx.h".
14#include <wx/wxprec.h>
15
16// for all others, include the necessary headers (this file is usually all you
17// need because it includes almost all "standard" wxWidgets headers)
18#ifndef WX_PRECOMP
19#include <wx/object.h>
20#include <wx/hashmap.h>
21#include <wx/dynarray.h>
22#include <wx/arrstr.h>
23#endif
24
25#include "json_defs.h"
26
27// forward declarations
28class WXDLLIMPEXP_JSON wxJSONReader;
29class WXDLLIMPEXP_JSON wxJSONRefData;
30
31#if defined(wxJSON_USE_STL)
32// if compiling on MinGW we use the STL-style declaration of wxWidget's
33// container classes
34class WXDLLIMPEXP_JSON wxJSONValue;
35WX_DECLARE_OBJARRAY(wxJSONValue, wxJSONInternalArray);
36WX_DECLARE_STRING_HASH_MAP(wxJSONValue, wxJSONInternalMap);
37#else
38class WXDLLIMPEXP_JSON wxJSONInternalMap;
39class WXDLLIMPEXP_JSON wxJSONInternalArray;
40#endif
41
43enum wxJSONType {
44 wxJSONTYPE_INVALID = 0,
45 wxJSONTYPE_NULL,
46 wxJSONTYPE_INT,
47 wxJSONTYPE_UINT,
48 wxJSONTYPE_DOUBLE,
49 wxJSONTYPE_STRING,
50 wxJSONTYPE_CSTRING,
51 wxJSONTYPE_BOOL,
52 wxJSONTYPE_ARRAY,
53 wxJSONTYPE_OBJECT,
54 wxJSONTYPE_LONG,
55 wxJSONTYPE_INT64,
56 wxJSONTYPE_ULONG,
57 wxJSONTYPE_UINT64,
58 wxJSONTYPE_SHORT,
59 wxJSONTYPE_USHORT,
60 wxJSONTYPE_MEMORYBUFF
61};
62
63// the comment position: every value only has one comment position
64// althrough comments may be splitted into several lines
65enum {
66 wxJSONVALUE_COMMENT_DEFAULT = 0,
67 wxJSONVALUE_COMMENT_BEFORE,
68 wxJSONVALUE_COMMENT_AFTER,
69 wxJSONVALUE_COMMENT_INLINE,
70};
71
72/***********************************************************************
73
74 class wxJSONValue
75
76***********************************************************************/
77
78// class WXDLLIMPEXP_JSON wxJSONValue : public wxObject
79class WXDLLIMPEXP_JSON wxJSONValue {
80 friend class wxJSONReader;
81
82public:
83 // ctors and dtor
85 wxJSONValue(wxJSONType type);
86 wxJSONValue(int i);
87 wxJSONValue(unsigned int i);
88 wxJSONValue(short i);
89 wxJSONValue(unsigned short i);
90 wxJSONValue(long int i);
91 wxJSONValue(unsigned long int i);
92#if defined(wxJSON_64BIT_INT)
93 wxJSONValue(wxInt64 i);
94 wxJSONValue(wxUint64 ui);
95#endif
96 wxJSONValue(bool b);
97 wxJSONValue(double d);
98 wxJSONValue(const wxChar* str); // assume static ASCIIZ strings
99 wxJSONValue(const wxString& str);
100 wxJSONValue(const wxMemoryBuffer& buff);
101 wxJSONValue(const void* buff, size_t len);
102 wxJSONValue(const wxJSONValue& other);
103 virtual ~wxJSONValue();
104
105 // functions for retrieving the value type
106 wxJSONType GetType() const;
107 bool IsValid() const;
108 bool IsNull() const;
109 bool IsInt() const;
110 bool IsUInt() const;
111 bool IsShort() const;
112 bool IsUShort() const;
113 bool IsLong() const;
114 bool IsULong() const;
115#if defined(wxJSON_64BIT_INT)
116 bool IsInt32() const;
117 bool IsInt64() const;
118 bool IsUInt32() const;
119 bool IsUInt64() const;
120#endif
121 bool IsBool() const;
122 bool IsDouble() const;
123 bool IsString() const;
124 bool IsCString() const;
125 bool IsArray() const;
126 bool IsObject() const;
127 bool IsMemoryBuff() const;
128
129 // function for retireving the value as ...
130 int AsInt() const;
131 unsigned int AsUInt() const;
132 short AsShort() const;
133 unsigned short AsUShort() const;
134 long int AsLong() const;
135 unsigned long AsULong() const;
136 bool AsInt(int& i) const;
137 bool AsUInt(unsigned int& ui) const;
138 bool AsShort(short int& s) const;
139 bool AsUShort(unsigned short& us) const;
140 bool AsLong(long int& l) const;
141 bool AsULong(unsigned long& ul) const;
142#if defined(wxJSON_64BIT_INT)
143 wxInt32 AsInt32() const;
144 wxUint32 AsUInt32() const;
145 wxInt64 AsInt64() const;
146 wxUint64 AsUInt64() const;
147 bool AsInt32(wxInt32& i32) const;
148 bool AsUInt32(wxUint32& ui32) const;
149 bool AsInt64(wxInt64& i64) const;
150 bool AsUInt64(wxUint64& ui64) const;
151#endif
152 bool AsBool() const;
153 double AsDouble() const;
154 wxString AsString() const;
155 const wxChar* AsCString() const;
156 bool AsBool(bool& b) const;
157 bool AsDouble(double& d) const;
158 bool AsString(wxString& str) const;
159 bool AsCString(wxChar* ch) const;
160 wxMemoryBuffer AsMemoryBuff() const;
161 bool AsMemoryBuff(wxMemoryBuffer& buff) const;
162
163 const wxJSONInternalMap* AsMap() const;
164 const wxJSONInternalArray* AsArray() const;
165
166 // get members names, size and other info
167 bool HasMember(unsigned index) const;
168 bool HasMember(const wxString& key) const;
169 int Size() const;
170 wxArrayString GetMemberNames() const;
171
172 // appending items, resizing and deleting items
173 wxJSONValue& Append(const wxJSONValue& value);
174 wxJSONValue& Append(bool b);
175 wxJSONValue& Append(int i);
176 wxJSONValue& Append(unsigned int ui);
177 wxJSONValue& Append(short int i);
178 wxJSONValue& Append(unsigned short int ui);
179 wxJSONValue& Append(long int l);
180 wxJSONValue& Append(unsigned long int ul);
181#if defined(wxJSON_64BIT_INT)
182 wxJSONValue& Append(wxInt64 i);
183 wxJSONValue& Append(wxUint64 ui);
184#endif
185 wxJSONValue& Append(double d);
186 wxJSONValue& Append(const wxChar* str);
187 wxJSONValue& Append(const wxString& str);
188 wxJSONValue& Append(const wxMemoryBuffer& buff);
189 wxJSONValue& Append(const void* buff, size_t len);
190 bool Remove(int index);
191 bool Remove(const wxString& key);
192 void Clear();
193 bool Cat(const wxChar* str);
194 bool Cat(const wxString& str);
195 bool Cat(const wxMemoryBuffer& buff);
196
197 // retrieve an item
198 wxJSONValue& Item(unsigned index);
199 wxJSONValue& Item(const wxString& key);
200 wxJSONValue ItemAt(unsigned index) const;
201 wxJSONValue ItemAt(const wxString& key) const;
202
203 wxJSONValue& operator[](unsigned index);
204 wxJSONValue& operator[](const wxString& key);
205
206 wxJSONValue& operator=(int i);
207 wxJSONValue& operator=(unsigned int ui);
208 wxJSONValue& operator=(short int i);
209 wxJSONValue& operator=(unsigned short int ui);
210 wxJSONValue& operator=(long int l);
211 wxJSONValue& operator=(unsigned long int ul);
212#if defined(wxJSON_64BIT_INT)
213 wxJSONValue& operator=(wxInt64 i);
214 wxJSONValue& operator=(wxUint64 ui);
215#endif
216 wxJSONValue& operator=(bool b);
217 wxJSONValue& operator=(double d);
218 wxJSONValue& operator=(const wxChar* str);
219 wxJSONValue& operator=(const wxString& str);
220 wxJSONValue& operator=(const wxMemoryBuffer& buff);
221 // wxJSONValue& operator = ( const void* buff, size_t len ); cannot be
222 // declared
223 wxJSONValue& operator=(const wxJSONValue& value);
224
225 // get the value or a default value
226 wxJSONValue Get(const wxString& key, const wxJSONValue& defaultValue) const;
227
228 // comparison function
229 bool IsSameAs(const wxJSONValue& other) const;
230
231 // comment-related functions
232 int AddComment(const wxString& str,
233 int position = wxJSONVALUE_COMMENT_DEFAULT);
234 int AddComment(const wxArrayString& comments,
235 int position = wxJSONVALUE_COMMENT_DEFAULT);
236 wxString GetComment(int idx = -1) const;
237 int GetCommentPos() const;
238 int GetCommentCount() const;
239 void ClearComments();
240 const wxArrayString& GetCommentArray() const;
241
242 // debugging functions
243 wxString GetInfo() const;
244 wxString Dump(bool deep = false, int mode = 0) const;
245
246 // misc functions
247 wxJSONRefData* GetRefData() const;
248 wxJSONRefData* SetType(wxJSONType type);
249 int GetLineNo() const;
250 void SetLineNo(int num);
251
252 // public static functions: mainly used for debugging
253 static wxString TypeToString(wxJSONType type);
254 static wxString MemoryBuffToString(const wxMemoryBuffer& buff,
255 size_t len = -1);
256 static wxString MemoryBuffToString(const void* buff, size_t len,
257 size_t actualLen = -1);
258 static int CompareMemoryBuff(const wxMemoryBuffer& buff1,
259 const wxMemoryBuffer& buff2);
260 static int CompareMemoryBuff(const wxMemoryBuffer& buff1, const void* buff2);
261 static wxMemoryBuffer ArrayToMemoryBuff(const wxJSONValue& value);
262
263protected:
264 wxJSONValue* Find(unsigned index) const;
265 wxJSONValue* Find(const wxString& key) const;
266 void DeepCopy(const wxJSONValue& other);
267
268 wxJSONRefData* Init(wxJSONType type);
270
271 // overidden from wxObject
272 virtual wxJSONRefData* CloneRefData(const wxJSONRefData* data) const;
273 virtual wxJSONRefData* CreateRefData() const;
274
275 void SetRefData(wxJSONRefData* data);
276 void Ref(const wxJSONValue& clone);
277 void UnRef();
278 void UnShare();
279 void AllocExclusive();
280
283
284 // used for debugging purposes: only in debug builds.
285#if defined(WXJSON_USE_VALUE_COUNTER)
286 int m_progr;
287 static int sm_progr;
288#endif
289};
290
291#if !defined(wxJSON_USE_STL)
292// if using wxWidget's implementation of container classes we declare
293// the OBJARRAY are HASH_MAP _after_ the wxJSONValue is fully known
294WX_DECLARE_OBJARRAY(wxJSONValue, wxJSONInternalArray);
295WX_DECLARE_STRING_HASH_MAP(wxJSONValue, wxJSONInternalMap);
296#endif
297
298/***********************************************************************
299
300 class wxJSONRefData
301
302***********************************************************************/
303
305
316 int m_valInt;
317 unsigned int m_valUInt;
318 short int m_valShort;
319 unsigned short m_valUShort;
320 long int m_valLong;
321 unsigned long m_valULong;
322 double m_valDouble;
323 const wxChar* m_valCString;
324 bool m_valBool;
325#if defined(wxJSON_64BIT_INT)
326 wxInt64 m_valInt64;
327 wxUint64 m_valUInt64;
328#endif
329};
330
331//
332// access to the (unsigned) integer value is done through
333// the VAL_INT macro which expands to the 'long' integer
334// data member of the 'long long' integer if 64-bits integer
335// support is enabled
336#if defined(wxJSON_64BIT_INT)
337#define VAL_INT m_valInt64
338#define VAL_UINT m_valUInt64
339#else
340#define VAL_INT m_valLong
341#define VAL_UINT m_valULong
342#endif
343
344// class WXDLLIMPEXP_JSON wxJSONRefData : public wxObjectRefData
345class WXDLLIMPEXP_JSON wxJSONRefData {
346 // friend class wxJSONReader;
347 friend class wxJSONValue;
348 friend class wxJSONWriter;
349
350public:
352 virtual ~wxJSONRefData();
353
354 int GetRefCount() const;
355
356 // there is no need to define copy ctor
357
360
362 wxJSONType m_type;
363
365
372
374 wxString m_valString;
375
377 wxJSONInternalArray m_valArray;
378
380 wxJSONInternalMap m_valMap;
381
383
390
392 wxArrayString m_comments;
393
395
403
405
410 wxMemoryBuffer* m_memBuff;
411
412 // used for debugging purposes: only in debug builds.
413#if defined(WXJSON_USE_VALUE_COUNTER)
414 int m_progr;
415 static int sm_progr;
416#endif
417};
418
419#endif // WX_JSONVAL_H
The JSON parser.
Definition jsonreader.h:45
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
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
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
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
JSON Configuration System Definitions.
The actual value held by the wxJSONValue class (internal use).
Definition jsonval.h:315