CUGL
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cugl::JsonValue Class Reference

#include <CUJsonValue.h>

Public Types

enum  Type : int {
  Type::NullType = 0, Type::BoolType = 1, Type::NumberType = 2, Type::StringType = 3,
  Type::ArrayType = 4, Type::ObjectType = 5
}
 

Public Member Functions

 JsonValue ()
 
 ~JsonValue ()
 
bool init (Type type)
 
bool init (const std::string &value)
 
bool init (const char *value)
 
bool init (bool value)
 
bool init (long value)
 
bool init (double value)
 
bool initNull ()
 
bool initArray ()
 
bool initObject ()
 
bool initWithJson (const std::string &json)
 
bool initWithJson (const char *json)
 
Type type () const
 
bool isNull () const
 
bool isNumber () const
 
bool isBool () const
 
bool isString () const
 
bool isValue () const
 
bool isArray () const
 
bool isObject () const
 
const std::string asString (const std::string &defaultValue) const
 
const std::string asString (const char *defaultValue="") const
 
float asFloat (float defaultValue=0.0f) const
 
double asDouble (double defaultValue=0.0) const
 
long asLong (long defaultValue=0L) const
 
int asInt (int defaultValue=0) const
 
bool asBool (bool defaultValue=false) const
 
std::vector< std::string > asStringArray (const std::string &defaultValue) const
 
std::vector< std::string > asStringArray (const char *defaultValue="") const
 
std::vector< float > asFloatArray (float defaultValue=0.0f) const
 
std::vector< double > asDoubleArray (double defaultValue=0.0) const
 
std::vector< long > asLongArray (long defaultValue=0L) const
 
std::vector< int > asIntArray (int defaultValue=0) const
 
std::vector< bool > asBoolArray (bool defaultValue=false)
 
void set (const std::string &value)
 
void set (const char *value)
 
void set (long value)
 
void set (double value)
 
void set (bool value)
 
void setNull ()
 
const std::string & key () const
 
void setKey (const std::string &key)
 
void setKey (const char *key)
 
const int index () const
 
size_t size () const
 
bool has (const std::string &name) const
 
bool has (const char *name) const
 
std::shared_ptr< JsonValueget (int index)
 
const std::shared_ptr< JsonValueget (int index) const
 
std::shared_ptr< JsonValueget (const std::string &name)
 
const std::shared_ptr< JsonValueget (const std::string &name) const
 
std::shared_ptr< JsonValueget (const char *name)
 
const std::shared_ptr< JsonValueget (const char *name) const
 
const std::string getString (const std::string &key, const std::string &defaultValue) const
 
const std::string getString (const char *key, const char *defaultValue="") const
 
const std::string getString (const std::string &key, const char *defaultValue="") const
 
const std::string getString (const char *key, const std::string &defaultValue) const
 
float getFloat (const std::string &key, float defaultValue=0.0f) const
 
float getFloat (const char *key, float defaultValue=0.0f) const
 
double getDouble (const std::string &key, double defaultValue=0.0) const
 
double getDouble (const char *key, double defaultValue=0.0) const
 
long getLong (const std::string &key, long defaultValue=0L) const
 
long getLong (const char *key, long defaultValue=0L) const
 
int getInt (const std::string &key, int defaultValue=0) const
 
int getInt (const char *key, int defaultValue=0) const
 
bool getBool (const std::string &key, bool defaultValue=false) const
 
bool getBool (const char *key, bool defaultValue=false) const
 
std::shared_ptr< JsonValueremoveChild (int index)
 
std::shared_ptr< JsonValueremoveChild (const std::string &name)
 
std::shared_ptr< JsonValueremoveChild (const char *name)
 
void appendChild (const std::shared_ptr< JsonValue > &child)
 
void appendChild (const std::string &key, const std::shared_ptr< JsonValue > &child)
 
void appendChild (const char *key, const std::shared_ptr< JsonValue > &child)
 
void insertChild (unsigned int index, const std::shared_ptr< JsonValue > &child)
 
void insertChild (unsigned int index, const std::string &key, const std::shared_ptr< JsonValue > &child)
 
void insertChild (unsigned int index, const char *key, const std::shared_ptr< JsonValue > &child)
 
void appendValue (bool value)
 
void appendValue (const std::string &key, bool value)
 
void appendValue (const char *key, bool value)
 
