CUGL 2.3
Cornell University Game Library
|
#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< JsonValue > | get (int index) |
const std::shared_ptr< JsonValue > | get (int index) const |
std::shared_ptr< JsonValue > | get (const std::string name) |
const std::shared_ptr< JsonValue > | get (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< JsonValue > | removeChild (int index) |
std::shared_ptr< JsonValue > | removeChild (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< JsonValue > | toJsonValue (const cJSON *node) |
static void | toJsonValue (JsonValue *value, const cJSON *node) |
static cJSON * | toCJSON (const JsonValue *value) |
static std::shared_ptr< JsonValue > | alloc (Type type) |
static std::shared_ptr< JsonValue > | alloc (const std::string value) |
static std::shared_ptr< JsonValue > | alloc (bool value) |
static std::shared_ptr< JsonValue > | alloc (long value) |
static std::shared_ptr< JsonValue > | alloc (double value) |
static std::shared_ptr< JsonValue > | allocNull () |
static std::shared_ptr< JsonValue > | allocArray () |
static std::shared_ptr< JsonValue > | allocObject () |
static std::shared_ptr< JsonValue > | allocWithJson (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 |
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.
|
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 |
cugl::JsonValue::JsonValue | ( | ) |
Creates a null JsonValue.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
cugl::JsonValue::~JsonValue | ( | ) |
Deletes this JsonValue and all of its resources.
If no other references own the descendants of this node, they will all be recursively deleted as well.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
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.
json | The JSON string to parse. |
|
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).
|
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.
key | The key to identify the child |
void cugl::JsonValue::appendChild | ( | const std::shared_ptr< JsonValue > & | child | ) |
Appends the given child to the end of this array or object.
The child will be added to the next available position in the array. If it is an object, it will use the current key of this object.
This method will fail if this node is not an array or object type. If is an object type, it will fail if the key to the child is not unique.
This node will acquire ownership of the child, preventing it from being deleted.
child | The child node to add |
void cugl::JsonValue::appendChild | ( | const std::string | key, |
const std::shared_ptr< JsonValue > & | child | ||
) |
Appends the given child to the end of this object.
The child will be added to the next available position in the array. It will also use the provided key to identify it.
This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.
This node will acquire ownership of the child, preventing it from being deleted.
key | The key to identify the child |
child | The child node to add |
|
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).
|
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.
key | The key to identify the child |
|
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).
|
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.
key | The key to identify the child |
|
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).
value | The boolean value for the child |
|
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.
key | The key to identify the child |
value | The boolean value for the child |
|
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.
key | The key to identify the child |
value | The string value for the child |
|
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.
key | The key to identify the child |
value | The double value for the child |
|
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.
key | The key to identify the child |
value | The long value for the child |
|
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).
value | The string value for the child |
|
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).
value | The double value for the child |
|
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).
value | The long value for the child |
bool cugl::JsonValue::asBool | ( | bool | defaultValue = false | ) | const |
Returns this node as a bool.
This method will fail if the node is not a value type. Otherwise, if the node is not a BoolType or NumberType, it will return the default value instead.
defaultValue | The value to return if the node is not a number |
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.
defaultValue | The value to use if a child is not a bool |
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.
defaultValue | The value to return if the node is not a number |
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.
defaultValue | The value to use if a child is not a double |
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.
defaultValue | The value to return if the node is not a number |
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.
defaultValue | The value to use if a child is not a float |
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.
defaultValue | The value to return if the node is not a number |
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.
defaultValue | The value to use if a child is not an int |
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.
defaultValue | The value to return if the node is not a number |
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.
defaultValue | The value to use if a child is not a long |
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.
defaultValue | The value to return if the node is not a string |
std::vector< std::string > cugl::JsonValue::asStringArray | ( | const std::string | defaultValue = "" | ) | const |
Returns the children of this value as a vector of strings
This method will fail if the node is not an array or object. For each child, it will attempt to convert it to a string. If it cannot, it will use the default value instead.
defaultValue | The value to use if a child is not a string |
|
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.
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.
name | The key identifying the child. |
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.
name | The key identifying the child. |
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.
index | The index into the child array. |
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.
index | The index into the child array. |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a boolean |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a number |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a number |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a number |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a number |
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.
key | The key identifying the child. |
defaultValue | The value to use if child does not exist or is not a string |
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
name | The key identifying the child |
const int cugl::JsonValue::index | ( | ) | const |
Returns the index for this array/object value.
This method fails if there is no parent node.
bool cugl::JsonValue::init | ( | bool | value | ) |
Initializes a new JsonValue with the given boolean.
The node will have type BoolType.
value | The (boolean) value of this JSON node. |
bool cugl::JsonValue::init | ( | const std::string | value | ) |
Initializes a new JsonValue with the given string.
The node will have type StringType.
value | The (string) value of this JSON node. |
bool cugl::JsonValue::init | ( | double | value | ) |
Initializes a new JsonValue with the given number.
The node will have type NumberType.
value | The (numeric) value of this JSON node. |
bool cugl::JsonValue::init | ( | long | value | ) |
Initializes a new JsonValue with the given number.
The node will have type NumberType.
value | The (numeric) value of this JSON node. |
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.
type | The type of the JSON node. |
|
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.
|
inline |
Initializes a new null JsonValue (e.g. it has no value).
The node will have type NullType.
|
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.
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.
json | The JSON string to parse. |
|
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).
index | The position to insert the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
void cugl::JsonValue::insertChild | ( | unsigned int | index, |
const std::shared_ptr< JsonValue > & | child | ||
) |
Inserts the given child at the given position in this array or object.
The child will be added to the given position in the array, and all other children will be shifted to the right. If it is an object, it will use the current key of this object.
This method will fail if this node is not an array or object type. If it is an object type, it will fail if the key to the child is not unique.
This node will acquire ownership of the child, preventing it from being deleted.
index | The position to add the child at |
child | The child node to add |
void cugl::JsonValue::insertChild | ( | unsigned int | index, |
const std::string | key, | ||
const std::shared_ptr< JsonValue > & | child | ||
) |
Inserts the given child at the given position in this object.
The child will be added to the given position in the array, and all other children will be shifted to the right. It will also use the provided key to identify it.
This method will fail if this node is not an object type (e.g. it will even fail if the node is an array). It will also fail if the key to the child is not unique.
This node will acquire ownership of the child, preventing it from being deleted.
index | The position to add the child at |
key | The key to identify the child |
child | The child node to add |
|
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).
index | The position to insert the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
|
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).
index | The position to insert the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
|
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).
index | The position to insert the child |
value | The boolean value for the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
value | The boolean value for the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
value | The string value for the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
value | The double value for the child |
|
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.
index | The position to insert the child |
key | The key to identify the child |
value | The long value for the child |
|
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).
index | The position to insert the child |
value | The string value for the child |
|
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).
index | The position to insert the child |
value | The double value for the child |
|
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).
index | The position to insert the child |
value | The long value for the child |
|
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.
|
inline |
Returns true if this node is a boolean value.
|
inline |
Returns true if this node has NULL type (e.g. it has no value)
|
inline |
Returns true if this node is a double or long value.
|
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.
|
inline |
Returns true if this node is a string value.
bool cugl::JsonValue::isValue | ( | ) | const |
Returns true if this node is not NULL nor an array or object.
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.
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).
node | The node to substitute |
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.
name | The key of the child to remove |
Returns the child with the specified key and removes it from this node.
std::shared_ptr< JsonValue > cugl::JsonValue::removeChild | ( | 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.
index | The index of the child to remove |
Returns the child with the specified index and removes it from this node.
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.
value | The boolean value to assign |
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.
value | The string value to assign |
void cugl::JsonValue::set | ( | double | value | ) |
Sets the value of this node to the given number.
This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to NumberType.
value | The numeric value to assign |
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.
value | The numeric value to assign |
void cugl::JsonValue::setKey | ( | const std::string | key | ) |
Sets this key for this object value.
This method fails if there is no parent or the parent type is not ObjectType. It also fails if this choice of key is not unique.
key | the key for this object value. |
void cugl::JsonValue::setNull | ( | ) |
Sets this node to NULL, erasing all values.
This method will fail if the node is not a value type or NULL. Using this method will set the type of the node to NullType.
|
inline |
Returns the number of children of this node
|
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.
value | The JsonValue to convert |
|
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.
node | The cJSON node to convert |
|
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.
value | The JsonValue to store the result |
node | The cJSON node to convert |
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.
format | Whether to pretty-print the JSON string |
|
inline |
Returns the type of this node
std::vector<std::shared_ptr<JsonValue> > cugl::JsonValue::_children |
The children of this node (only non-empty if array or object)
double cugl::JsonValue::_doubleValue |
The number data stored in this node (only defined if NumberType)
std::string cugl::JsonValue::_key |
The key indexing this node with respect to its parent (maybe "")
long cugl::JsonValue::_longValue |
The number/boolean data stored in this node (only defined if BoolType/NumberType)
JsonValue* cugl::JsonValue::_parent |
A weak reference to the parent of this node (nullptr if root).
std::string cugl::JsonValue::_stringValue |
The string data stored in this node (only defined if StringType)
Type cugl::JsonValue::_type |
The type (see above) of this node