CloudBuilder  2.8.4
Making social games is easy !
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
CotCHelpers::CHJSON Class Reference

#include <CHJSON.h>

Classes

struct  Iterator
 

Public Types

enum  jsonType { jsonFalse, jsonTrue, jsonNULL, jsonNumber, jsonString, jsonArray, jsonObject }
 

Public Member Functions

 CHJSON ()
 
 ~CHJSON ()
 
bool Has (const char *key) const
 
const CHJSONGet (int aIndex) const
 
const CHJSONGet (const char *aItem) const
 
const CHJSONGetSafe (int aIndex) const
 
const CHJSONGetSafe (const char *aItem) const
 
double GetDouble (const char *aItem, double defaultValue=0) const
 
int GetInt (const char *aItem, int defaultValue=0) const
 
bool GetBool (const char *aItem, bool defaultValue=false) const
 
const char * GetString (const char *key, const char *defaultValue=0) const
 
void Delete (const char *aItem)
 
void Clear ()
 
CHJSONDuplicate () const
 
void Put (const char *aKey, bool aValue)
 
void Put (const char *aKey, int aValue)
 
void Put (const char *aKey, double aValue)
 
void Put (const char *aKey, const char *aValue)
 
void Put (const char *aKey, CHJSON *json)
 
void Put (const char *aKey, const CHJSON *json)
 
void Put (const char *aKey, const CHJSON &json)
 
Iterator begin () const
 
Iterator end () const
 
char * print () const
 
char * printFormatted () const
 
void Add (CHJSON *aJson)
 
int size () const
 
 CHJSON (bool aBoolean)
 
 CHJSON (double aNumber)
 
 CHJSON (const char *aString)
 
const char * name () const
 
jsonType type () const
 
const char * valueString () const
 
int valueInt () const
 
double valueDouble () const
 

Static Public Member Functions

static CHJSONparse (const char *aJsonString)
 
static const CHJSONEmpty ()
 
static CHJSONArray ()
 
static CHJSONArray (int *aNumbers, int aCount)
 
static CHJSONArray (float *aNumbers, int aCount)
 
static CHJSONArray (double *aNumbers, int aCount)
 
static CHJSONArray (const char **aStrings, int aCount)
 
static CHJSONcreateNull (void)
 

Protected Member Functions

void AddStringSafe (const char *aItem, const char *aValue)
 
void AddOrReplaceStringSafe (const char *aItem, const char *aValue)
 
void Add (const char *aItem, CHJSON *aJson)
 
void Add (const char *aItem, double aNumber)
 
void Add (const char *aItem, int aNumber)
 
void Add (const char *aItem, bool aFlag)
 
void Replace (const char *aItem, CHJSON *aJson)
 

Static Protected Member Functions

static CHJSONinitWith (const char **aArgs)
 
static CHJSONdup (const CHJSON *aJson)
 

Detailed Description

Helper class for manipulating JSON data.

Use Put() methods to add entries and Get*() to fetch entries.

Member Enumeration Documentation

enum to describe the different data which can be embedded in a JSON.

Enumerator
jsonFalse 

False value.

jsonTrue 

True value.

jsonNULL 

NULL JSON.

jsonNumber 

A number value.

jsonString 

A string value.

jsonArray 

An array value.

jsonObject 

A JSON object value.

Constructor & Destructor Documentation

CotCHelpers::CHJSON::CHJSON ( )

Default constructor, builds an empty JSON.

CotCHelpers::CHJSON::~CHJSON ( )

Destructor.

CotCHelpers::CHJSON::CHJSON ( bool  aBoolean)

Constructor with a boolean. Will create a JSON of type jsonFalse or jsonTrue depending on the value passed as parameter.

Parameters
aBooleanis the boolean to be embedded in the JSON created.
CotCHelpers::CHJSON::CHJSON ( double  aNumber)

Constructor with a number. Will create a JSON of type jsonNumber.

Parameters
aNumberis the number held in the JSON created.
CotCHelpers::CHJSON::CHJSON ( const char *  aString)