void insertValue (unsigned int index, bool value)
 
void insertValue (unsigned int index, const std::string &key, bool value)
 
void insertValue (unsigned int index, const char *key, bool value)
 
void appendValue (long value)
 
void appendValue (const std::string &key, long value)
 
void appendValue (const char *key, long value)
 
void insertValue (unsigned int index, long value)
 
void insertValue (unsigned int index, const std::string &key, long value)
 
void insertValue (unsigned int index, const char *key, long value)
 
void appendValue (double value)
 
void appendValue (const std::string &key, double value)
 
void appendValue (const char *key, double value)
 
void insertValue (unsigned int index, double value)
 
void insertValue (unsigned int index, const std::string &key, double value)
 
void insertValue (unsigned int index, const char *key, double value)
 
void appendValue (const std::string &value)
 
void appendValue (const char *value)
 
void appendValue (const std::string &key, const std::string &value)
 
void appendValue (const std::string &key, const char *value)
 
void appendValue (const char *key, const std::string &value)
 
void appendValue (const char *key, const char *value)
 
void insertValue (unsigned int index, const std::string &value)
 
void insertValue (unsigned int index, const char *value)
 
void insertValue (unsigned int index, const std::string &key, const std::string &value)
 
void insertValue (unsigned int index, const std::string &key, const char *value)
 
void insertValue (unsigned int index, const char *key, const std::string &value)
 
void insertValue (unsigned int index, const char *key, const char *value)
 
void appendNull ()
 
void appendNull (const std::string &key)
 
void appendNull (const char *key)
 
void insertNull (unsigned int index)
 
void insertNull (unsigned int index, const std::string &key)
 
void insertNull (unsigned int index, const char *key)
 
void appendArray ()
 
void appendArray (const std::string &key)
 
void appendArray (const char *key)
 
void insertArray (unsigned int index)
 
void insertArray (unsigned int index, const std::string &key)
 
void insertArray (unsigned int index, const char *key)
 
void appendObject ()
 
void appendObject (const std::string &key)
 
void appendObject (const char *key)
 
void insertObject (unsigned int index)
 
void insertObject (unsigned int index, const std::string &key)
 
void insertObject (unsigned int index, const char *key)
 
std::string toString (bool format=true) const
 

Static Public Member Functions

static std::shared_ptr< JsonValuealloc (Type type)
 
static std::shared_ptr< JsonValuealloc (const std::string &value)
 
static std::shared_ptr< JsonValuealloc (const char *value)
 
static std::shared_ptr< JsonValuealloc (bool value)
 
static std::shared_ptr< JsonValuealloc (long value)
 
static std::shared_ptr< JsonValuealloc (double value)
 
static std::shared_ptr< JsonValueallocNull ()
 
static std::shared_ptr< JsonValueallocArray ()
 
static std::shared_ptr< JsonValueallocObject ()
 
static std::shared_ptr< JsonValueallocWithJson (const std::string &json)
 
static std::shared_ptr< JsonValueallocWithJson (const char *json)
 

Detailed Description

This class represents a node in a JSON DOM tree.

While each instance is a single node, there are many methods for traversing the node as a tree. A node only has a child if it is an array or an object type. All other types are leaves in the tree.

Children may be accessed by index or key regardless of whether or not the node is an array or object. However, keys are only guaranteed to be unique if the node is an object type. Hence the main usage of this feature is to "cast" object nodes to arrays.

This class uses cJSON as the underlying parsing engine. However, it manages memory automatically so that the user does not need to worry about deleting or allocating memory beyond the initial node itself.

Member Enumeration Documentation

enum cugl::JsonValue::Type : int
strong

This enum represents the possible data types in a JsonValue

We do not distinguish numeric types (int, float, long, double) since the JSON specification does not distinguish between them. Instead, we freely convert between these values on access.

Enumerator
NullType 

The JsonValue contains no value at all

BoolType 

The JsonValue contains a boolean value

NumberType 

The JsonValue contains a numeric (int, float, etc.) value

StringType 

The JsonValue contains a string value

ArrayType 

The JsonValue is an array whose contents are children of the node

ObjectType 

The JsonValue is an object whose contents are children of the node

Constructor & Destructor Documentation

cugl::JsonValue::JsonValue ( )

Creates a null JsonValue.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

cugl::JsonValue::~JsonValue ( )

Deletes this JsonValue and all of its resources.

