wxJSONValue Class Reference

The JSON value class implementation. More...

#include <jsonval.h>

List of all members.

Public Member Functions

 wxJSONValue ()
 Constructors.
 wxJSONValue (wxJSONType type)
 wxJSONValue (int i)
 wxJSONValue (unsigned int i)
 wxJSONValue (short i)
 wxJSONValue (unsigned short i)
 wxJSONValue (long int i)
 wxJSONValue (unsigned long int i)
 wxJSONValue (wxInt64 i)
 wxJSONValue (wxUint64 ui)
 wxJSONValue (bool b)
 wxJSONValue (double d)
 wxJSONValue (const wxChar *str)
 wxJSONValue (const wxString &str)
 wxJSONValue (const wxJSONValue &other)
 Copy constructor.
virtual ~wxJSONValue ()
 Dtor - calls UnRef().
wxJSONType GetType () const
 Return the type of the value stored in the object.
bool IsValid () const
 Return TRUE if the value stored is valid.
bool IsNull () const
 Return TRUE if the type of the value is wxJSONTYPE_NULL.
bool IsInt () const
 Return TRUE if the type of the value stored is integer.
bool IsUInt () const
 Return TRUE if the type of the value stored is a unsigned int.
bool IsShort () const
 Return TRUE if the type of the value stored is 16-bit integer.
bool IsUShort () const
 Return TRUE if the type of the value stored is a unsigned short.
bool IsLong () const
 Return TRUE if the stored value is an integer which fits in a long int.
bool IsULong () const
 Return TRUE if the stored value is an integer which fits in a unsigned long int.
bool IsInt32 () const
 Return TRUE if the stored value is a 32-bits integer.
bool IsInt64 () const
 Return TRUE if the stored value is integer.
bool IsUInt32 () const
 Return TRUE if the stored value is a unsigned 32-bits integer.
bool IsUInt64 () const
 Return TRUE if the stored value is a unsigned integer.
bool IsBool () const
 Return TRUE if the type of the value stored is a boolean.
bool IsDouble () const
 Return TRUE if the type of the value stored is a double.
bool IsString () const
 Return TRUE if the type of the value stored is a wxString object.
bool IsCString () const
 Return TRUE if the type of the value stored is a pointer to a static C string.
bool IsArray () const
 Return TRUE if the type of the value stored is an array type.
bool IsObject () const
 Return TRUE if the type of this value is a key/value map.
int AsInt () const
 Return the stored value as an integer.
unsigned int AsUInt () const
 Return the stored value as a unsigned int.
short AsShort () const
 Returns the value as a short integer.
unsigned short AsUShort () const
 Returns the value as a unsigned short integer.
long int AsLong () const
 Returns the value as a long integer.
unsigned long AsULong () const
 Returns the value as a unsigned long integer.
bool AsInt (int &i) const
 Stores the value of this object in the provided argument.
bool AsUInt (unsigned int &ui) const
bool AsShort (short int &s) const
bool AsUShort (unsigned short &us) const
bool AsLong (long int &l) const
bool AsULong (unsigned long &ul) const
wxInt32 AsInt32 () const
 Returns the low-order 32 bits of the value as an integer.
wxUint32 AsUInt32 () const
 Returns the low-order 32 bits of the value as an unsigned integer.
wxInt64 AsInt64 () const
 Return the numeric value as a 64-bit integer.
wxUint64 AsUInt64 () const
 Return the numeric value as a 64-bit unsigned integer.
bool AsInt32 (wxInt32 &i32) const
bool AsUInt32 (wxUint32 &ui32) const
bool AsInt64 (wxInt64 &i64) const
bool AsUInt64 (wxUint64 &ui64) const
bool AsBool () const
 Return the stored value as a boolean.
double AsDouble () const
 Return the stored value as a double.
wxString AsString () const
 Return the stored value as a wxWidget's string.
const wxChar * AsCString () const
 Return the stored value as a pointer to a static C string.
bool AsBool (bool &b) const
bool AsDouble (double &d) const
bool AsString (wxString &str) const
 Return this string value in the provided argument.
bool AsCString (wxChar *ch) const
const wxJSONInternalMapAsMap () const
 Return the stored value as a map object.
const wxJSONInternalArrayAsArray () const
 Return the stored value as an array object.
bool HasMember (unsigned index) const
 Return TRUE if the object contains an element at the specified index.
bool HasMember (const wxString &key) const
 Return TRUE if the object contains an element at the specified key.
int Size () const
 Return the size of the array or map stored in this value.
wxArrayString GetMemberNames () const
 Return the array of keys of this JSON object.
wxJSONValueAppend (const wxJSONValue &value)
 Append the specified value in the array.
wxJSONValueAppend (bool b)
wxJSONValueAppend (int i)
wxJSONValueAppend (unsigned int ui)
wxJSONValueAppend (short int i)
wxJSONValueAppend (unsigned short int ui)
wxJSONValueAppend (long int l)
wxJSONValueAppend (unsigned long int ul)
wxJSONValueAppend (wxInt64 i)
wxJSONValueAppend (wxUint64 ui)
wxJSONValueAppend (double d)
wxJSONValueAppend (const wxChar *str)
wxJSONValueAppend (const wxString &str)
bool Remove (int index)
 Remove the item at the specified index or key.
bool Remove (const wxString &key)
void Clear ()
 Clear the object value.
bool Cat (const wxChar *str)
bool Cat (const wxString &str)
 Concatenate a string to this string object.
wxJSONValueItem (unsigned index)
 Return the item at the specified index.
