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