If no other references own the descendants of this node, they will all be recursively deleted as well.

Member Function Documentation

static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( Type  type)
inlinestatic

Returns a newly allocated JsonValue of the given type.

The value of this node will be the default value of the type.

Parameters
typeThe type of the JSON node.
Returns
a newly allocated JsonValue of the given type.
static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( const std::string &  value)
inlinestatic

Returns a newly allocated JsonValue with the given string.

The node will have type StringType.

Parameters
valueThe (string) value of this JSON node.
Returns
a newly allocated JsonValue with the given string.
static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( const char *  value)
inlinestatic

Returns a newly allocated JsonValue with the given string.

The node will have type StringType.

Parameters
valueThe (string) value of this JSON node.
Returns
a newly allocated JsonValue with the given string.
static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( bool  value)
inlinestatic

Returns a newly allocated JsonValue with the given boolean.

The node will have type BoolType.

Parameters
valueThe (boolean) value of this JSON node.
Returns
a newly allocated JsonValue with the given boolean.
static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( long  value)
inlinestatic

Returns a newly allocated JsonValue with the given number.

The node will have type NumberType.

Parameters
valueThe (numeric) value of this JSON node.
Returns
a newly allocated JsonValue with the given number.
static std::shared_ptr<JsonValue> cugl::JsonValue::alloc ( double  value)
inlinestatic

Returns a newly allocated JsonValue with the given number.

The node will have type NumberType.

Parameters
valueThe (numeric) value of this JSON node.
Returns
a newly allocated JsonValue with the given number.
static std::shared_ptr<JsonValue> cugl::JsonValue::allocArray ( )
inlinestatic

Returns a newly allocated JsonValue array.

The node will have type ArrayType. The node itself will have no value. The contents of the array are the children of this node.

Returns
a newly allocated JsonValue array.
static std::shared_ptr<JsonValue> cugl::JsonValue::allocNull ( )
inlinestatic

Returns a newly allocated null JsonValue (e.g. it has no value).

The node will have type NullType.

Returns
a newly allocated null JsonValue (e.g. it has no value).
static std::shared_ptr<JsonValue> cugl::JsonValue::allocObject ( )
inlinestatic

Returns a newly allocated JsonValue object.

The node will have type ObjectType. The node itself will have no value. The contents of the object are the children of this node.

Returns
a newly allocated JsonValue object.
static std::shared_ptr<JsonValue> cugl::JsonValue::allocWithJson ( const std::string &  json)
inlinestatic

Returns a newly allocated JsonValue from the given JSON string.

This initializer will parse the JSON string and construct a full JSON tree for the string, if possible. The children are all owned by this node will be deleted when this node is deleted (provided there are no other references).

If there is a parsing error, this method will return nullptr. Detailed information about the parsing error will be passed to an assert. Hence error messages are suppressed if asserts are turned off.

Parameters
jsonThe JSON string to parse.
Returns
a newly allocated JsonValue from the given JSON string.
static std::shared_ptr<JsonValue> cugl::JsonValue::allocWithJson ( const char *  json)
inlinestatic

Returns a newly allocated JsonValue from the given JSON string.

This initializer will parse the JSON string and construct a full JSON tree for the string, if possible. The children are all owned by this node will be deleted when this node is deleted (provided there are no other references).

If there is a parsing error, this method will return nullptr. Detailed information about the parsing error will be passed to an assert. Hence error messages are suppressed if asserts are turned off.

Parameters
jsonThe JSON string to parse.
Returns
a newly allocated JsonValue from the given JSON string.
void cugl::JsonValue::appendArray ( )
inline

Allocates a new (empty) array and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

void cugl::JsonValue::appendArray ( const std::string &  key)
inline

Allocates a new (empty) array and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendArray ( const char *  key)
inline

Allocates a new (empty) array and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendChild ( const std::shared_ptr< JsonValue > &  child)

Appends the given child to the end of this array or object.

The child will be added to the next available position in the array. If it is an object, it will use the current key of this object.

This method will fail if this node is not an array or object type. If is an object type, it will fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
childThe child node to add
void cugl::JsonValue::appendChild ( const std::string &  key,
const std::shared_ptr< JsonValue > &  child 
)

Appends the given child to the end of this object.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
keyThe key to identify the child
childThe child node to add
void cugl::JsonValue::appendChild ( const char *  key,
const std::shared_ptr< JsonValue > &  child 
)
inline