Constructor with a string. Will create a JSON of type jsonString.

Parameters
aStringis the string held in the JSON created.

Member Function Documentation

void CotCHelpers::CHJSON::Add ( CHJSON aJson)

Method to add a new JSON in an array.

Parameters
aJsonis the JSON to be added to the array. The item will be automatically destroyed after this call.
void CotCHelpers::CHJSON::Add ( const char *  aItem,
CHJSON aJson 
)
protected

Method to add a JSON inside another JSON of type jsonObject.

Parameters
aItemis the key of the item inside the JSON which will receive the incoming JSON.
aJsonis the JSON to insert in the existing JSON. The item will be automatically destroyed after this call.
void CotCHelpers::CHJSON::Add ( const char *  aItem,
double  aNumber 
)
inlineprotected

Helper method to add a pair key/ double value.

Parameters
aItemis the key of the value you want to add.
aNumberis the number to add, associated with the key.
void CotCHelpers::CHJSON::Add ( const char *  aItem,
int  aNumber 
)
inlineprotected

Helper method to add a pair key/ int value.

Parameters
aItemis the key of the value you want to add.
aNumberis the number to add, associated with the key.
void CotCHelpers::CHJSON::Add ( const char *  aItem,
bool  aFlag 
)
inlineprotected

Helper method to add a pair key/ boolean value.

Parameters
aItemis the key of the value you want to add.
aFlagis the boolean to add, associated with the key.
void CotCHelpers::CHJSON::AddOrReplaceStringSafe ( const char *  aItem,
const char *  aValue 
)
protected

Method which adds or replace a pair key/value inside a JSON object of type jsonObject.

Parameters
aItemis the key of the value you want to add.
aValueis the string you want to add. If NULL, an empty string will be passed.
void CotCHelpers::CHJSON::AddStringSafe ( const char *  aItem,
const char *  aValue 
)
protected

Method which adds a pair key/value inside a JSON object of type jsonObject.

Parameters
aItemis the key of the value you want to add.
aValueis the string you want to add. If NULL, an empty string will be passed.
static CHJSON* CotCHelpers::CHJSON::Array ( )
static

Static function to create a JSON as an empty array. Will create a JSON of type jsonArray.

Returns
is the JSON object created. It must be deleted by you.
static CHJSON* CotCHelpers::CHJSON::Array ( int *  aNumbers,
int  aCount 
)
static

Static function to create a JSON as an array of integers. Will create a JSON of type jsonArray.

Parameters
aNumbersis the array of integers to transfer in the JSON array.
aCountis the number of integers in the parameter aNumbers.
Returns
is the JSON object created. It must be deleted by you.
static CHJSON* CotCHelpers::CHJSON::Array ( float *  aNumbers,
int  aCount 
)
static

Static function to create a JSON as an array of floats. Will create a JSON of type jsonArray.

Parameters
aNumbersis the array of floats to transfer in the JSON array.
aCountis the number of floats in the parameter aNumbers.
Returns
is the JSON object created. It must be deleted by you.
static CHJSON* CotCHelpers::CHJSON::Array ( double *  aNumbers,
int  aCount 
)
static

Static function to create a JSON as an array of doubles. Will create a JSON of type jsonArray.

Parameters
aNumbersis the array of doubles to transfer in the JSON array.
aCountis the number of doubles in the parameter aNumbers.
Returns
is the JSON object created. It must be deleted by you.
static CHJSON* CotCHelpers::CHJSON::Array ( const char **  aStrings,
int  aCount 
)
static

Static function to create a JSON as an array of strings. Will create a JSON of type jsonArray.

Parameters
aStringsis the array of strings to transfer in the JSON array.
aCountis the number of strings in the parameter aNumbers.
Returns
is the JSON object created. It must be deleted by you.
Iterator CotCHelpers::CHJSON::begin ( ) const

Allow for iterating the nodes inside an object or an array.