wxJSONValueItem (const wxString &key)
 Return the item at the specified key.
wxJSONValue ItemAt (unsigned index) const
 Return the item at the specified index.
wxJSONValue ItemAt (const wxString &key) const
 Return the item at the specified key.
wxJSONValueoperator[] (unsigned index)
 Return the item at the specified index.
wxJSONValueoperator[] (const wxString &key)
 Return the item at the specified key.
wxJSONValueoperator= (int i)
 Assign the specified value to this object replacing the old value.
wxJSONValueoperator= (unsigned int ui)
wxJSONValueoperator= (short int i)
wxJSONValueoperator= (unsigned short int ui)
wxJSONValueoperator= (long int l)
wxJSONValueoperator= (unsigned long int ul)
wxJSONValueoperator= (wxInt64 i)
wxJSONValueoperator= (wxUint64 ui)
wxJSONValueoperator= (bool b)
wxJSONValueoperator= (double d)
wxJSONValueoperator= (const wxChar *str)
wxJSONValueoperator= (const wxString &str)
wxJSONValueoperator= (const wxJSONValue &value)
 Assignment operator using reference counting.
wxJSONValue Get (const wxString &key, const wxJSONValue &defaultValue) const
 Return a value or a default value.
bool IsSameAs (const wxJSONValue &other) const
 The comparison function.
int AddComment (const wxString &str, int position=wxJSONVALUE_COMMENT_DEFAULT)
 Add a comment to this JSON value object.
int AddComment (const wxArrayString &comments, int position=wxJSONVALUE_COMMENT_DEFAULT)
 Add one or more comments to this JSON value object.
wxString GetComment (int idx=-1) const
 Return a comment string.
int GetCommentPos () const
 Return the comment position.
int GetCommentCount () const
 Return the number of comment strings.
void ClearComments ()
 Clear all comment strings.
const wxArrayString & GetCommentArray () const
 Get the comment string's array.
wxString GetInfo () const
 Returns informations about the object.
wxString Dump (bool deep=false, int mode=0) const
 Returns informations about the object.
wxJSONRefDataGetRefData () const
 Return the pointer to the referenced data structure.
wxJSONRefDataSetType (wxJSONType type)
 Set the type of the stored value.
int GetLineNo () const
 Return the line number of this JSON value object.
void SetLineNo (int num)
 Set the line number of this JSON value object.

Static Public Member Functions

static wxString TypeToString (wxJSONType type)
 Return a string description of the type.

Protected Member Functions

wxJSONValueFind (unsigned index) const
 Find an element.
wxJSONValueFind (const wxString &key) const
 Find an element.
void DeepCopy (const wxJSONValue &other)
 Do a deep copy of the other object.
wxJSONRefDataInit (wxJSONType type)
 Initialize the JSON value class.
wxJSONRefDataCOW ()
 Make sure the referenced data is unique.
virtual wxJSONRefDataCloneRefData (const wxJSONRefData *data) const
 Make a copy of the referenced data.
virtual wxJSONRefDataCreateRefData () const
 Create a new data structure.
void SetRefData (wxJSONRefData *data)
 Set the pointer to the referenced data.
void Ref (const wxJSONValue &clone)
 Increments the referenced data counter.
void UnRef ()
 Unreferences the shared data.
void UnShare ()
 Makes an exclusive copy of shared data.
void AllocExclusive ()
 Makes a private copy of the referenced data.

Protected Attributes

wxJSONRefDatam_refData
 the referenced data

Friends

class wxJSONReader


Detailed Description

The JSON value class implementation.

This class holds a JSON value which may be of variuos types (see the wxJSONType constants for a description of the types). To know more about the internal representation of JSON values see wxJSON internals.

Starting from version 0.5 the wxJSON library supports 64-bits integers on platforms that have native support for very large integers. Note that the integer type is still stored as a generic wxJSONTYPE_(U)INT constant regardless the size of the value but the JSON value class defines functions in order to let the user know if an integer value fits in 16, 32 or 64 bit integer. To know more about 64-bits integer support see 64-bits and 32-bits integers

Storing values in a JSON value object of this class is very simple. The following is an example:

    wxJSONValue v( _T( "A string"));  // store a string value in the object
    wxString s = v.AsString();        // get the string value

    v = 12;            // now 'v' contains an integer value
    int i = v.AsInt(); // get the integer

To know more about using this class see the wxJSON - A brief tutorial.


Constructor & Destructor Documentation

wxJSONValue::wxJSONValue (  ) 

Constructors.

The overloaded constructors allow the user to construct a JSON value object that holds the specified value and type of value. The default ctor construct a valid JSON object that constains a null value.

If you want to create an empty JSON value object you have to use the wxJSONValue( wxJSONTYPE_INVALID ) ctor. Note that this object is not a valid JSON value - to know more about this topic see the SetType() function.

The C-string JSON value object
The wxJSONValue(const wxChar*) ctor allows you to create a JSON value object that contains a string value which is stored as a pointer-to-statci-string.

In fact, the ctor DOES NOT copy the string: it only stores the pointer in a data member and the pointed-to buffer is not deleted by the dtor. If the string is not static you have to use the wxJSONValue(const wxString&) constructor.

Also note that this does NOT mean that the value stored in this JSON object cannot change: you can assign whatever other value you want, an integer, a double or an array of values. What I intended is that the pointed-to string must exist for the lifetime of the wxJSONValue object. The following code is perfectly legal:

   wxJSONvalue aString( "this is a static string" );
   aString = 10;
