00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #if !defined( _WX_JSONVAL_H )
00012 #define _WX_JSONVAL_H
00013
00014 #ifdef __GNUG__
00015 #pragma interface "jsonval.h"
00016 #endif
00017
00018
00019 #include "wx/wxprec.h"
00020
00021 #ifdef __BORLANDC__
00022 #pragma hdrstop
00023 #endif
00024
00025
00026
00027 #ifndef WX_PRECOMP
00028 #include <wx/object.h>
00029 #include <wx/hashmap.h>
00030 #include <wx/dynarray.h>
00031 #include <wx/arrstr.h>
00032 #endif
00033
00034
00035 #include "json_defs.h"
00036
00037
00038 class WXDLLIMPEXP_JSON wxJSONReader;
00039 class WXDLLIMPEXP_JSON wxJSONRefData;
00040
00041 #if defined( wxJSON_USE_MINGW )
00042
00043
00044 class WXDLLIMPEXP_JSON wxJSONValue;
00045 WX_DECLARE_OBJARRAY( wxJSONValue, wxJSONInternalArray );
00046 WX_DECLARE_STRING_HASH_MAP( wxJSONValue, wxJSONInternalMap );
00047 #else
00048 class WXDLLIMPEXP_JSON wxJSONInternalMap;
00049 class WXDLLIMPEXP_JSON wxJSONInternalArray;
00050 #endif
00051
00052
00054 enum wxJSONType {
00055 wxJSONTYPE_INVALID = 0,
00056 wxJSONTYPE_NULL,
00057 wxJSONTYPE_INT,
00058 wxJSONTYPE_UINT,
00059 wxJSONTYPE_DOUBLE,
00060 wxJSONTYPE_STRING,
00061 wxJSONTYPE_CSTRING,
00062 wxJSONTYPE_BOOL,
00063 wxJSONTYPE_ARRAY,
00064 wxJSONTYPE_OBJECT,
00065 wxJSONTYPE_LONG,
00066 wxJSONTYPE_INT64,
00067 wxJSONTYPE_ULONG,
00068 wxJSONTYPE_UINT64,
00069 wxJSONTYPE_SHORT,
00070 wxJSONTYPE_USHORT
00071 };
00072
00073
00074
00075 enum {
00076 wxJSONVALUE_COMMENT_DEFAULT = 0,
00077 wxJSONVALUE_COMMENT_BEFORE,
00078 wxJSONVALUE_COMMENT_AFTER,
00079 wxJSONVALUE_COMMENT_INLINE,
00080 };
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 class WXDLLIMPEXP_JSON wxJSONValue
00091 {
00092 friend class wxJSONReader;
00093
00094 public:
00095
00096
00097 wxJSONValue();
00098 wxJSONValue( wxJSONType type );
00099 wxJSONValue( int i );
00100 wxJSONValue( unsigned int i );
00101 wxJSONValue( short i );
00102 wxJSONValue( unsigned short i );
00103 wxJSONValue( long int i );
00104 wxJSONValue( unsigned long int i );
00105 #if defined( wxJSON_64BIT_INT)
00106 wxJSONValue( wxInt64 i );
00107 wxJSONValue( wxUint64 ui );
00108 #endif
00109 wxJSONValue( bool b );
00110 wxJSONValue( double d );
00111 wxJSONValue( const wxChar* str );
00112 wxJSONValue( const wxString& str );
00113 wxJSONValue( const wxJSONValue& other );
00114 virtual ~wxJSONValue();
00115
00116
00117 wxJSONType GetType() const;
00118 bool IsValid() const;
00119 bool IsNull() const;
00120 bool IsInt() const;
00121 bool IsUInt() const;
00122 bool IsShort() const;
00123 bool IsUShort() const;
00124 bool IsLong() const;
00125 bool IsULong() const;
00126 #if defined( wxJSON_64BIT_INT)
00127 bool IsInt32() const;
00128 bool IsInt64() const;
00129 bool IsUInt32() const;
00130 bool IsUInt64() const;
00131 #endif
00132 bool IsBool() const;
00133 bool IsDouble() const;
00134 bool IsString() const;
00135 bool IsCString() const;
00136 bool IsArray() const;
00137 bool IsObject() const;
00138
00139
00140 int AsInt() const;
00141 unsigned int AsUInt() const;
00142 short AsShort() const;
00143 unsigned short AsUShort() const;
00144 long int AsLong() const;
00145 unsigned long AsULong() const;
00146 bool AsInt( int& i ) const;
00147 bool AsUInt( unsigned int& ui ) const;
00148 bool AsShort( short int& s ) const;
00149 bool AsUShort( unsigned short& us ) const;
00150 bool AsLong( long int& l ) const;
00151 bool AsULong( unsigned long& ul ) const;
00152 #if defined( wxJSON_64BIT_INT)
00153 wxInt32 AsInt32() const;
00154 wxUint32 AsUInt32() const;
00155 wxInt64 AsInt64() const;
00156 wxUint64 AsUInt64() const;
00157 bool AsInt32( wxInt32& i32 ) const;
00158 bool AsUInt32( wxUint32& ui32 ) const;
00159 bool AsInt64( wxInt64& i64 ) const;
00160 bool AsUInt64( wxUint64& ui64 ) const;
00161 #endif
00162 bool AsBool() const;
00163 double AsDouble() const;
00164 wxString AsString() const;
00165 const wxChar* AsCString() const;
00166 bool AsBool( bool& b ) const;
00167 bool AsDouble( double& d ) const;
00168 bool AsString( wxString& str ) const;
00169 bool AsCString( wxChar* ch ) const;
00170
00171 const wxJSONInternalMap* AsMap() const;
00172 const wxJSONInternalArray* AsArray() const;
00173
00174
00175 bool HasMember( unsigned index ) const;
00176 bool HasMember( const wxString& key ) const;
00177 int Size() const;
00178 wxArrayString GetMemberNames() const;
00179
00180
00181 wxJSONValue& Append( const wxJSONValue& value );
00182 wxJSONValue& Append( bool b );
00183 wxJSONValue& Append( int i );
00184 wxJSONValue& Append( unsigned int ui );
00185 wxJSONValue& Append( short int i );
00186 wxJSONValue& Append( unsigned short int ui );
00187 wxJSONValue& Append( long int l );
00188 wxJSONValue& Append( unsigned long int ul );
00189 #if defined( wxJSON_64BIT_INT )
00190 wxJSONValue& Append( wxInt64 i );
00191 wxJSONValue& Append( wxUint64 ui );
00192 #endif
00193 wxJSONValue& Append( double d );
00194 wxJSONValue& Append( const wxChar* str );
00195 wxJSONValue& Append( const wxString& str );
00196 bool Remove( int index );
00197 bool Remove( const wxString& key );
00198 void Clear();
00199 bool Cat( const wxChar* str );
00200 bool Cat( const wxString& str );
00201
00202
00203 wxJSONValue& Item( unsigned index );
00204 wxJSONValue& Item( const wxString& key );
00205 wxJSONValue ItemAt( unsigned index ) const;
00206 wxJSONValue ItemAt( const wxString& key ) const;
00207
00208 wxJSONValue& operator [] ( unsigned index );
00209 wxJSONValue& operator [] ( const wxString& key );
00210
00211 wxJSONValue& operator = ( int i );
00212 wxJSONValue& operator = ( unsigned int ui );
00213 wxJSONValue& operator = ( short int i );
00214 wxJSONValue& operator = ( unsigned short int ui );
00215 wxJSONValue& operator = ( long int l );
00216 wxJSONValue& operator = ( unsigned long int ul );
00217 #if defined( wxJSON_64BIT_INT )
00218 wxJSONValue& operator = ( wxInt64 i );
00219 wxJSONValue& operator = ( wxUint64 ui );
00220 #endif
00221 wxJSONValue& operator = ( bool b );
00222 wxJSONValue& operator = ( double d );
00223 wxJSONValue& operator = ( const wxChar* str );
00224 wxJSONValue& operator = ( const wxString& str );
00225 wxJSONValue& operator = ( const wxJSONValue& value );
00226
00227
00228 wxJSONValue Get( const wxString& key, const wxJSONValue& defaultValue ) const;
00229
00230
00231 bool IsSameAs( const wxJSONValue& other ) const;
00232
00233
00234 int AddComment( const wxString& str, int position = wxJSONVALUE_COMMENT_DEFAULT );
00235 int AddComment( const wxArrayString& comments, int position = wxJSONVALUE_COMMENT_DEFAULT );
00236 wxString GetComment( int idx = -1 ) const;
00237 int GetCommentPos() const;
00238 int GetCommentCount() const;
00239 void ClearComments();
00240 const wxArrayString& GetCommentArray() const;
00241
00242
00243 wxString GetInfo() const;
00244 wxString Dump( bool deep = false, int mode = 0 ) const;
00245
00246 wxJSONRefData* GetRefData() const;
00247 wxJSONRefData* SetType( wxJSONType type );
00248
00249 int GetLineNo() const;
00250 void SetLineNo( int num );
00251
00252 static wxString TypeToString( wxJSONType type );
00253
00254 protected:
00255 wxJSONValue* Find( unsigned index ) const;
00256 wxJSONValue* Find( const wxString& key ) const;
00257
00258 void DeepCopy( const wxJSONValue& other );
00259
00260 wxJSONRefData* Init( wxJSONType type );
00261 wxJSONRefData* COW();
00262
00263
00264 virtual wxJSONRefData* CloneRefData(const wxJSONRefData *data) const;
00265 virtual wxJSONRefData* CreateRefData() const;
00266
00267 void SetRefData(wxJSONRefData* data);
00268 void Ref(const wxJSONValue& clone);
00269 void UnRef();
00270 void UnShare();
00271 void AllocExclusive();
00272
00274 wxJSONRefData* m_refData;
00275
00276
00277 #if defined( WXJSON_USE_VALUE_COUNTER )
00278 int m_progr;
00279 static int sm_progr;
00280 #endif
00281 };
00282
00283
00284 #if !defined( wxJSON_USE_MINGW )
00285
00286
00287 WX_DECLARE_OBJARRAY( wxJSONValue, wxJSONInternalArray );
00288 WX_DECLARE_STRING_HASH_MAP( wxJSONValue, wxJSONInternalMap );
00289 #endif
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00302
00312 union wxJSONValueHolder {
00313 int m_valInt;
00314 unsigned int m_valUInt;
00315 short int m_valShort;
00316 unsigned short m_valUShort;
00317 long int m_valLong;
00318 unsigned long m_valULong;
00319 double m_valDouble;
00320 const wxChar* m_valCString;
00321 bool m_valBool;
00322 #if defined( wxJSON_64BIT_INT )
00323 wxInt64 m_valInt64;
00324 wxUint64 m_valUInt64;
00325 #endif
00326 };
00327
00328
00329
00330
00331
00332
00333 #if defined( wxJSON_64BIT_INT )
00334 #define VAL_INT m_valInt64
00335 #define VAL_UINT m_valUInt64
00336 #else
00337 #define VAL_INT m_valLong
00338 #define VAL_UINT m_valULong
00339 #endif
00340
00341
00342
00343
00344 class WXDLLIMPEXP_JSON wxJSONRefData
00345 {
00346
00347 friend class wxJSONValue;
00348 friend class wxJSONWriter;
00349
00350 public:
00351
00352 wxJSONRefData();
00353 virtual ~wxJSONRefData();
00354
00355 int GetRefCount() const;
00356
00357
00358
00359
00360 protected:
00362 int m_refCount;
00363
00365 wxJSONType m_type;
00366
00368
00374 wxJSONValueHolder m_value;
00375
00377 wxString m_valString;
00378
00380 wxJSONInternalArray m_valArray;
00381
00383 wxJSONInternalMap m_valMap;
00384
00385
00387
00393 int m_commentPos;
00394
00396 wxArrayString m_comments;
00397
00399
00406 int m_lineNo;
00407
00408
00409
00410 #if defined( WXJSON_USE_VALUE_COUNTER )
00411 int m_progr;
00412 static int sm_progr;
00413 #endif
00414 };
00415
00416
00417
00418 #endif // not defined _WX_JSONVAL_H
00419
00420