Returns
an iterator to start with
void CotCHelpers::CHJSON::Clear ( )

Clears all elements.

static CHJSON* CotCHelpers::CHJSON::createNull ( void  )
static

Static function to create an empty JSON object of type jsonNULL.

Returns
is the JSON object created. It must be deleted by you.
void CotCHelpers::CHJSON::Delete ( const char *  aItem)

Method to remove a pair key/value from a JSON object of type jsonObject.

Parameters
aItemis the key of the pair key/value you want to remove from the dictionary.
static CHJSON* CotCHelpers::CHJSON::dup ( const CHJSON aJson)
staticprotected

Static function to duplicate an existing JSON object.

Parameters
aJsonis the JSON object to be duplicated.
Returns
is a copy of the original JSON object. It must be deleted by you.
CHJSON* CotCHelpers::CHJSON::Duplicate ( ) const

Same as CHJSON::dup(json) but on this member.

Returns
is a copy of this JSON object. It must be deleted by you.
static const CHJSON* CotCHelpers::CHJSON::Empty ( )
static

Returns an empty JSON.

Iterator CotCHelpers::CHJSON::end ( ) const

Allow for iterating the nodes inside an object or an array.

Returns
the ending iterator
const CHJSON* CotCHelpers::CHJSON::Get ( int  aIndex) const

Method to return a line from a JSON, of type jsonArray or jsonObject.

Parameters
aIndexis the index in the array or dictionary, beginning with 0.
Returns
is the JSON, or NULL if out of bounds or not a jsonArray/jsonObject.
const CHJSON* CotCHelpers::CHJSON::Get ( const char *  aItem) const

Method to retrieve a JSON inside another JSON.

Parameters
aItemis the key associated to the JSON to be retrieved.
Returns
is the JSON retrieved. It's a NULL pointer if there is no such key. Do NOT delete it.
bool CotCHelpers::CHJSON::GetBool ( const char *  aItem,
bool  defaultValue = false 
) const

Helper method to retrieve a boolean value, given a key.

Parameters
aItemis the key of the value you want to retrieve.
defaultValuethe default value to return if the key is absent
Returns
is the value retrieved.
double CotCHelpers::CHJSON::GetDouble ( const char *  aItem,
double  defaultValue = 0 
) const

Helper method to retrieve a double value, given a key.

Parameters
aItemis the key of the value you want to retrieve.
defaultValuethe default value to return if the key is absent
Returns
is the value retrieved.
int CotCHelpers::CHJSON::GetInt ( const char *  aItem,
int  defaultValue = 0 
) const

Helper method to retrieve an int value, given a key.

Parameters
aItemis the key of the value you want to retrieve.
defaultValuethe default value to return if the key is absent
Returns
is the value retrieved.
const CHJSON* CotCHelpers::CHJSON::GetSafe ( int  aIndex) const
inline

Preferred over Get. Never returns NULL but an empty const node instead. Using this method, it is safe to call result->GetSafe("result")->GetString("gamer_id"), without checking subnodes.

Parameters
aIndexis the index in the array or dictionary, beginning with 0.
Returns
a JSON node, possibly empty.
const CHJSON* CotCHelpers::CHJSON::GetSafe ( const char *  aItem) const
inline

Preferred over Get. Never returns NULL but an empty const node instead. Using this method, it is safe to call result->GetSafe("result")->GetString("gamer_id"), without checking subnodes.

Parameters
aItemis the key associated to the JSON to be retrieved.
Returns
a JSON node, possibly empty.
const char* CotCHelpers::CHJSON::GetString ( const char *  key,
const char *  defaultValue = 0 
) const
Returns
the string corresponding to the key, or NULL if the key doesn't exist / is not a string
bool CotCHelpers::CHJSON::Has ( const char *  key) const
Parameters
keykey to test for
Returns
whether the JSON contains a value for key (regardless of the value type inside)
static CHJSON* CotCHelpers::CHJSON::initWith ( const char **  aArgs)
staticprotected

