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