Unigine::Json Class
Header: | #include <UnigineJson.h> |
This class is used to parse and create JSON formatted strings.
Each Json node has type, name, and value. Also, it can have child Json nodes and a parent node.
Json node can have one of the following types:
- null
- bool
- number
- string
- array
- object
The Json node has a hierarchy structure. The following code creates a Json node and adds child nodes with different types of values:
#include <UnigineJson.h>
/* ... */
JsonPtr json = Json::create();
json->addChild("child_0",0); // int
json->addChild("child_1",1.1); // float
json->addChild("child_2","two"); // string
The result is:
{
"child_0": 0,
"child_1": 1.1,
"child_2": "two"
}
Child nodes have names "child_0", "child_1", "child_2" and values: integer 0, double 1.1 and string "two". The name has always string type.
Json Class
Members
static JsonPtr create ( ) #
Default constructor that creates an empty instance.static JsonPtr create ( const char * name ) #
Constructor that creates a JSON node with a given name.Arguments
- const char * name - Name of the Json node.
void setArray ( ) #
Sets array type to the current Json node.int isArray ( ) const#
Returns a value indicating if the Json node has an array type.Return value
1 if the Json has an array type; otherwise, 0.void setBool ( int arg1 ) #
Sets a boolean value and type to the current Json node.Arguments
- int arg1 - Integer value.
int getBool ( ) const#
Reads a boolean value of the current Json node.Return value
1 if the Json node has a bool type; otherwise, 0.int isBool ( ) const#
Returns a value indicating if the Json node has a bool type.Return value
1 if the Json has a bool type; otherwise, 0.Ptr<Json> getChild ( int num ) const#
Returns the child node of the current Json node.Arguments
- int num - Argument of one of the following types:
- string name - Name of the Json node.
- int number - Number of the child of the Json node.
Return value
Child Json node.Ptr<Json> getChild ( const char * name ) const#
Returns the child node of the current Json node.Arguments
- const char * name - Argument of one of the following types:
- string name - Name of the Json node.
- int number - Number of the child of the Json node.
Return value
Child Json node.int isChild ( const char * name ) const#
Checks if a child node with a given name exists.Arguments
- const char * name - Name of the child node.
Return value
1 if a child with the provided name exists; otherwise, 0.String getFormattedSubTree ( const char * name = 0 ) #
Returns a subtree of a Json node as the formatted string.Arguments
- const char * name - The name of a child node. If there is a name, the function returns formatted subtree for this child Json node.
Return value
Formatted subtree.void setName ( const char * name ) #
Sets the given name to the Json node.Arguments
- const char * name - Name of the Json node.
const char * getName ( ) const#
Returns the name of the current Json node.Return value
The name of the Json node.void setNull ( ) #
Sets null type to the current Json node.int isNull ( ) const#
Returns a value indicating if the Json node has a null type.Return value
1 if the Json has a null type; otherwise, 0.void setNumber ( double arg1 ) #
Sets a number value and type to the current Json node.Arguments
- double arg1 - Double value.
void setNumber ( int arg1 ) #
Sets a number value and type to the current Json node.Arguments
- int arg1 - Integer value.
double getNumber ( ) const#
Returns the number value of the current Json node.Return value
Number value of the current Json node.int isNumber ( ) const#
Returns a value indicating if the Json node has a number type.Return value
1 if the Json has a number type; otherwise, 0.int getNumChildren ( ) const#
Returns the number of child nodes of the current Json node.Return value
Number of child nodes.void setObject ( ) #
Sets object type to the current Json node.int isObject ( ) const#
Returns a value indicating if the Json node has an object type.Return value
1 if the Json has an object type; otherwise, 0.Ptr<Json> getParent ( ) #
Returns the parent node of the current Json node.Return value
Parent Json node.void setString ( const char * arg1 ) #
Sets a string value and type to the current Json node. The function automatically casts number values to string type.Arguments
- const char * arg1 - String value.
String getString ( ) #
Returns the value of the current Json node as string.Return value
Value of the current Json nodeint isString ( ) const#
Returns a value indicating if the Json node has a string type.Return value
1 if the Json has a string type; otherwise, 0.String getSubTree ( const char * name = 0 ) #
Returns a subtree of a Json node as the non-formatted string.Arguments
- const char * name - The name of a child node. If there is a name, the function returns formatted subtree for this child Json node.
Return value
Non-formatted subtree.Ptr<Json> addChild ( const char * name, double value, double value ) #
Adds a new name-value pair as a child node to the current Json node.Arguments
- const char * name - Node name.
- double value - Argument of one of the following types:
- int value - integer value.
- float value - float value.
- string value - string value.
- double value - Argument of one of the following types:
- int value - integer value.
- float value - float value.
- string value - string value.
Return value
Child Json node.Ptr<Json> addChild ( const char * name ) #
Adds a new name-value pair as a child node to the current Json node.Arguments
- const char * name - Node name.
Return value
Child Json node.Ptr<Json> addChild ( const char * name, const char * value ) #
Adds a new name-value pair as a child node to the current Json node.Arguments
- const char * name - Node name.
- const char * value - Argument of one of the following types:
- int value - integer value.
- float value - float value.
- string value - string value.
Return value
Child Json node.Ptr<Json> addChild ( const Ptr<Json> & json ) #
Adds a new name-value pair as a child node to the current Json node.Arguments
- const Ptr<Json> & json - Node name.
Return value
Child Json node.Ptr<Json> addChild ( const char * name, int value ) #
Adds a new name-value pair as a child node to the current Json node.Arguments
- const char * name - Node name.
- int value - Argument of one of the following types:
- int value - integer value.
- float value - float value.
- string value - string value.
Return value
Child Json node.void clear ( ) #
Clears all data of the current Json node including type, value, name and all children. If the current Json node has a parent, it also removed from the parent Json node.void clearChildren ( ) #
Clears all children of the current Json node.void copy ( const Ptr<Json> & source ) #
Copies type, name and value from the source Json node to the current Json node and adds the source Json child nodes as child nodes to the current Json node.Arguments
- const Ptr<Json> & source - Source Json node.
Ptr<Json> find ( const char * name ) #
Finds Json node by its name in current Json node tree.Arguments
- const char * name - Name of the Json node.
Return value
Founded Json node.int load ( const char * path ) #
Loads the data to the current Json node from the file with a given path.Arguments
- const char * path - Path of the file.
Return value
1 if the Json node was loaded successfully; otherwise, 0.int parse ( const char * source ) #
Parses a given string into the Json node.Usage Example
#include <UnigineJson.h>
/* ... */
JsonPtr json = Json::create();
json->addChild("child_0", 1);
JsonPtr json_2 = Json::create();
json_2->parse(json->getSubTree().get());
Now the json_2 node contains:
{
"child_0": 1
}
Arguments
- const char * source - String to parse.
Return value
1 if the string was parsed successfully; otherwise, 0.Ptr<Json> removeChild ( const Ptr<Json> & json ) #
Removes the child Json node.Arguments
- const Ptr<Json> & json - Argument of one of the following types:
- Json json - Json node.
- string name - Name of the Json node.
Return value
Removed child Json node.Ptr<Json> removeChild ( const char * name ) #
Removes the child Json node.Arguments
- const char * name - Argument of one of the following types:
- Json json - Json node.
- string name - Name of the Json node.
Return value
Removed child Json node.int save ( const char * path ) const#
Saves the Json node into a file with a given path.Arguments
- const char * path - Path to the file.
Return value
1 if the file was saved successfully; otherwise, 0.void read ( const char * name, bool & value ) const#
Reads a boolean value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- bool & value - Target boolean variable to which the value of the Json node with the specified name.
void read ( const char * name, int & value ) const#
Reads an integer boolean value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- int & value - Target integer variable to which the value of the Json node with the specified name.
void read ( const char * name, unsigned int & value ) const#
Reads an unsigned integer value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- unsigned int & value - Target unsigned integer variable to which the value of the Json node with the specified name.
void read ( const char * name, char & value ) const#
Reads a character value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- char & value - Target character variable to which the value of the Json node with the specified name.
void read ( const char * name, double & value ) const#
Reads a double value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- double & value - Target double variable to which the value of the Json node with the specified name.
void read ( const char * name, float & value ) const#
Reads a float value of a Json node with the specified name to the specified target variable.Arguments
- const char * name - Name of the Json node.
- float & value - Target float variable to which the value of the Json node with the specified name.
void write ( const char * name, bool value ) #
Writes the specified boolean value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- bool value - Boolean value to be written to the Json node with the specified name.
void write ( const char * name, int value ) #
Writes the specified integer value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- int value - Integer value to be written to the Json node with the specified name.
void write ( const char * name, unsigned int value ) #
Writes the specified unsigned integer value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- unsigned int value - Unsigned integer value to be written to the Json node with the specified name.
void write ( const char * name, char value ) #
Writes the specified character value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- char value - Character value to be written to the Json node with the specified name.
void write ( const char * name, double value ) #
Writes the specified double value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- double value - Double value to be written to the Json node with the specified name.
void write ( const char * name, float value ) #
Writes the specified float value to the target Json node with the specified name. In case such node is not found, a new one is added with the name specified.Arguments
- const char * name - Name of the target Json node.
- float value - Float value to be written to the Json node with the specified name.
Math::vec2 getVec2 ( ) const#
Returns a two-component vector composed of the first two elements of the array-type Json node. If the node is not an array a zero-vector (0.0f, 0.0f) shall be returned.Return value
Vector composed of the first two elements of the array-type Json nodeMath::vec3 getVec3 ( ) const#
Returns a three-component vector composed of the first three elements of the array-type Json node. If the node is not an array a zero-vector (0.0f, 0.0f, 0.0f) shall be returned.Return value
Vector composed of the first three elements of the array-type Json nodeMath::vec4 getVec4 ( ) const#
Returns a four-component vector composed of the first four elements of the array-type Json node. If the node is not an array a zero-vector (0.0f, 0.0f, 0.0f, 0.0f) shall be returned.Return value
Vector composed of the first four elements of the array-type Json nodeconst char * getTypeName ( ) const#
Returns the name of the type of the Json node.Return value
Name of the Json node type.Last update:
2021-10-25
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)