Function to create a JSON object of type jsonObject, as a dictionary of pairs key/value, all values being strings.

Parameters
aArgsis an array of C strings following the format key1, value1, key2, value2, ..., keyN, valueN, NULL
Returns
is the JSON created with the values passed. It has to be deleted.
const char* CotCHelpers::CHJSON::name ( ) const

Method which returns the key of a pair key/value when you retrieved a JSON object with the get(int) method.

Returns
is the C string holding the value of the key. Do not delete.
static CHJSON* CotCHelpers::CHJSON::parse ( const char *  aJsonString)
static

Function which creates a JSON object given a properly constructed string representing a JSON.

Parameters
aJsonStringis the string describing the JSON you want to create.
Returns
is the JSON object, which you must delete.
char* CotCHelpers::CHJSON::print ( ) const

Method which prints the content of the JSON object in a C string, in a single line.

Returns
is a C string which must be released using free().
char* CotCHelpers::CHJSON::printFormatted ( ) const

Method which prints the content of the JSON object in a C string, in a formatted and indented way.

Returns
is a C string which must be released using free().
void CotCHelpers::CHJSON::Put ( const char *  aKey,
bool  aValue 
)

Used to add, or replace if the key already exists, a boolean value.

Parameters
aKeyis the key to add or replace.
aValueis the boolean value used.
void CotCHelpers::CHJSON::Put ( const char *  aKey,
int  aValue 
)

Used to add, or replace if the key already exists, an int value.

Parameters
aKeyis the key to add or replace.
aValueis the int value used.
void CotCHelpers::CHJSON::Put ( const char *  aKey,
double  aValue 
)

Used to add, or replace if the key already exists, a double value.

Parameters
aKeyis the key to add or replace.
aValueis the double value used.
void CotCHelpers::CHJSON::Put ( const char *  aKey,
const char *  aValue 
)

Used to add, or replace if the key already exists, a string value.

Parameters
aKeyis the key to add or replace.
aValueis the string value used.
void CotCHelpers::CHJSON::Put ( const char *  aKey,
CHJSON json 
)

Adds or replace a JSON. WARNING: trashes the original (passed) JSON. You must not delete nor use it afterwards.

Parameters
aKeyis the key to add or replace.
jsonis the value to put inside (taken ownership of)
void CotCHelpers::CHJSON::Put ( const char *  aKey,
const CHJSON json 
)
inline

Adds a JSON, keeping it intact after the call. Meaning that unlike the standard method the JSON will be duplicated and not owned.

Parameters
aKeyis the key to add or replace.
jsonis the value to put inside (copy)
void CotCHelpers::CHJSON::Put ( const char *  aKey,
const CHJSON json 
)
inline

Same as the previous method, but with a local object passed like by value.

Parameters
aKeyis the key to add or replace.
jsonis the value to put inside (copy)
void CotCHelpers::CHJSON::Replace ( const char *  aItem,
CHJSON aJson 
)
protected

Method to replace a JSON inside a JSON, by another JSON.

Parameters
aItemis the key associated to the JSON to be replaced.
aJsonis the new JSON, which replaces the old one.
int CotCHelpers::CHJSON::size ( ) const

Methods which gives the number of elements inside the JSON.

Returns
is the number of elements. If not jsonArray or jsonObject, will always be 0.
jsonType CotCHelpers::CHJSON::type ( ) const

Method which returns the type of the JSON object.

Returns
is the type, see enum jsonType.
double CotCHelpers::CHJSON::valueDouble ( ) const

Method which returns a double if the JSON object is of type jsonNumber.

Returns
is the value, will be 0 if type is not jsonNumber.
int CotCHelpers::CHJSON::valueInt ( ) const

Method which returns an int if the JSON object is of type jsonNumber.

Returns
is the value, will be 0 if type is not jsonNumber.
const char* CotCHelpers::CHJSON::valueString ( ) const

Method which returns a C string if the JSON object is of type jsonString.

Returns
is the C string, will be NULL if type is not jsonString.

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