Appends the given child to the end of this object.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
keyThe key to identify the child
childThe child node to add
void cugl::JsonValue::appendNull ( )
inline

Allocates a new child with no value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

void cugl::JsonValue::appendNull ( const std::string &  key)
inline

Allocates a new child with no value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendNull ( const char *  key)
inline

Allocates a new child with no value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendObject ( )
inline

Allocates a new (empty) object and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

void cugl::JsonValue::appendObject ( const std::string &  key)
inline

Allocates a new (empty) object and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendObject ( const char *  key)
inline

Allocates a new (empty) object and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
void cugl::JsonValue::appendValue ( bool  value)
inline

Allocates a new child with a boolean value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
valueThe boolean value for the child
void cugl::JsonValue::appendValue ( const std::string &  key,
bool  value 
)
inline

Allocates a new child with a boolean value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe boolean value for the child
void cugl::JsonValue::appendValue ( const char *  key,
bool  value 
)
inline

Allocates a new child with a boolean value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe boolean value for the child
void cugl::JsonValue::appendValue ( long  value)
inline

Allocates a new child with a long value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
valueThe long value for the child
void cugl::JsonValue::appendValue ( const std::string &  key,
long  value 
)
inline

Allocates a new child with a long value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe long value for the child
void cugl::JsonValue::appendValue ( const char *  key,
long  value 
)
inline

Allocates a new child with a long value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe long value for the child
void cugl::JsonValue::appendValue ( double  value)
inline

Allocates a new child with a double value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
valueThe double value for the child
void cugl::JsonValue::appendValue ( const std::string &  key,
double  value 
)
inline

Allocates a new child with a double value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe double value for the child
void cugl::JsonValue::appendValue ( const char *  key,
double  value 
)
inline

Allocates a new child with a double value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe double value for the child
void cugl::JsonValue::appendValue ( const std::string &  value)
inline

Allocates a new child with a string value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
valueThe string value for the child
void cugl::JsonValue::appendValue ( const char *  value)
inline

Allocates a new child with a string value and appends it to the end.

The child will be added to the next available position in the array.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
valueThe string value for the child
void cugl::JsonValue::appendValue ( const std::string &  key,
const std::string &  value 
)
inline

Allocates a new child with a string value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::appendValue ( const std::string &  key,
const char *  value 
)
inline

Allocates a new child with a string value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::appendValue ( const char *  key,
const std::string &  value 
)
inline

Allocates a new child with a string value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::appendValue ( const char *  key,
const char *  value 
)
inline

Allocates a new child with a string value and appends with the given key.

The child will be added to the next available position in the array. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
keyThe key to identify the child
valueThe string value for the child
bool cugl::JsonValue::asBool ( bool  defaultValue = false) const

Returns this node as a bool.

This method will fail if the node is not a value type. Otherwise, if the node is not a BoolType or NumberType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a number
Returns
this node as a bool.
std::vector<bool> cugl::JsonValue::asBoolArray ( bool  defaultValue = false)

Returns the children of this value as a vector of bools

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a bool. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a bool
Returns
the children of this value as a vector of bools
double cugl::JsonValue::asDouble ( double  defaultValue = 0.0) const

Returns this node as a double.

This method will fail if the node is not a value type. Otherwise, if the node is not a NumberType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a number
Returns
this node as a double.
std::vector<double> cugl::JsonValue::asDoubleArray ( double  defaultValue = 0.0) const

Returns the children of this value as a vector of doubles

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a double. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a double
Returns
the children of this value as a vector of doubles
float cugl::JsonValue::asFloat ( float  defaultValue = 0.0f) const

Returns this node as a float.

This method will fail if the node is not a value type. Otherwise, if the node is not a NumberType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a number
Returns
this node as a float.
std::vector<float> cugl::JsonValue::asFloatArray ( float  defaultValue = 0.0f) const

Returns the children of this value as a vector of floats

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a float. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a float
Returns
the children of this value as a vector of floats
int cugl::JsonValue::asInt ( int  defaultValue = 0) const

Returns this node as a int.

This method will fail if the node is not a value type. Otherwise, if the node is not a NumberType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a number
Returns
this node as a int.
std::vector<int> cugl::JsonValue::asIntArray ( int  defaultValue = 0) const

