The JSON value class implementation. More...
Public Member Functions | |
wxJSONValue () | |
Constructors. More... | |
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 (bool b) | |
wxJSONValue (double d) | |
wxJSONValue (const wxChar *str) | |
wxJSONValue (const wxString &str) | |
wxJSONValue (const wxMemoryBuffer &buff) | |
Construct a JSON value object of type memory buffer. More... | |
wxJSONValue (const void *buff, size_t len) | |
Construct a JSON value object of type memory buffer. More... | |
wxJSONValue (const wxJSONValue &other) | |
Copy constructor. More... | |
virtual | ~wxJSONValue () |
Dtor - calls UnRef(). | |
wxJSONType | GetType () const |
Return the type of the value stored in the object. More... | |
bool | IsValid () const |
Return TRUE if the value stored is valid. More... | |
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. More... | |
bool | IsUInt () const |
Return TRUE if the type of the value stored is a unsigned int. More... | |
bool | IsShort () const |
Return TRUE if the type of the value stored is 16-bit integer. More... | |
bool | IsUShort () const |
Return TRUE if the type of the value stored is a unsigned short. More... | |
bool | IsLong () const |
Return TRUE if the stored value is an integer which fits in a long int. More... | |
bool | IsULong () const |
Return TRUE if the stored value is an integer which fits in a unsigned long int. More... | |
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. More... | |
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. | |
bool | IsMemoryBuff () const |
Return TRUE if the type of this value is a binary memory buffer. | |
int | AsInt () const |
Return the stored value as an integer. More... | |
unsigned int | AsUInt () const |
Return the stored value as a unsigned int. More... | |
short | AsShort () const |
Returns the value as a short integer. More... | |
unsigned short | AsUShort () const |
Returns the value as a unsigned short integer. More... | |
long int | AsLong () const |
Returns the value as a long integer. More... | |
unsigned long | AsULong () const |
Returns the value as a unsigned long integer. More... | |
bool | AsInt (int &i) const |
Stores the value of this object in the provided argument. More... | |
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 |
bool | AsBool () const |
Return the stored value as a boolean. More... | |
double | AsDouble () const |
Return the stored value as a double. More... | |
wxString | AsString () const |
Return the stored value as a wxWidget's string. More... | |
const wxChar * | AsCString () const |
Return the stored value as a pointer to a static C string. More... | |
bool | AsBool (bool &b) const |
bool | AsDouble (double &d) const |
bool | AsString (wxString &str) const |
Return this string value in the provided argument. More... | |
bool | AsCString (wxChar *ch) const |
wxMemoryBuffer | AsMemoryBuff () const |
Returns the value as a memory buffer. More... | |
bool | AsMemoryBuff (wxMemoryBuffer &buff) const |
Returns the value as a memory buffer. More... | |
const wxJSONInternalMap * | AsMap () const |
Return the stored value as a map object. More... | |
const wxJSONInternalArray * | AsArray () const |
Return the stored value as an array object. More... | |
bool | HasMember (unsigned index) const |
Return TRUE if the object contains an element at the specified index. More... | |
bool | HasMember (const wxString &key) const |
Return TRUE if the object contains an element at the specified key. More... | |
int | Size () const |
Return the size of the array or map stored in this value. More... | |
wxArrayString | GetMemberNames () const |
Return the array of keys of this JSON object. More... | |
wxJSONValue & | Append (const wxJSONValue &value) |
Append the specified value in the array. More... | |
wxJSONValue & | Append (bool b) |
wxJSONValue & | Append (int i) |
wxJSONValue & | Append (unsigned int ui) |
wxJSONValue & | Append (short int i) |
wxJSONValue & | Append (unsigned short int ui) |
wxJSONValue & | Append (long int l) |
wxJSONValue & | Append (unsigned long int ul) |
wxJSONValue & | Append (double d) |
wxJSONValue & | Append (const wxChar *str) |
wxJSONValue & | Append (const wxString &str) |
wxJSONValue & | Append (const wxMemoryBuffer &buff) |
wxJSONValue & | Append (const void *buff, size_t len) |
bool | Remove (int index) |
Remove the item at the specified index or key. More... | |
bool | Remove (const wxString &key) |
void | Clear () |
Clear the object value. More... | |
bool | Cat (const wxChar *str) |
bool | Cat (const wxString &str) |
Concatenate a string to this string object. More... | |
bool | Cat (const wxMemoryBuffer &buff) |
Concatenate a memory buffer to this memory buffer object. More... | |
wxJSONValue & | Item (unsigned index) |
Return the item at the specified index. More... | |
wxJSONValue & | Item (const wxString &key) |
Return the item at the specified key. More... | |
wxJSONValue | ItemAt (unsigned index) const |
Return the item at the specified index. More... | |
wxJSONValue | ItemAt (const wxString &key) const |
Return the item at the specified key. More... | |
wxJSONValue & | operator[] (unsigned index) |
Return the item at the specified index. More... | |
wxJSONValue & | operator[] (const wxString &key) |
Return the item at the specified key. More... | |
wxJSONValue | operator[] (unsigned index) const |
wxJSONValue | operator[] (const wxString &key) const |
wxJSONValue & | operator= (int i) |
Assign the specified value to this object replacing the old value. More... | |
wxJSONValue & | operator= (unsigned int ui) |
wxJSONValue & | operator= (short int i) |
wxJSONValue & | operator= (unsigned short int ui) |
wxJSONValue & | operator= (long int l) |
wxJSONValue & | operator= (unsigned long int ul) |
wxJSONValue & | operator= (bool b) |
wxJSONValue & | operator= (double d) |
wxJSONValue & | operator= (const wxChar *str) |
wxJSONValue & | operator= (const wxString &str) |
wxJSONValue & | operator= (const wxMemoryBuffer &buff) |
Assigns to this object a memory buffer type. More... | |
wxJSONValue & | operator= (const wxJSONValue &value) |
Assignment operator using reference counting. More... | |
wxJSONValue | Get (const wxString &key, const wxJSONValue &defaultValue) const |
Return a value or a default value. More... | |
bool | IsSameAs (const wxJSONValue &other) const |
The comparison function. More... | |
int | AddComment (const wxString &str, int position=wxJSONVALUE_COMMENT_DEFAULT) |
Add a comment to this JSON value object. More... | |
int | AddComment (const wxArrayString &comments, int position=wxJSONVALUE_COMMENT_DEFAULT) |
Add one or more comments to this JSON value object. More... | |
wxString | GetComment (int idx=-1) const |
Return a comment string. More... | |
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. More... | |
wxString | Dump (bool deep=false, int mode=0) const |
Returns informations about the object. More... | |
wxJSONRefData * | GetRefData () const |
Return the pointer to the referenced data structure. | |
wxJSONRefData * | SetType (wxJSONType type) |
Set the type of the stored value. More... | |
int | GetLineNo () const |
Return the line number of this JSON value object. More... | |
void | SetLineNo (int num) |
Set the line number of this JSON value object. | |
void | UnShare () |
Makes an exclusive copy of shared data. | |
Static Public Member Functions | |
static wxString | TypeToString (wxJSONType type) |
Return a string description of the type. More... | |
static wxString | MemoryBuffToString (const wxMemoryBuffer &buff, size_t len=-1) |
Convert memory buffer object to a string representation. | |
static wxString | MemoryBuffToString (const void *buff, size_t len, size_t actualLen=-1) |
Convert a binary memory buffer to a string representation. | |
static int | CompareMemoryBuff (const wxMemoryBuffer &buff1, const wxMemoryBuffer &buff2) |
Compares two memory buffer objects. More... | |
static int | CompareMemoryBuff (const wxMemoryBuffer &buff1, const void *buff2) |
Compares a memory buffer object and a memory buffer. More... | |
static wxMemoryBuffer | ArrayToMemoryBuff (const wxJSONValue &value) |
Converts an array of INTs to a memory buffer. More... | |
Protected Member Functions | |
wxJSONValue * | Find (unsigned index) const |
Find an element. More... | |
wxJSONValue * | Find (const wxString &key) const |
Find an element. More... | |
void | DeepCopy (const wxJSONValue &other) |
Do a deep copy of the other object. More... | |
wxJSONRefData * | Init (wxJSONType type) |
Initialize the JSON value class. More... | |
wxJSONRefData * | COW () |
Make sure the referenced data is unique. More... | |
virtual wxJSONRefData * | CloneRefData (const wxJSONRefData *data) const |
Make a copy of the referenced data. More... | |
virtual wxJSONRefData * | CreateRefData () const |
Create a new data structure. More... | |
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. More... | |
void | AllocExclusive () |
Makes a private copy of the referenced data. | |
Protected Attributes | |
wxJSONRefData * | m_refData |
the referenced data | |
Friends | |
class | wxJSONReader |
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 pg_json_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 json_internals_integer
Storing values in a JSON value object of this class is very simple. The following is an example:
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-static-string. Beware that this 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:
To know more about this topic see json_internals_cstring.
Starting from version 1.3 the class can hold binary memory buffers as an extension to the JSON syntax. Memory buffers are stored as wxMemoryBuffer objects which contain binary data. The class uses reference counting for the copy and assignment operation but it is not a copy-on-write structure. To know more about memory buffers read wxjson_tutorial_memorybuff
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 invalid 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.
To create an empty array or key/value map use the following:
wxJSONValue::wxJSONValue | ( | wxJSONType | type | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | int | i | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | unsigned int | ui | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | unsigned short | ui | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | long int | l | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | unsigned long int | ul | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | bool | b | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | double | d | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | const wxChar * | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | const wxString & | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue::wxJSONValue | ( | const wxMemoryBuffer & | buff | ) |
Construct a JSON value object of type memory buffer.
Note that this ctor makes a deep copy of buff
so changes made to the original buffer does not reflect to the buffer stored in this JSON value.
wxJSONValue::wxJSONValue | ( | const void * | buff, |
size_t | len | ||
) |
Construct a JSON value object of type memory buffer.
Note that this ctor makes a deep copy of buff
so changes made to the original buffer does not reflect to the buffer stored in this JSON value.
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.
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:
To know more about comment's storage see json_comment_add
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.
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 | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | int | i | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | unsigned int | ui | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | short int | i | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | long int | l | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | double | d | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | const wxChar * | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | const wxString & | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | const wxMemoryBuffer & | buff | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::Append | ( | const void * | buff, |
size_t | len | ||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
static |
Converts an array of INTs to a memory buffer.
This static function converts an array of INTs stored in a wxJSONvalue object into a memory buffer object. The wxJSONvalue object passed as parameter must be of type ARRAY and must contain INT types whose values are between 0 and 255.
Every element of the array si converted to a BYTE value and appended to the returned wxMemoryBuffer object. The following rules apply in the conversion:
value
is not an ARRAY type, an empty memory buffer is returned value
array contains elements of type other than INT, those elements are ignored value
array contains elements of type INT which value is outside the range 0..255, those elements are ignored value
array contains only ignored elements an empty wxMemoryBuffer object is returned.This function can be used to get a memory buffer object from valid JSON text. Please note that the wxJSONReader cannot know which array of INTs represent a binary memory buffer unless you use the wxJSON memory buffer extension in the writer and in the reader.
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::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.
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.
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()
.
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()
.
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:
This is the same as:
Thanks to catalin who has suggested this new feature.
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()
.
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.
wxMemoryBuffer wxJSONValue::AsMemoryBuff | ( | ) | const |
Returns the value as a memory buffer.
The function returns the memory buffer object stored in this JSON object. Note that as of wxWidgets 2.8 and 2.9 the wxMemoryBuffer object uses reference counting when copying the actual buffer but the class itself is not a copy-on-write structure so changes made to one buffer affects all other copies made from it. This means that if you make a change to the returned copy of the memory buffer, the change affects also the memory buffer stored in this JSON value.
If this JSON object does not contain a wxJSONTYPE_MEMORYBUFF type the function returns an empty memory buffer object. An empty memory buffer is also returned if this JSON type contains a valid, empty memory buffer. You have to use the IsMemoryBuff() function to known the type of the JSON value contained in this object, or the overloaded version of this function.
bool wxJSONValue::AsMemoryBuff | ( | wxMemoryBuffer & | buff | ) | const |
Returns the value as a memory buffer.
The function returns the memory buffer object stored in this JSON object. Note that as of wxWidgets 2.8 and 2.9 the wxMemoryBuffer object uses reference counting when copying the actual buffer but the class itself is not a copy-on-write structure so changes made to one buffer affects all other copies made from it. This means that if you make a change to the returned copy of the memory buffer, the change affects also the memory buffer stored in this JSON value.
If this JSON object does not contain a wxJSONTYPE_MEMORYBUFF type the function returns an empty memory buffer object. An empty memory buffer is also returned if this JSON type contains a valid, empty memory buffer. You have to use the IsMemoryBuff() function to known the type of the JSON value contained in this object, or the overloaded version of this function.
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()
.
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 by this function and returned as a string:
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:
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. Also note that the string value is only stored in str
if this object actually contains a string or c-string value. str
will never contain a string representation of other types.
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.
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()
.
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()
.
bool wxJSONValue::Cat | ( | const wxChar * | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.
bool wxJSONValue::Cat | ( | const wxMemoryBuffer & | buff | ) |
Concatenate a memory buffer to this memory buffer object.
The function concatenates buff
to the wxMemoryBuffer object contained in this object and returns TRUE if the operation is succefull. If the value stored in this value is not a memory buffer object the function does nothing and returns FALSE.
void wxJSONValue::Clear | ( | void | ) |
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.
|
protectedvirtual |
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.
|
static |
Compares two memory buffer objects.
The function is the counterpart of the comparison operator == for two wxMemoryBuffer objects. You may noticed that the wxMemoryBuffer class does not define comparison operators so if you write a code snippset like the following:
you may expect that r is TRUE, because both objects contain the same data. This is not true. The result you get is FALSE because the default comparison operator is used, which just compares the data members of the class. The data member is the pointer to the allocated memory that contains the data and they are not equal. This function uses the (fast) memcmp function to compare the actual data contained in the nenory buffer objects thus doing a deep comparison. The function returns the return value of memcmp:
the memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of buff1
is found, respectively, to be less than, to match, or be greater than the first n bytes of buff2
.
|
static |
Compares a memory buffer object and a memory buffer.
The function compares the data contained in a memory buffer object with a memory buffer. This function uses the (fast) memcmp function to compare the actual data contained in the nenory buffer object thus doing a deep comparison. The function returns the return value of memcmp:
The memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of buff1
is found, respectively, to be less than, to match, or be greater than the first n bytes of buff2
.
|
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.
|
protectedvirtual |
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).
|
protected |
Do a deep copy of the other object.
This function allocates a new ref-data structure and copies it from the object other
.
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 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.
|
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.
|
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.
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:
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.
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.
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.
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.
wxJSONType wxJSONValue::GetType | ( | void | ) | 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.
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:
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).
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.
|
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.
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 json_internals_cstring
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:
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.
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:
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):
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 json_internals_compare.
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:
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:
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.
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:
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:
bool wxJSONValue::IsValid | ( | void | ) | 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;
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= | ( | 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 | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::operator= | ( | bool | b | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::operator= | ( | double | d | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::operator= | ( | const wxChar * | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::operator= | ( | const wxString & | str | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
wxJSONValue & wxJSONValue::operator= | ( | const wxMemoryBuffer & | buff | ) |
Assigns to this object a memory buffer type.
As with the ctor, this function makes a deep copy of the memory buffer buff
so changes made to the original buffer does not reflect to the memory buffer stored in this JSON value.
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.
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.
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 | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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:
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 json_internals_integer.
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:
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:
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.
|
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).
|
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 and by the copy-on-write functions.