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_STL )
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
00247 wxJSONRefData* GetRefData() const;
00248 wxJSONRefData* SetType( wxJSONType type );
00249 int GetLineNo() const;
00250 void SetLineNo( int num );
00251 static wxString TypeToString( wxJSONType type );
00252
00253 protected:
00254 wxJSONValue* Find( unsigned index ) const;
00255 wxJSONValue* Find( const wxString& key ) const;
00256 void DeepCopy( const wxJSONValue& other );
00257
00258 wxJSONRefData* Init( wxJSONType type );
00259 wxJSONRefData* COW();
00260
00261
00262 virtual wxJSONRefData* CloneRefData(const wxJSONRefData *data) const;
00263 virtual wxJSONRefData* CreateRefData() const;
00264
00265 void SetRefData(wxJSONRefData* data);
00266 void Ref(const wxJSONValue& clone);
00267 void UnRef();
00268 void UnShare();
00269 void AllocExclusive();
00270
00272 wxJSONRefData* m_refData;
00273
00274
00275 #if defined( WXJSON_USE_VALUE_COUNTER )
00276 int m_progr;
00277 static int sm_progr;
00278 #endif
00279 };
00280
00281
00282 #if !defined( wxJSON_USE_STL )
00283
00284
00285 WX_DECLARE_OBJARRAY( wxJSONValue, wxJSONInternalArray );
00286 WX_DECLARE_STRING_HASH_MAP( wxJSONValue, wxJSONInternalMap );
00287 #endif
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00300
00310 union wxJSONValueHolder {
00311 int m_valInt;
00312 unsigned int m_valUInt;
00313 short int m_valShort;
00314 unsigned short m_valUShort;
00315 long int m_valLong;
00316 unsigned long m_valULong;
00317 double m_valDouble;
00318 const wxChar* m_valCString;
00319 bool m_valBool;
00320 #if defined( wxJSON_64BIT_INT )
00321 wxInt64 m_valInt64;
00322 wxUint64 m_valUInt64;
00323 #endif
00324 };
00325
00326
00327
00328
00329
00330
00331 #if defined( wxJSON_64BIT_INT )
00332 #define VAL_INT m_valInt64
00333 #define VAL_UINT m_valUInt64
00334 #else
00335 #define VAL_INT m_valLong
00336 #define VAL_UINT m_valULong
00337 #endif
00338
00339
00340
00341
00342 class WXDLLIMPEXP_JSON wxJSONRefData
00343 {
00344
00345 friend class wxJSONValue;
00346 friend class wxJSONWriter;
00347
00348 public:
00349
00350 wxJSONRefData();
00351 virtual ~wxJSONRefData();
00352
00353 int GetRefCount() const;
00354
00355
00356
00358 int m_refCount;
00359
00361 wxJSONType m_type;
00362
00364
00370 wxJSONValueHolder m_value;
00371
00373 wxString m_valString;
00374
00376 wxJSONInternalArray m_valArray;
00377
00379 wxJSONInternalMap m_valMap;
00380
00381
00383
00389 int m_commentPos;
00390
00392 wxArrayString m_comments;
00393
00395
00402 int m_lineNo;
00403
00404
00405
00406 #if defined( WXJSON_USE_VALUE_COUNTER )
00407 int m_progr;
00408 static int sm_progr;
00409 #endif
00410 };
00411
00412
00413
00414 #endif // not defined _WX_JSONVAL_H
00415
00416