Returns the children of this value as a vector of ints

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to an int. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not an int
Returns
the children of this value as a vector of ints
long cugl::JsonValue::asLong ( long  defaultValue = 0L) const

Returns this node as a long.

This method will fail if the node is not a value type. Otherwise, if the node is not a NumberType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a number
Returns
this node as a long.
std::vector<long> cugl::JsonValue::asLongArray ( long  defaultValue = 0L) const

Returns the children of this value as a vector of longs

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a long. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a long
Returns
the children of this value as a vector of longs
const std::string cugl::JsonValue::asString ( const std::string &  defaultValue) const
inline

Returns this node as a string.

This method will fail if the node is not a value type. Otherwise, if the node is not a StringType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a string
Returns
this node as a string.
const std::string cugl::JsonValue::asString ( const char *  defaultValue = "") const

Returns this node as a string.

This method will fail if the node is not a value type. Otherwise, if the node is not a StringType, it will return the default value instead.

Parameters
defaultValueThe value to return if the node is not a string
Returns
this node as a string.
std::vector<std::string> cugl::JsonValue::asStringArray ( const std::string &  defaultValue) const

Returns the children of this value as a vector of strings

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a string. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a string
Returns
the children of this value as a vector of strings
std::vector<std::string> cugl::JsonValue::asStringArray ( const char *  defaultValue = "") const
inline

Returns the children of this value as a vector of strings

This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a string. If it cannot, it will use the default value instead.

Parameters
defaultValueThe value to use if a child is not a string
Returns
the children of this value as a vector of strings
std::shared_ptr<JsonValue> cugl::JsonValue::get ( int  index)

Returns the child at the specified index.

This method will fail if the node is not an array or object type. If the index is out of bounds, this method will return nullptr.

Parameters
indexThe index into the child array.
Returns
the child at the specified index.
const std::shared_ptr<JsonValue> cugl::JsonValue::get ( int  index) const

Returns the child at the specified index.

This method will fail if the node is not an array or object type. If the index is out of bounds, this method will return nullptr.

Parameters
indexThe index into the child array.
Returns
the child at the specified index.
std::shared_ptr<JsonValue> cugl::JsonValue::get ( const std::string &  name)

Returns the child with the specified key.

This method will fail if the node is not an object type. If there is no child with this key, the method returns nullptr. If the node is somehow corrupted and there is more than one child of this name, it will return the first one.

Parameters
nameThe key identifying the child.
Returns
the child with the specified key.
const std::shared_ptr<JsonValue> cugl::JsonValue::get ( const std::string &  name) const

Returns the child with the specified key.

This method will fail if the node is not an object type. If there is no child with this key, the method returns nullptr. If the node is somehow corrupted and there is more than one child of this name, it will return the first one.

Parameters
nameThe key identifying the child.
Returns
the child with the specified key.
std::shared_ptr<JsonValue> cugl::JsonValue::get ( const char *  name)
inline

Returns the child with the specified key.

This method will fail if the node is not an object type. If there is no child with this key, the method returns nullptr. If the node is somehow corrupted and there is more than one child of this name, it will return the first one.

Parameters
nameThe key identifying the child.
Returns
the child with the specified key.
const std::shared_ptr<JsonValue> cugl::JsonValue::get ( const char *  name) const
inline

Returns the child with the specified key.

This method will fail if the node is not an object type. If there is no child with this key, the method returns nullptr. If the node is somehow corrupted and there is more than one child of this name, it will return the first one.

Parameters
nameThe key identifying the child.
Returns
the child with the specified key.
bool cugl::JsonValue::getBool ( const std::string &  key,
bool  defaultValue = false 
) const

Returns the boolean value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a boolean value, it returns the default value instead.

