CUGL 2.3
Cornell University Game Library
Loading...
Searching...
No Matches
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 class  Type : int {
  NullType = 0 , BoolType = 1 , NumberType = 2 , StringType = 3 ,
  ArrayType = 4 , ObjectType = 5
}
 

Public Member Functions

 JsonValue ()
 
 ~JsonValue ()
 
bool init (Type type)
 
bool init (const std::string 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)
 
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
 
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< 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 (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)
 
const int index () const
 
size_t size () const
 
bool has (const std::string 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
 
const std::string getString (const std::string key, const std::string defaultValue="") const
 
float getFloat (const std::string key, float defaultValue=0.0f) const
 
double getDouble (const std::string key, double defaultValue=0.0) const
 
long getLong (const std::string key, long defaultValue=0L) const
 
int getInt (const std::string key, int defaultValue=0) const
 
bool getBool (const std::string key, bool defaultValue=false) const
 
std::shared_ptr< JsonValueremoveChild (int index)
 
std::shared_ptr< JsonValueremoveChild (const std::string name)
 
void appendChild (const std::shared_ptr< JsonValue > &child)
 
void appendChild (const std::string 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 appendValue (bool value)
 
void appendValue (const std::string key, bool value)
 
void insertValue (unsigned int index, bool value)
 
void insertValue (unsigned int index, const std::string key, bool value)
 
void appendValue (long value)
 
void appendValue (const std::string key, long value)
 
void insertValue (unsigned int index, long value)
 
void insertValue (unsigned int index, const std::string key, long value)
 
void appendValue (double value)
 
void appendValue (const std::string key, double value)
 
void insertValue (unsigned int index, double value)
 
void insertValue (unsigned int index, const std::string key, double value)
 
void appendValue (const std::string value)
 
void appendValue (const std::string key, const std::string value)
 
void insertValue (unsigned int index, const std::string value)
 
void insertValue (unsigned int index, const std::string key, const std::string value)
 
void appendNull ()
 
void appendNull (const std::string key)
 
void insertNull (unsigned int index)
 
void insertNull (unsigned int index, const std::string key)
 
void appendArray ()
 
void appendArray (const std::string key)
 
void insertArray (unsigned int index)
 
void insertArray (unsigned int index, const std::string key)
 
void appendObject ()
 
void appendObject (const std::string key)
 
void insertObject (unsigned int index)
 
void insertObject (unsigned int index, const std::string key)
 
void merge (const 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 (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)
 

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 class 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/5]

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/5]

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() [3/5]

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() [4/5]

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() [5/5]

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

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/2]

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/2]

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/2]

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() [2/2]

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/2]

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/2]

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/2]

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/2]

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/8]

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/8]

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() [3/8]

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() [4/8]

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() [5/8]

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() [6/8]

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() [7/8]

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() [8/8]

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

const std::string cugl::JsonValue::asString ( const std::string  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.

◆ asStringArray()

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/4]

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() [2/4]

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() [3/4]

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() [4/4]

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

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

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

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

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

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

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

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/5]

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/5]

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() [3/5]

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() [4/5]

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() [5/5]

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

bool cugl::JsonValue::initWithJson ( const std::string  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.

◆ insertArray() [1/2]

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/2]

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/2]

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() [2/2]

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/2]

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/2]

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/2]

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/2]

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/8]

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/8]

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() [3/8]

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() [4/8]

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() [5/8]

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() [6/8]

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() [7/8]

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() [8/8]

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 ( const 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/2]

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() [2/2]

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/4]

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/4]

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() [3/4]

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() [4/4]

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

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: