CUGL 2.1
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | 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::vector< std::shared_ptr< JsonValue > > & children ()
 
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)
 
void merge (std::shared_ptr< JsonValue > &node)
 
std::string toString (bool format=true) const
 

Static Public Member Functions

static std::shared_ptr< JsonValuetoJsonValue (const cJSON *node)
 
static void toJsonValue (JsonValue *value, const cJSON *node)
 
static cJSON * toCJSON (const JsonValue *value)
 
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)
 

Public Attributes

Type _type
 
JsonValue_parent
 
std::string _key
 
std::string _stringValue
 
long _longValue
 
double _doubleValue
 
std::vector< std::shared_ptr< JsonValue > > _children
 

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

◆ Type

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

◆ JsonValue()

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.

◆ ~JsonValue()

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

◆ alloc() [1/6]

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.

◆ alloc() [2/6]

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.

◆ alloc() [3/6]

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.

◆ alloc() [4/6]

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.

◆ alloc() [5/6]

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.

◆ alloc() [6/6]

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.

◆ allocArray()

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.

◆ allocNull()

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).

◆ allocObject()

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.

◆ allocWithJson() [1/2]

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.

◆ allocWithJson() [2/2]

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.

◆ appendArray() [1/3]

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).

◆ appendArray() [2/3]

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

◆ appendArray() [3/3]

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

◆ appendChild() [1/3]

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

◆ appendChild() [2/3]

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

◆ appendChild() [3/3]

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

◆ appendNull() [1/3]

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).

◆ appendNull() [2/3]

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

◆ appendNull() [3/3]

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

◆ appendObject() [1/3]

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).

◆ appendObject() [2/3]

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

◆ appendObject() [3/3]

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

◆ appendValue() [1/15]

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

◆ appendValue() [2/15]

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

◆ appendValue() [3/15]

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

◆ appendValue() [4/15]

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

◆ appendValue() [5/15]

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

◆ appendValue() [6/15]

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

◆ appendValue() [7/15]

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

◆ appendValue() [8/15]

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

◆ appendValue() [9/15]

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

◆ appendValue() [10/15]

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

◆ appendValue() [11/15]

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

◆ appendValue() [12/15]

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

◆ appendValue() [13/15]

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

◆ appendValue() [14/15]

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

◆ appendValue() [15/15]

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

◆ asBool()

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.

◆ asBoolArray()

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

◆ asDouble()

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.

◆ asDoubleArray()

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

◆ asFloat()

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.

◆ asFloatArray()

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

◆ asInt()

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.

◆ asIntArray()

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

◆ asLong()

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.

◆ asLongArray()

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

◆ asString() [1/2]

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.

◆ asString() [2/2]

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.

◆ asStringArray() [1/2]

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

◆ asStringArray() [2/2]

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

◆ children()

const std::vector<std::shared_ptr<JsonValue> >& cugl::JsonValue::children ( )
inline

Returns a reference to the children of this node

This value is non-empty only if this node is an array or an object.

Returns
a reference to the children of this node

◆ get() [1/6]

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.

◆ get() [2/6]

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.

◆ get() [3/6]

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.

◆ get() [4/6]

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.

◆ get() [5/6]

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.

◆ get() [6/6]

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.

◆ getBool() [1/2]

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
keyThe key identifying the child.
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.

◆ getBool() [2/2]

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
keyThe key identifying the child.
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.

◆ getDouble() [1/2]

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
keyThe key identifying the child.
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.

◆ getDouble() [2/2]

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
keyThe key identifying the child.
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.

◆ getFloat() [1/2]

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
keyThe key identifying the child.
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.

◆ getFloat() [2/2]

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
keyThe key identifying the child.
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.

◆ getInt() [1/2]

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
keyThe key identifying the child.
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.

◆ getInt() [2/2]

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
keyThe key identifying the child.
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.

◆ getLong() [1/2]

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
keyThe key identifying the child.
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.

◆ getLong() [2/2]

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
keyThe key identifying the child.
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.

◆ getString() [1/4]

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
keyThe key identifying the child.
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.

◆ getString() [2/4]

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
keyThe key identifying the child.
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.

◆ getString() [3/4]

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
keyThe key identifying the child.
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.

◆ getString() [4/4]

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
keyThe key identifying the child.
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.

◆ has() [1/2]

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.

◆ has() [2/2]

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.

◆ index()

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.

◆ init() [1/6]

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.

◆ init() [2/6]

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.

◆ init() [3/6]

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.

◆ init() [4/6]

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.

◆ init() [5/6]

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.

◆ init() [6/6]

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.

◆ initArray()

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.

◆ initNull()

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.

◆ initObject()

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.

◆ initWithJson() [1/2]

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.

◆ initWithJson() [2/2]

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.

◆ insertArray() [1/3]

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

◆ insertArray() [2/3]

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

◆ insertArray() [3/3]

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

◆ insertChild() [1/3]

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

◆ insertChild() [2/3]

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

◆ insertChild() [3/3]

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

◆ insertNull() [1/3]

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

◆ insertNull() [2/3]

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

◆ insertNull() [3/3]

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

◆ insertObject() [1/3]

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

◆ insertObject() [2/3]

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

◆ insertObject() [3/3]

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

◆ insertValue() [1/15]

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

◆ insertValue() [2/15]

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

◆ insertValue() [3/15]

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

◆ insertValue() [4/15]

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

◆ insertValue() [5/15]

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

◆ insertValue() [6/15]

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

◆ insertValue() [7/15]

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

◆ insertValue() [8/15]

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

◆ insertValue() [9/15]

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

◆ insertValue() [10/15]

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

◆ insertValue() [11/15]

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

◆ insertValue() [12/15]

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

◆ insertValue() [13/15]

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

◆ insertValue() [14/15]

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

◆ insertValue() [15/15]

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

◆ isArray()

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.

◆ isBool()

bool cugl::JsonValue::isBool ( ) const
inline

Returns true if this node is a boolean value.

Returns
true if this node is a boolean value.

◆ isNull()

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)

◆ isNumber()

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.

◆ isObject()

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.

◆ isString()

bool cugl::JsonValue::isString ( ) const
inline

Returns true if this node is a string value.

Returns
true if this node is a string value.

◆ isValue()

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.

◆ key()

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.

◆ merge()

void cugl::JsonValue::merge ( std::shared_ptr< JsonValue > &  node)

Replaces this node with the contents of the given node.

This method is used by WidgetLoader for variable replacement. It requires that this node be part of a large JSON tree (i.e. it cannot be the root).

Parameters
nodeThe node to substitute

◆ removeChild() [1/3]

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.

◆ removeChild() [2/3]

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.

◆ removeChild() [3/3]

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.

◆ set() [1/5]

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

◆ set() [2/5]

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)

◆ set() [3/5]

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

◆ set() [4/5]

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

◆ set() [5/5]

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

◆ setKey() [1/2]

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.

◆ setKey() [2/2]

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.

◆ setNull()

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()

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

Returns the number of children of this node

Returns
the number of children of this node

◆ toCJSON()

static cJSON* cugl::JsonValue::toCJSON ( const JsonValue value)
static

Returns a newly allocated cJSON node equivalent to value

This method recursively allocates child nodes as necessary. These nodes will be owned by the parent node and deleted when it is deleted. However, the returned cJSON node is not stored in a smart pointer, so it must be manually deleted (with cJSON_Delete) when it is no longer necessary.

Parameters
valueThe JsonValue to convert

◆ toJsonValue() [1/2]

static std::shared_ptr<JsonValue> cugl::JsonValue::toJsonValue ( const cJSON *  node)
static

Returns a newly allocated JsonValue equivalent to the cJSON node

This allocator recursively allocates child nodes as necessary. These nodes will be owned by the parent node and deleted when it is deleted (provided there are no other references).

This method does not delete the cJSON node when done.

Parameters
nodeThe cJSON node to convert
Returns
a newly allocated JsonValue equivalent to the cJSON node

◆ toJsonValue() [2/2]

static void cugl::JsonValue::toJsonValue ( JsonValue value,
const cJSON *  node 
)
static

Modifies value so that it is equivalent to the cJSON node

This allocator recursively allocates child nodes as necessary. These nodes will be owned by the parent node value and deleted when it is deleted (provided there are no other references).

This method does not delete the cJSON node when done.

Parameters
valueThe JsonValue to store the result
nodeThe cJSON node to convert

◆ toString()

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 allocWithJson 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()

Type cugl::JsonValue::type ( ) const
inline

Returns the type of this node

Returns
the type of this node

Member Data Documentation

◆ _children

std::vector<std::shared_ptr<JsonValue> > cugl::JsonValue::_children

The children of this node (only non-empty if array or object)

◆ _doubleValue

double cugl::JsonValue::_doubleValue

The number data stored in this node (only defined if NumberType)

◆ _key

std::string cugl::JsonValue::_key

The key indexing this node with respect to its parent (maybe "")

◆ _longValue

long cugl::JsonValue::_longValue

The number/boolean data stored in this node (only defined if BoolType/NumberType)

◆ _parent

JsonValue* cugl::JsonValue::_parent

A weak reference to the parent of this node (nullptr if root).

◆ _stringValue

std::string cugl::JsonValue::_stringValue

The string data stored in this node (only defined if StringType)

◆ _type

Type cugl::JsonValue::_type

The type (see above) of this node


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