Note this is not the same behavior as get(key).asBool(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a boolean
Returns
the boolean value of the child with the specified key.
bool cugl::JsonValue::getBool ( const char *  key,
bool  defaultValue = false 
) const
inline

Returns the boolean value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a boolean value, it returns the default value instead.

Note this is not the same behavior as get(key).asBool(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a boolean
Returns
the boolean value of the child with the specified key.
double cugl::JsonValue::getDouble ( const std::string &  key,
double  defaultValue = 0.0 
) const

Returns the double value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asDouble(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the double value of the child with the specified key.
double cugl::JsonValue::getDouble ( const char *  key,
double  defaultValue = 0.0 
) const
inline

Returns the double value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asDouble(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the double value of the child with the specified key.
float cugl::JsonValue::getFloat ( const std::string &  key,
float  defaultValue = 0.0f 
) const

Returns the float value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asFloat(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the float value of the child with the specified key.
float cugl::JsonValue::getFloat ( const char *  key,
float  defaultValue = 0.0f 
) const
inline

Returns the float value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asFloat(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the float value of the child with the specified key.
int cugl::JsonValue::getInt ( const std::string &  key,
int  defaultValue = 0 
) const

Returns the int value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asInt(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the int value of the child with the specified key.
int cugl::JsonValue::getInt ( const char *  key,
int  defaultValue = 0 
) const
inline

Returns the int value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asInt(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the int value of the child with the specified key.
long cugl::JsonValue::getLong ( const std::string &  key,
long  defaultValue = 0L 
) const

Returns the long value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asLong(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the long value of the child with the specified key.
long cugl::JsonValue::getLong ( const char *  key,
long  defaultValue = 0L 
) const
inline

Returns the long value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a numeric value, it returns the default value instead.

Note this is not the same behavior as get(key).asLong(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a number
Returns
the long value of the child with the specified key.
const std::string cugl::JsonValue::getString ( const std::string &  key,
const std::string &  defaultValue 
) const

Returns the string value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a string value, it returns the default value instead.

Note this is not the same behavior as get(key).asString(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a string
Returns
the string value of the child with the specified key.
const std::string cugl::JsonValue::getString ( const char *  key,
const char *  defaultValue = "" 
) const
inline

Returns the string value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a string value, it returns the default value instead.

Note this is not the same behavior as get(key).asString(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a string
Returns
the string value of the child with the specified key.
const std::string cugl::JsonValue::getString ( const std::string &  key,
const char *  defaultValue = "" 
) const
inline

Returns the string value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a string value, it returns the default value instead.

Note this is not the same behavior as get(key).asString(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a string
Returns
the string value of the child with the specified key.
const std::string cugl::JsonValue::getString ( const char *  key,
const std::string &  defaultValue 
) const
inline

Returns the string value of the child with the specified key.

If there is no child with the given key, or if that child cannot be represented as a string value, it returns the default value instead.

Note this is not the same behavior as get(key).asString(defaultValue), since it will not fail if the child is an array or object.

Parameters
defaultValueThe value to use if child does not exist or is not a string
Returns
the string value of the child with the specified key.
bool cugl::JsonValue::has ( const std::string &  name) const

Returns true if a child with the specified name exists.

This method will always return false if the node is not an object type

Parameters
nameThe key identifying the child
Returns
true if a child with the specified name exists.
bool cugl::JsonValue::has ( const char *  name) const
inline

Returns true if a child with the specified name exists.

This method will always return false if the node is not an object type

Parameters
nameThe key identifying the child
Returns
true if a child with the specified name exists.
const int cugl::JsonValue::index ( ) const

Returns the index for this array/object value.

This method fails if there is no parent node.

Returns
the index for this array/object value.
bool cugl::JsonValue::init ( Type  type)

Initializes a new JsonValue of the given type.

The value of this node will be the default value of the type.

Parameters
typeThe type of the JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::init ( const std::string &  value)

Initializes a new JsonValue with the given string.

The node will have type StringType.

Parameters
valueThe (string) value of this JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::init ( const char *  value)
inline

Initializes a new JsonValue with the given string.

The node will have type StringType.

Parameters
valueThe (string) value of this JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::init ( bool  value)

Initializes a new JsonValue with the given boolean.

The node will have type BoolType.

Parameters
valueThe (boolean) value of this JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::init ( long  value)

Initializes a new JsonValue with the given number.

The node will have type NumberType.

Parameters
valueThe (numeric) value of this JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::init ( double  value)

Initializes a new JsonValue with the given number.

The node will have type NumberType.

Parameters
valueThe (numeric) value of this JSON node.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::initArray ( )
inline

Initializes a new JsonValue array.

The node will have type ArrayType. The node itself will have no value. The contents of the array are the children of this node.

Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::initNull ( )
inline

Initializes a new null JsonValue (e.g. it has no value).

The node will have type NullType.

Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::initObject ( )
inline

Initializes a new JsonValue object.

The node will have type ObjectType. The node itself will have no value. The contents of the object are the children of this node.

Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::initWithJson ( const std::string &  json)
inline

Initializes a new JsonValue from the given JSON string.

This initializer will parse the JSON string and construct a full JSON tree for the string, if possible. The children are all owned by this node will be deleted when this node is deleted (provided there are no other references).

If there is a parsing error, this method will return false. Detailed information about the parsing error will be passed to an assert. Hence error messages are suppressed if asserts are turned off.

Parameters
jsonThe JSON string to parse.
Returns
true if the JSON node is initialized properly, false otherwise.
bool cugl::JsonValue::initWithJson ( const char *  json)

Initializes a new JsonValue from the given JSON string.

This initializer will parse the JSON string and construct a full JSON tree for the string, if possible. The children are all owned by this node will be deleted when this node is deleted (provided there are no other references).

If there is a parsing error, this method will return false. Detailed information about the parsing error will be passed to an assert. Hence error messages are suppressed if asserts are turned off.

Parameters
jsonThe JSON string to parse.
Returns
true if the JSON node is initialized properly, false otherwise.
void cugl::JsonValue::insertArray ( unsigned int  index)
inline

Allocates a new (empty) array and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
void cugl::JsonValue::insertArray ( unsigned int  index,
const std::string &  key 
)
inline

Allocates a new (empty) array and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertArray ( unsigned int  index,
const char *  key 
)
inline

Allocates a new (empty) array and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertChild ( unsigned int  index,
const std::shared_ptr< JsonValue > &  child 
)

Inserts the given child at the given position in this array or object.

The child will be added to the given position in the array, and all other children will be shifted to the right. If it is an object, it will use the current key of this object.

This method will fail if this node is not an array or object type. If it is an object type, it will fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
indexThe position to add the child at
childThe child node to add
void cugl::JsonValue::insertChild ( unsigned int  index,
const std::string &  key,
const std::shared_ptr< JsonValue > &  child 
)

Inserts the given child at the given position in this object.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
indexThe position to add the child at
keyThe key to identify the child
childThe child node to add
void cugl::JsonValue::insertChild ( unsigned int  index,
const char *  key,
const std::shared_ptr< JsonValue > &  child 
)
inline

Inserts the given child at the given position in this object.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

This node will acquire ownership of the child, preventing it from being deleted.

Parameters
indexThe position to add the child at
keyThe key to identify the child
childThe child node to add
void cugl::JsonValue::insertNull ( unsigned int  index)
inline

Allocates a new child with no value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
void cugl::JsonValue::insertNull ( unsigned int  index,
const std::string &  key 
)
inline

Allocates a new child with no value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertNull ( unsigned int  index,
const char *  key 
)
inline

Allocates a new child with no value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertObject ( unsigned int  index)
inline

Allocates a new (empty) object and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
void cugl::JsonValue::insertObject ( unsigned int  index,
const std::string &  key 
)
inline

Allocates a new (empty) object and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertObject ( unsigned int  index,
const char *  key 
)
inline

Allocates a new (empty) object and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
void cugl::JsonValue::insertValue ( unsigned int  index,
bool  value 
)
inline

Allocates a new child with a boolean value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
valueThe boolean value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  key,
bool  value 
)
inline

Allocates a new child with a boolean value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe boolean value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  key,
bool  value 
)
inline

Allocates a new child with a boolean value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe boolean value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
long  value 
)
inline

Allocates a new child with a long value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
valueThe long value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  key,
long  value 
)
inline

Allocates a new child with a long value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe long value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  key,
long  value 
)
inline

Allocates a new child with a long value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe long value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
double  value 
)
inline

Allocates a new child with a double value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
valueThe double value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  key,
double  value 
)
inline

Allocates a new child with a double value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe double value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  key,
double  value 
)
inline

Allocates a new child with a double value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe double value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
valueThe string value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right.

This method will fail if this node is not an array (e.g. it will even fail if it is an object, as the child will have no key).

Parameters
indexThe position to insert the child
valueThe string value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  key,
const std::string &  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const std::string &  key,
const char *  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  key,
const std::string &  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe string value for the child
void cugl::JsonValue::insertValue ( unsigned int  index,
const char *  key,
const char *  value 
)
inline

Allocates a new child with a string value and inserts it in place.

The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.

This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.

Parameters
indexThe position to insert the child
keyThe key to identify the child
valueThe string value for the child
bool cugl::JsonValue::isArray ( ) const
inline

Returns true if this node is an array.

If this method returns true, it is not safe to access the children by keys, as the keys may not be unique.

Returns
true if this node is an array.
bool cugl::JsonValue::isBool ( ) const
inline

Returns true if this node is a boolean value.

Returns
true if this node is a boolean value.
bool cugl::JsonValue::isNull ( ) const
inline

Returns true if this node has NULL type (e.g. it has no value)

Returns
true if this node has NULL type (e.g. it has no value)
bool cugl::JsonValue::isNumber ( ) const
inline

Returns true if this node is a double or long value.

Returns
true if this node is a double or long value.
bool cugl::JsonValue::isObject ( ) const
inline

Returns true if this node is an object.

If this method returns true, it is safe to access the children by either index or key.

Returns
true if this node is an array.
bool cugl::JsonValue::isString ( ) const
inline

Returns true if this node is a string value.

Returns
true if this node is a string value.
bool cugl::JsonValue::isValue ( ) const

Returns true if this node is not NULL nor an array or object.

Returns
true if this node is not NULL nor an array or object.
const std::string& cugl::JsonValue::key ( ) const

Returns the key for this object value.

This method fails if there is no parent or the parent type is not ObjectType.

Returns
the key for this object value.
std::shared_ptr<JsonValue> cugl::JsonValue::removeChild ( int  index)

Returns the child with the specified index and removes it from this node.

All other children will be shifted to the right. Returning the node allows the user to acquire ownership before it is deleted.

If the index is out of bounds, this method returns nullptr.

Parameters
indexThe index of the child to remove

Returns the child with the specified index and removes it from this node.

std::shared_ptr<JsonValue> cugl::JsonValue::removeChild ( const std::string &  name)

Returns the child with the specified key and removes it from this node.

Returning the node allows the user to acquire ownership before it is deleted.

If there is no child with this key, this method returns nullptr.

Parameters
nameThe key of the child to remove

Returns the child with the specified key and removes it from this node.

std::shared_ptr<JsonValue> cugl::JsonValue::removeChild ( const char *  name)
inline

Returns the child with the specified key and removes it from this node.

Returning the node allows the user to acquire ownership before it is deleted.

If there is no child with this key, this method returns nullptr.

Parameters
nameThe key of the child to remove

Returns the child with the specified key and removes it from this node.

void cugl::JsonValue::set ( const std::string &  value)

Sets the value of this node to the given string.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to StringType.

Parameters
valueThe string value to assign
void cugl::JsonValue::set ( const char *  value)
inline

Sets the value of this node to the given string.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to StringType.

Parameters
valueThe string value to assign (may not be null)
void cugl::JsonValue::set ( long  value)

Sets the value of this node to the given number.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to NumberType.

Parameters
valueThe numeric value to assign
void cugl::JsonValue::set ( double  value)

Sets the value of this node to the given number.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to NumberType.

Parameters
valueThe numeric value to assign
void cugl::JsonValue::set ( bool  value)

Sets the value of this node to the given boolean.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to BoolType.

Parameters
valueThe boolean value to assign
void cugl::JsonValue::setKey ( const std::string &  key)

Sets this key for this object value.

This method fails if there is no parent or the parent type is not ObjectType. It also fails if this choice of key is not unique.

Parameters
keythe key for this object value.
void cugl::JsonValue::setKey ( const char *  key)
inline

Sets this key for this object value.

This method fails if there is no parent or the parent type is not ObjectType. It also fails if this choice of key is not unique.

Parameters
keythe key for this object value.
void cugl::JsonValue::setNull ( )

Sets this node to NULL, erasing all values.

This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to NullType.

size_t cugl::JsonValue::size ( ) const
inline

Returns the number of children of this node

Returns
the number of children of this node
std::string cugl::JsonValue::toString ( bool  format = true) const

Returns a string representation of this JSON.

This method returns a proper string representation that can be written to the file. Providing this string to the allocJson constructor is guaranteed to make a duplicate of this JSON tree.

The JSON may either be pretty-printed or condensed depending on the value of format. By default, we pretty-print all JSON strings.

Parameters
formatWhether to pretty-print the JSON string
Returns
a string representation of this JSON.
Type cugl::JsonValue::type ( ) const
inline

Returns the type of this node

Returns
the type of this node

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