To know more about this topic see wxJSON internals: the C string type

wxJSONValue::wxJSONValue ( wxJSONType  type  ) 

wxJSONValue::wxJSONValue ( int  i  ) 

wxJSONValue::wxJSONValue ( unsigned int  i  ) 

wxJSONValue::wxJSONValue ( short  i  ) 

wxJSONValue::wxJSONValue ( unsigned short  i  ) 

wxJSONValue::wxJSONValue ( long int  i  ) 

wxJSONValue::wxJSONValue ( unsigned long int  i  ) 

wxJSONValue::wxJSONValue ( wxInt64  i  ) 

wxJSONValue::wxJSONValue ( wxUint64  ui  ) 

wxJSONValue::wxJSONValue ( bool  b  ) 

wxJSONValue::wxJSONValue ( double  d  ) 

wxJSONValue::wxJSONValue ( const wxChar *  str  ) 

wxJSONValue::wxJSONValue ( const wxString &  str  ) 

wxJSONValue::wxJSONValue ( const wxJSONValue other  ) 

Copy constructor.

The function copies the content of other in this object. Note that the JSON value object is not really copied; the function calls Ref() in order to increment the reference count of the wxJSONRefData structure.

wxJSONValue::~wxJSONValue (  )  [virtual]

Dtor - calls UnRef().


Member Function Documentation

wxJSONType wxJSONValue::GetType (  )  const

Return the type of the value stored in the object.

This function is the only one that does not ASSERT that the m_refData data member is not NULL. In fact, if the JSON value object does not contain a pointer to a wxJSONRefData structure, the function returns the wxJSONTYPE_INVALID constant which represent an invalid JSON value object. Also note that the pointer to the referenced data structure should NEVER be NULL.

Integer types
Integers are stored internally in a signed/unsigned long int or, on platforms that support 64-bits integers, in a wx(U)Int64 data type. When constructed, it is assigned a generic integer type that only depends on the sign: wxJSON_(U)INT regardless the size of the stored value.

This function can be used to know the actual size requirement of the stored value and how it can be retrieved. The value returned by this function is:

  • for signed integers:
    • wxJSONTYPE_SHORT if the value is between SHORT_MIN and SHORT_MAX
    • wxJSONTYPE_LONG if the value is between LONG_MIN and LONG_MAX and greater than SHORT_MAX and less than SHORT_MIN
    • wxJSONTYPE_INT64 if the value is greater than LONG_MAX and less than LONG_MIN

  • for unsigned integers:
    • wxJSONTYPE_USHORT if the value is between 0 and USHORT_MAX
    • wxJSONTYPE_ULONG if the value is between 0 and ULONG_MAX and greater than USHORT_MAX
    • wxJSONTYPE_UINT64 if the value is greater than ULONG_MAX

Note that this function never returns the wxJSONTYPE_(U)INT constant because the int data type may have the same width as SHORT or LONG depending on the platform. This does not mean that you cannot use int as the return value: if you use wxWidgets to develop application in only one platform, you can use int because you know the size of the data type. Otherwise, if is preferable to always use long instead of int.

Also note that the class defines the IsInt() memberfunction which works fine regardless the actual width of the int data type. This function returns TRUE if the stored value fits in a int data type whatever its size is on the current platform (16 or 32-bits).

See also:
SetType IsInt

bool wxJSONValue::IsValid (  )  const

Return TRUE if the value stored is valid.

The function returns TRUE if the wxJSONValue object was correctly initialized - that is it contains a valid value. A JSON object is valid if its type is not equal to wxJSONTYPE_INVALID. Please note that the default ctor of wxJSONValue constructs a valid JSON object of type null. To create an invalid object you have to use;

bool wxJSONValue::IsNull (  )  const

Return TRUE if the type of the value is wxJSONTYPE_NULL.

bool wxJSONValue::IsInt (  )  const

Return TRUE if the type of the value stored is integer.

This function returns TRUE if the stored value is of type signed integer and the numeric value fits in a int data type. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_INT and:

   INT_MIN <= m_value <= INT_MAX

Note that if you are developing cross-platform applications you should never use int as the integer data type but long for 32-bits integers and short for 16-bits integers. This is because the int data type may have different width on different platforms. Regardless the widht of the data type (16 or 32 bits), the function returns the correct result because it relies on the INT_MAX and INT_MIN macros.

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsUInt (  )  const

Return TRUE if the type of the value stored is a unsigned int.

This function returns TRUE if the stored value is of type unsigned integer and the numeric value fits int a int data type. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_UINT and:

   0 <= m_value <= UINT_MAX

Note that if you are developing cross-platform applications you should never use unsigned int as the integer data type but unsigned long for 32-bits integers and unsigned short for 16-bits integers. This is because the unsigned int data type may have different width on different platforms. Regardless the widht of the data type (16 or 32 bits), the function returns the correct result because it relies on the UINT_MAX macro.

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsShort (  )  const

Return TRUE if the type of the value stored is 16-bit integer.

This function returns TRUE if the stored value is of type signed integer and the numeric value fits in a short int data type (16-bit integer). In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_INT and:

   SHORT_MIN <= m_value <= SHORT_MAX

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsUShort (  )  const

Return TRUE if the type of the value stored is a unsigned short.

This function returns TRUE if the stored value is of type unsigned integer and the numeric value fits in a unsigned short int data type. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_UINT and:

   0 <= m_value <= USHORT_MAX

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsLong (  )  const

Return TRUE if the stored value is an integer which fits in a long int.

This function returns TRUE if the stored value is of type signed LONG integer and the numeric value fits int a long int data type. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_INT and:

   LONG_MIN <= m_value <= LONG_MAX

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsULong (  )  const

Return TRUE if the stored value is an integer which fits in a unsigned long int.

This function returns TRUE if the stored value is of type unsigned LONG integer and the numeric value fits int a unsigned long int data type. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_UINT and:

   0 <= m_value <= ULONG_MAX

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsInt32 (  )  const

Return TRUE if the stored value is a 32-bits integer.

This function is only available on 64-bits platforms and returns TRUE if, and only if, the stored value is of type wxJSONTYPE_INT and the numeric value fits in a 32-bits signed integer. The function just calls IsLong() and returns the value returned by that function. The use of this function is deprecated: use IsLong() instead

bool wxJSONValue::IsInt64 (  )  const

Return TRUE if the stored value is integer.

This function returns TRUE if the stored value is of type signed integer. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_INT The function is only available if 64-bits integer support is enabled.

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsUInt32 (  )  const

Return TRUE if the stored value is a unsigned 32-bits integer.

This function is only available on 64-bits platforms and returns TRUE if, and only if, the stored value is of type wxJSONTYPE_UINT and the numeric value fits in a 32-bits unsigned integer. The function just calls IsULong() and returns the value returned by that function. The use of this function is deprecated: use IsULong() instead

bool wxJSONValue::IsUInt64 (  )  const

Return TRUE if the stored value is a unsigned integer.

This function returns TRUE if the stored value is of type unsigned integer. In other words, the function returns TRUE if the wxJSONRefData::m_type data member is of type wxJSONTYPE_UINT. The function is only available if 64-bits integer support is enabled.

See also:
64-bits and 32-bits integers

bool wxJSONValue::IsBool (  )  const

Return TRUE if the type of the value stored is a boolean.

bool wxJSONValue::IsDouble (  )  const

Return TRUE if the type of the value stored is a double.

bool wxJSONValue::IsString (  )  const

Return TRUE if the type of the value stored is a wxString object.

bool wxJSONValue::IsCString (  )  const

Return TRUE if the type of the value stored is a pointer to a static C string.

This function returns TRUE if, and only if the stored value is a pointer to a static C-string and the C-string storage is enabled in the wxJSON library. By default, C-string storage is not enabled in the library so this function always returns FALSE. To know more about C-strings read wxJSON internals: the C string type

bool wxJSONValue::IsArray (  )  const

Return TRUE if the type of the value stored is an array type.

bool wxJSONValue::IsObject (  )  const

Return TRUE if the type of this value is a key/value map.

int wxJSONValue::AsInt (  )  const

Return the stored value as an integer.

The function returns the stored value as an integer. Note that the function does not check that the type of the value is actually an integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value IsInt().

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

unsigned int wxJSONValue::AsUInt (  )  const

Return the stored value as a unsigned int.

The function returns the stored value as a unsigned integer. Note that the function does not check that the type of the value is actually a unsigned integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value is wxJSONTYPE_UINT.

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

short int wxJSONValue::AsShort (  )  const

Returns the value as a short integer.

The function returns the stored value as a short integer. Note that the function does not check that the type of the value is actually a short integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value IsShort().

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

unsigned short wxJSONValue::AsUShort (  )  const

Returns the value as a unsigned short integer.

The function returns the stored value as a unsigned short integer. Note that the function does not check that the type of the value is actually a unsigned short and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value IsUShort().

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

long int wxJSONValue::AsLong (  )  const

Returns the value as a long integer.

The function returns the stored value as a long integer. Note that the function does not check that the type of the value is actually a long integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value IsLong().

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

unsigned long int wxJSONValue::AsULong (  )  const

Returns the value as a unsigned long integer.

The function returns the stored value as a unsigned long integer. Note that the function does not check that the type of the value is actually a unsigned long integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value IsLong().

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

bool wxJSONValue::AsInt ( int &  i  )  const

Stores the value of this object in the provided argument.

The functions of the form AsXxxxxx(T&) are the same as the AsXxxxxxx() but store the value in the provided argument and return TRUE if the value of this object is of the correct type. By using these functions you can get the value and test if the JSON value is of the expected type in only one step. For example:

   int i; wxJSONValue v(10);
   if ( !v.AsInt( i )) {
     cout << "Error: value is not of the expected type";
   }
This is the same as:
   int i; wxJSONValue v(10);
   if ( v.IsInt() {
     i = v.AsInt();
   }
   else {
     cout << "Error: value is not of the expected type";
   }
Thanks to catalin who has suggested this new feature.

bool wxJSONValue::AsUInt ( unsigned int &  ui  )  const

bool wxJSONValue::AsShort ( short int &  s  )  const

bool wxJSONValue::AsUShort ( unsigned short &  us  )  const

bool wxJSONValue::AsLong ( long int &  l  )  const

bool wxJSONValue::AsULong ( unsigned long &  ul  )  const

wxInt32 wxJSONValue::AsInt32 (  )  const

Returns the low-order 32 bits of the value as an integer.

This function is only available on 64-bits platforms and returns the low-order 32-bits of the integer stored in the JSON value. Note that all integer types are stored as wx(U)Int64 data types by the JSON value class and that the function does not check that the numeric value fits in a 32-bit integer. The function just calls AsLong() and casts the value in a wxInt32 data type

See also:
Obtaining values from JSON value class

wxUint32 wxJSONValue::AsUInt32 (  )  const

Returns the low-order 32 bits of the value as an unsigned integer.

This function is only available on 64-bits platforms and returns the low-order 32-bits of the integer stored in the JSON value. Note that all integer types are stored as wx(U)Int64 data types by the JSON value class and that the function does not check that the numeric value fits in a 32-bit integer. The function just calls AsULong() and casts the value in a wxUInt32 data type

See also:
Obtaining values from JSON value class

wxInt64 wxJSONValue::AsInt64 (  )  const

Return the numeric value as a 64-bit integer.

This function is only available on 64-bits platforms and returns the numeric value as a 64-bit integer.

Note that the function does not check that the type of the value is actually an integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value is wxJSONTYPE_INT.

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

wxUint64 wxJSONValue::AsUInt64 (  )  const

Return the numeric value as a 64-bit unsigned integer.

This function is only available on 64-bits platforms and returns the numeric value as a 64-bit unsigned integer.

Note that the function does not check that the type of the value is actually an integer and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function wxJSON_ASSERTs that the type of the stored value is wxJSONTYPE_UINT.

See also:
64-bits and 32-bits integers

Obtaining values from JSON value class

bool wxJSONValue::AsInt32 ( wxInt32 &  i32  )  const

bool wxJSONValue::AsUInt32 ( wxUint32 &  ui32  )  const

bool wxJSONValue::AsInt64 ( wxInt64 &  i64  )  const

bool wxJSONValue::AsUInt64 ( wxUint64 &  ui64  )  const

bool wxJSONValue::AsBool (  )  const

Return the stored value as a boolean.

The function returns the stored value as a boolean. Note that the function does not check that the type of the value is actually a boolean and it just returns the content of the wxJSONValueHolder union. However, in debug builds, the function ASSERTs that the type of the stored value is wxJSONTYPE_BOOL.

See also:
Obtaining values from JSON value class

double wxJSONValue::AsDouble (  )  const

Return the stored value as a double.

The function returns the stored value as a double. Note that the function does not check that the type of the value is actually a double and it just returns the content of the wxJSONValueHolder union as if it was a double. However, in debug builds, the function ASSERTs that the type of the stored value IsDouble().

See also:
Obtaining values from JSON value class

wxString wxJSONValue::AsString (  )  const

Return the stored value as a wxWidget's string.

The function returns a string representation of the value stored in the JSON object. All value types are converted to a string but the actual string depends on the type:

For integer and double, the string is the string representation of the numerical value in decimal notation. For booleans the string returned is true or false. If the value is a NULL value the null literal string is returned.

If the value is of type wxJSONTYPE_INVALID, the literal string <invalid> is returned. Note that this is NOT a valid JSON text.

If the value is an array or map, the returned string is the number of elements is the array/object enclosed in the JSON special characters that identifies the array/object. Example:

    [0]    // an empty array
    {12}   // an object of 12 elements

See also:
Obtaining values from JSON value class

const wxChar * wxJSONValue::AsCString (  )  const

Return the stored value as a pointer to a static C string.

If the type of the value is stored as a C-string data type the function just returns that pointer. If the stored value is a wxString object, the function returns the pointer returned by the wxString::c_str() function. If the stored value is of all other JSON types, the functions returns a NULL pointer.

Note that in versions prior to 0.5, the function returned a NULL pointer also if the value is a wxString object.

See also:
wxJSON internals: the C string type

Obtaining values from JSON value class

bool wxJSONValue::AsBool ( bool &  b  )  const

bool wxJSONValue::AsDouble ( double &  d  )  const

bool wxJSONValue::AsString ( wxString &  str  )  const

Return this string value in the provided argument.

This function is different from AsString because the latter always returns a string also when this object does not contain a string. In that case, a string representation of this value is returned. This function, instead, returns TRUE only if this object contains a string, that is only if IsString() returns TRUE.

bool wxJSONValue::AsCString ( wxChar *  ch  )  const

const wxJSONInternalMap * wxJSONValue::AsMap (  )  const

Return the stored value as a map object.

This function is for testing and debugging purposes and you shold never use it. To retreive values from an array or map JSON object use the Item() or ItemAt() memberfunctions or the subscript operator. If the stored value is not a map type, returns a NULL pointer.

const wxJSONInternalArray * wxJSONValue::AsArray (  )  const

Return the stored value as an array object.

This function is for testing and debugging purposes and you shold never use it. To retreive values from an array or map JSON object use the Item() or ItemAt() memberfunctions or the subscript operator. If the stored value is not an array type, returns a NULL pointer.

bool wxJSONValue::HasMember ( unsigned  index  )  const

Return TRUE if the object contains an element at the specified index.

If the stoerd value is not an array or a map, the function returns FALSE.

bool wxJSONValue::HasMember ( const wxString &  key  )  const

Return TRUE if the object contains an element at the specified key.

If the stored value is not a key/map map, the function returns FALSE.

int wxJSONValue::Size (  )  const

Return the size of the array or map stored in this value.

Note that both the array and the key/value map may have a size of ZERO elements. If the stored value is not an array nor a key/value hashmap, the function returns -1.

wxArrayString wxJSONValue::GetMemberNames (  )  const

Return the array of keys of this JSON object.

If the stored value is a key/value map, the function returns an array of strings containing the key of all elements. Note that the returned array may be empty if the map has ZERO elements. An empty string array is also returned if the stored value is not a key/value map. Also note that in debug builds, the function wxJSON_ASSERTs that the type of the stored object is wxJSONTYPE_OBJECT.

wxJSONValue & wxJSONValue::Append ( const wxJSONValue value  ) 

Append the specified value in the array.

The function appends the value specified in the parameter to the array contained in this object. If this object does not contain an array type, the actual content is deleted, a new array type is created and the JSON value value is appended to the newly created array. Returns a reference to the appended object.

wxJSONValue & wxJSONValue::Append ( bool  b  ) 

wxJSONValue & wxJSONValue::Append ( int  i  ) 

wxJSONValue & wxJSONValue::Append ( unsigned int  ui  ) 

wxJSONValue & wxJSONValue::Append ( short int  i  ) 

wxJSONValue& wxJSONValue::Append ( unsigned short int  ui  ) 

wxJSONValue & wxJSONValue::Append ( long int  l  ) 

wxJSONValue& wxJSONValue::Append ( unsigned long int  ul  ) 

wxJSONValue & wxJSONValue::Append ( wxInt64  i  ) 

wxJSONValue & wxJSONValue::Append ( wxUint64  ui  ) 

wxJSONValue & wxJSONValue::Append ( double  d  ) 

wxJSONValue & wxJSONValue::Append ( const wxChar *  str  ) 

wxJSONValue & wxJSONValue::Append ( const wxString &  str  ) 

bool wxJSONValue::Remove ( int  index  ) 

Remove the item at the specified index or key.

The function removes the item at index index or at the specified key in the array or map. If this object does not contain an array (for a index parameter) or a map (for a key parameter), the function does nothing and returns FALSE. If the element does not exist, FALSE is returned.

bool wxJSONValue::Remove ( const wxString &  key  ) 

void wxJSONValue::Clear (  ) 

Clear the object value.

This function causes the object to be empty. The function simply calls UnRef() making this object to become invalid and set its type to wxJSONTYPE_INVALID.

bool wxJSONValue::Cat ( const wxChar *  str  ) 

bool wxJSONValue::Cat ( const wxString &  str  ) 

Concatenate a string to this string object.

The function concatenates str to the string contained in this object and returns TRUE if the operation is succefull. If the value stored in this value is not a string object the function does nothing and returns FALSE. Note that in order to be successfull, the value must contain a wxString object and not a pointer to C-string.

wxJSONValue & wxJSONValue::Item ( unsigned  index  ) 

Return the item at the specified index.

The function returns a reference to the object at the specified index. If the element does not exist, the array is enlarged to index + 1 elements and a reference to the last element will be returned. New elements will contain NULL values. If this object does not contain an array, the old value is replaced by an array object which will be enlarged to the needed dimension.

wxJSONValue & wxJSONValue::Item ( const wxString &  key  ) 

Return the item at the specified key.

The function returns a reference to the object in the map that has the specified key. If key does not exist, a new NULL value is created with the provided key and a reference to it is returned. If this object does not contain a map, the old value is replaced by a map object.

wxJSONValue wxJSONValue::ItemAt ( unsigned  index  )  const

Return the item at the specified index.

The function returns a copy of the object at the specified index. If the element does not exist, the function returns an invalid value.

wxJSONValue wxJSONValue::ItemAt ( const wxString &  key  )  const

Return the item at the specified key.

The function returns a copy of the object in the map that has the specified key. If key does not exist, an invalid value is returned.

wxJSONValue & wxJSONValue::operator[] ( unsigned  index  ) 

Return the item at the specified index.

The function returns a reference to the object at the specified index. If the element does not exist, the array is enlarged to index + 1 elements and a reference to the last element will be returned. New elements will contain NULL values. If this object does not contain an array, the old value is replaced by an array object.

wxJSONValue & wxJSONValue::operator[] ( const wxString &  key  ) 

Return the item at the specified key.

The function returns a reference to the object in the map that has the specified key. If key does not exist, a new NULL value is created with the provided key and a reference to it is returned. If this object does not contain a map, the old value is replaced by a map object.

wxJSONValue & wxJSONValue::operator= ( int  i  ) 

Assign the specified value to this object replacing the old value.

The assignment operator assigns to this object the value specified in the right operand of the assignment operator. Note that the old value is deleted but not the other data members in the wxJSONRefData structure. This is particularly usefull for the parser class which stores comment lines in a temporary wxJSONvalue object that is of type wxJSONTYPE_INVALID. As comment lines may apear before the value they refer to, comments are stored in a value that is not yet being read. when the value is read, it is assigned to the temporary JSON value object without deleting the comment lines.

wxJSONValue & wxJSONValue::operator= ( unsigned int  ui  ) 

wxJSONValue& wxJSONValue::operator= ( short int  i  ) 

wxJSONValue& wxJSONValue::operator= ( unsigned short int  ui  ) 

wxJSONValue& wxJSONValue::operator= ( long int  l  ) 

wxJSONValue& wxJSONValue::operator= ( unsigned long int  ul  ) 

wxJSONValue & wxJSONValue::operator= ( wxInt64  i  ) 

wxJSONValue & wxJSONValue::operator= ( wxUint64  ui  ) 

wxJSONValue & wxJSONValue::operator= ( bool  b  ) 

wxJSONValue & wxJSONValue::operator= ( double  d  ) 

wxJSONValue & wxJSONValue::operator= ( const wxChar *  str  ) 

wxJSONValue & wxJSONValue::operator= ( const wxString &  str  ) 

wxJSONValue & wxJSONValue::operator= ( const wxJSONValue other  ) 

Assignment operator using reference counting.

Unlike all other assignment operators, this one makes a swallow copy of the other JSON value object. The function calls Ref() to get a shared referenced data.

See also:
wxJSON internals: reference counting

wxJSONValue wxJSONValue::Get ( const wxString &  key,
const wxJSONValue defaultValue 
) const

Return a value or a default value.

This function returns a copy of the value object for the specified key. If the key is not found, a copy of defaultValue is returned. Note that the returned values are not real copy of the key or the default values because copy-on-write is used by this class. However, you have to treat them as real copies; in other words, if you change the values of the returned object your changes does not reflect in the otiginal value. Example:

  wxJSONValue defaultValue( 0 );
  wxJSONvalue v1;
  v1["key"] = 100;   // 'v1["key"]' contains the integer 100

  // 'v2' contains 100 but it is a swallow copy of 'v1["key"]'
  wxJSONValue v2 = v1.Get( "key", defaultValue );

  // 'v1["key"]' still contains 100
  v2 = 200;

  // if you want your change to be reflected in the 'v1' object
  // you have to assign it
  v1["key"] = v2;

bool wxJSONValue::IsSameAs ( const wxJSONValue other  )  const

The comparison function.

This function returns TRUE if this object looks like other. Note that this class does not define a comparison operator (the classical operator== function) because the notion of equal for JSON values objects is not applicable. The comment strings array are not compared: JSON value objects are the same if they contains the same values, regardless the comment's strings.

Note that the function does not return the element that cause the comparison to return FALSE. There is not a good structure to tell this information. If you need it for debugging purposes, you have to turn on the sameas tracing feature by setting the WXTRACE environment variable (you need a debug version of the application):

   export WXTRACE=sameas     // for unix systems that use bash

Note that if the two JSON value objects share the same referenced data, the function immediatly returns TRUE without doing a deep comparison which is, sure, useless. For further info see The comparison function and operator.

Bug:
comparing negative INTs and UINTs that have the same bit patterns (for example -1 and (unsigned) -1 (all bits sets) returns TRUE instead of FALSE (see Test2_7() in 'samples/test1.cpp')

int wxJSONValue::AddComment ( const wxString &  str,
int  position = wxJSONVALUE_COMMENT_DEFAULT 
)

Add a comment to this JSON value object.

The function adds a comment string to this JSON value object and returns the total number of comment strings belonging to this value. Note that the comment string must be a valid C/C++ comment because the wxJSONWriter does not modify it. In other words, a C++ comment string must start with '//' and must end with a new-line character. If the final LF char is missing, the automatically adds it. You can also add C-style comments which must be enclosed in the usual C-comment characters. For C-style comments, the function does not try to append the final comment characters but allows trailing whitespaces and new-line chars. The position parameter is one of:

  • wxJSONVALUE_COMMENT_BEFORE: the comment will be written before the value
  • wxJSONVALUE_COMMENT_INLINE: the comment will be written on the same line
  • wxJSONVALUE_COMMENT_AFTER: the comment will be written after the value
  • wxJSONVALUE_COMMENT_DEFAULT: the old value of comment's position is not changed; if no comments were added to the value object this is the same as wxJSONVALUE_COMMENT_BEFORE.
To know more about comment's storage see Adding C/C++ comments using member functions

int wxJSONValue::AddComment ( const wxArrayString &  comments,
int  position = wxJSONVALUE_COMMENT_DEFAULT 
)

Add one or more comments to this JSON value object.

The function adds the strings contained in comments to the comment's string array of this value object by calling the AddComment( const wxString&,int) function for every string in the comment array. Returns the number of strings correctly added.

wxString wxJSONValue::GetComment ( int  idx = -1  )  const

Return a comment string.

The function returns the comment string at index idx. If idx is out of range, an empty string is returned. If idx is equal to -1, then the function returns a string that contains all comment's strings stored in the array.

int wxJSONValue::GetCommentPos (  )  const

Return the comment position.

int wxJSONValue::GetCommentCount (  )  const

Return the number of comment strings.

void wxJSONValue::ClearComments (  ) 

Clear all comment strings.

const wxArrayString & wxJSONValue::GetCommentArray (  )  const

Get the comment string's array.

wxString wxJSONValue::GetInfo (  )  const

Returns informations about the object.

The function is only usefull for debugging purposes and will probably be dropped in future versions. You should not rely on this function to exist in future versions.

wxString wxJSONValue::Dump ( bool  deep = false,
int  indent = 0 
) const

Returns informations about the object.

The function is only usefull for debugging purposes and will probably be dropped in future versions. Returns a string that contains info about the object such as:

  • the type of the object
  • the size
  • the progressive counter
  • the pointer to referenced data
  • the progressive counter of referenced data
  • the number of share of referenced data
The deep parameter is used to specify if the function will be called recursively in order to dump sub-items. If the parameter is TRUE than a deep dump is executed.

The indent is the initial indentation: it is incremented by 3 every time the Dump() function is called recursively.

wxJSONRefData * wxJSONValue::GetRefData (  )  const

Return the pointer to the referenced data structure.

wxJSONRefData * wxJSONValue::SetType ( wxJSONType  type  ) 

Set the type of the stored value.

The function sets the type of the stored value as specified in the provided argument. If the actual type is equal to type, nothing happens and this JSON value object retains the original type and value. If the type differs, however, the original type and value are lost.

The function just sets the type of the object and not the value itself. If the object does not have a data structure it is allocated using the CreateRefData() function unless the type to be set is wxJSONTYPE_INVALID. In this case and if a data structure is not yet allocated, it is not allocated.

If the object already contains a data structure it is not deleted but the type is changed in the original data structure. Complex values in the old structure are cleared. The type argument can be one of the following:

  • wxJSONTYPE_INVALID: an empty (not initialized) JSON value
  • wxJSONTYPE_NULL: a NULL value
  • wxJSONTYPE_INT: an integer value
  • wxJSONTYPE_UINT: an unsigned integer
  • wxJSONTYPE_DOUBLE: a double precision number
  • wxJSONTYPE_BOOL: a boolean
  • wxJSONTYPE_CSTRING: a C string
  • wxJSONTYPE_STRING: a wxString object
  • wxJSONTYPE_ARRAY: an array of wxJSONValue objects
  • wxJSONTYPE_OBJECT: a hashmap of key/value pairs where value is a wxJSONValue object
  • wxJSONTYPE_LONG: a 32-bits integer value
  • wxJSONTYPE_ULONG: an unsigned 32-bits integer
  • wxJSONTYPE_INT64: a 64-bits integer value
  • wxJSONTYPE_UINT64: an unsigned 64-bits integer
  • wxJSONTYPE_SHORT: a signed short integer
  • wxJSONTYPE_USHORT: an unsigned short integer
The integer storage depends on the platform: for platforms that support 64-bits integers, integers are always stored as 64-bits integers. On platforms that do not support 64-bits integers, ints are stored as long int. To know more about the internal representation of integers, read 64-bits and 32-bits integers.

Note that there is no need to set a type for the object in order to assign a value to it. In other words, if you want to construct a JSON value which holds an integer value of 10, just use the specific constructor:

   wxJSONValue value( 10 );
which sets the integer type and also the numeric value. Moreover, there is no need to set the type for none of the handled types, not only for primitive types but for complex types, too. For example, if you want to construct an array of JSON values, just use the default ctor and call the Append() member function which will append the first element to the array and will set the array type:
   wxJSONValue value;
   value.Append( "a string" );
See also:
GetType

int wxJSONValue::GetLineNo (  )  const

Return the line number of this JSON value object.

The line number of a JSON value object is set to -1 when the object is constructed. The line number is set by the parser class, wxJSONReader, when a JSON text is read from a stream or a string. it is used when reading a comment line: comment lines that apear on the same line as a value are considered inline comments of the value.

void wxJSONValue::SetLineNo ( int  num  ) 

Set the line number of this JSON value object.

wxString wxJSONValue::TypeToString ( wxJSONType  type  )  [static]

Return a string description of the type.

This static function is only usefull for debugging purposes and should not be used by users of this class. It simply returns a string representation of the JSON value type stored in a object. For example, if type is wxJSONTYPE_INT the function returns the string "wxJSONTYPE_INT". If type is out of range, an empty string is returned (should never happen).

wxJSONValue * wxJSONValue::Find ( unsigned  index  )  const [protected]

Find an element.

The function returns a pointer to the element at index index or a NULL pointer if index does not exist. A NULL pointer is also returned if the object does not contain an array nor a key/value map.

wxJSONValue * wxJSONValue::Find ( const wxString &  key  )  const [protected]

Find an element.

The function returns a pointer to the element with key key or a NULL pointer if key does not exist. A NULL pointer is also returned if the object does not contain a key/value map.

void wxJSONValue::DeepCopy ( const wxJSONValue other  )  [protected]

Do a deep copy of the other object.

This function allocates a new ref-data structure and copies it from the object other.

wxJSONRefData * wxJSONValue::Init ( wxJSONType  type  )  [protected]

Initialize the JSON value class.

The function is called by the ctors and allocates a new instance of the wxJSONRefData class and sets the type of the JSON value. Note that only the type is set, not the value. Also note that this function may be called from other memberfunctions if the m_refData data member is NULL.

wxJSONRefData * wxJSONValue::COW (  )  [protected]

Make sure the referenced data is unique.

This function is called by all non-const member functions and makes sure that the referenced data is unique by calling UnShare() If the referenced data is shared acrosss other wxJSONValue instances, the UnShare() function makes a private copy of the shared data.

wxJSONRefData * wxJSONValue::CloneRefData ( const wxJSONRefData otherData  )  const [protected, virtual]

Make a copy of the referenced data.

The function allocates a new instance of the wxJSONRefData structure, copies the content of other and returns the pointer to the newly created structure. This function is called by the wxObject::UnRef() function when a non-const member function is called on multiple referenced data.

wxJSONRefData * wxJSONValue::CreateRefData (  )  const [protected, virtual]

Create a new data structure.

The function allocates a new instance of the wxJSONRefData structure and returns its pointer. The type of the JSON value is set to wxJSONTYPE_INVALID (= a not initialized value).

void wxJSONValue::SetRefData ( wxJSONRefData data  )  [protected]

Set the pointer to the referenced data.

void wxJSONValue::Ref ( const wxJSONValue clone  )  [protected]

Increments the referenced data counter.

void wxJSONValue::UnRef (  )  [protected]

Unreferences the shared data.

The function decrements the number of shares in wxJSONRefData::m_refCount and if it is ZERO, deletes the referenced data. It is called by the destructor.

void wxJSONValue::UnShare (  )  [protected]

Makes an exclusive copy of shared data.

void wxJSONValue::AllocExclusive (  )  [protected]

Makes a private copy of the referenced data.


Friends And Related Function Documentation

friend class wxJSONReader [friend]


Member Data Documentation

the referenced data


The documentation for this class was generated from the following files:

Generated on Fri Nov 13 22:52:30 2009 for wxJSON by  doxygen 1.5.5