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