This page has been translated automatically.
Programming
Fundamentials
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Engine Classes
Node-Related Classes
Rendering-Related Classes
Physics-Related Classes
Bounds-Related Classes
GUI-Related Classes
Controls-Related Classes
Pathfinding-Related Classes
Utility Classes
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Json Class

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 type of values:

Source code(UnigineScript)
Json json = new Json();
json.addChild("child_0",0);    // int
json.addChild("child_1",1.1);   // float
json.addChild("child_2","two");   // string

The result is:

Output
{
  "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


Json ()

Default constructor that creates an empty instance.

Json (const char * name)

Constructor that creates a JSON node with a given name.

Arguments

  • const char * name - Name of the Json node.

int getNumChildren ()

Returns the number of child nodes of the current Json node.

Return value

Number of child nodes.

void setName (const char * name)

Sets the given name to the Json node.

Arguments

  • const char * name - Name of the Json node.

int getBool ()

Reads a boolean value of the current Json node.

Return value

1 if the Json node has a bool type; otherwise, 0.

int isNull ()

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 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 - Argument of one of the following types:
    • stringvalue - string value.
    • intnumber - integer value.
    • doublenumber - double value.
    • floatnumber - float value.

void setNumber (double arg1)

Sets a number value and type to the current Json node.

Arguments

  • double arg1 - Argument of one of the following types:
    • intnumber - integer value. For example, 5.
    • doublenumber - double value. For example, 5.0.
    • floatnumber - float value. For example, 5.0f.
    • stringvalue - string with value. For example, "5".

void clearChildren ()

Clears all children of the current Json node.

Ptr<Json> getChild (int num)

Returns the child node of the current Json node.

Arguments

  • int num - Argument of one of the following types:
    • stringname - Name of the Json node.
    • intnumber - Number of the child of the Json node.

Return value

Child Json node.

double getNumber ()

Returns the number value of the current Json node.

Return value

Number value of the current Json node.

String getString ()

Returns the value of the current Json node as string.

Return value

Value of the current Json node

int isArray ()

Returns a value indicating if the Json node has an array type.

Return value

1 if the Json has an array type; otherwise, 0.

int isNumber ()

Returns a value indicating if the Json node has a number type.

Return value

1 if the Json has a number type; otherwise, 0.

Ptr<Json> getChild (const char * name)

Returns the child node of the current Json node.

Arguments

  • const char * name - Argument of one of the following types:
    • stringname - Name of the Json node.
    • intnumber - Number of the child of the Json node.

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.

String getFormattedSubTree (const char * name = 0)

Returns a subtree of a Json node as the formatted string.

Usage Example

To get the formatted subtree of a Json node, do the following:

Source code(UnigineScript)
Json json = new Json();

json.addChild("child_0", 1);
json.addChild("child_1", 2.2);
json.addChild("child_2", "three");
Json object = json.addChild("object");
object.setObject(("1": 5, "4": 6, "2": 7));

string fSubTree = json.getFormattedSubTree();
log.message("the formatted subtree is:%s\n", fSubTree);

In the console you see the following:

Output
the formatted subtree is:{
  "child_0": 1,
  "child_1": 2.2,
  "child_2": "three",
  "object": {
    "1": 5,
    "2": 7,
    "4": 6
  }
}

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.

String getSubTree (const char * name = 0)

Returns a subtree of a Json node as the non-formatted string.

Usage Example

To get the subtree of a Json node, do the following:

Source code(UnigineScript)
Json json = new Json();

json.addChild("child_0", 1);
json.addChild("child_1", 2.2);
json.addChild("child_2", "three");
Json object = json.addChild("object");
object.setObject(("1": 5, "4": 6, "2": 7));

string subTree = json.getSubTree();
log.message("the subtree is:%s\n", subTree);

In the console you see the following:

Output
the subtree is:{"child_0":1,"child_1":2.2,"child_2":"three","object":{"1":5,"2":7,"4":6}}

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.

void setNumber (int arg1)

Sets a number value and type to the current Json node.

Arguments

  • int arg1 - Argument of one of the following types:
    • intnumber - integer value. For example, 5.
    • doublenumber - double value. For example, 5.0.
    • floatnumber - float value. For example, 5.0f.
    • stringvalue - string with value. For example, "5".

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.

void setNull ()

Sets null type to the current Json node.

void setArray ()

Sets array type to the current Json node and adds a given array as a child.

Usage Example

Adding an Array

The following example shows how to add an array to the Json node.

Source code(UnigineScript)
Json json = new Json();
Json array = json.addChild("array");
array.setArray((1,2.2,"3"));
array.addChild(NULL,"data_0");
array.addChild(NULL, 5);

The result is:

Output
{
  "array": [
    1,
    2.2,
    "3",
    "data_0",
    5
  ]
}

Adding an Array to Array

To add array to the array, just add an array as a child:

Source code(UnigineScript)
Json json = new Json();
Json array = json.addChild("array");
array.setArray((1,2.2,"3"));
array.addChild(NULL,"data_0");
array.addChild(NULL, 5); 
array.addChild(NULL,"array").setArray((5,6,7));

The result is:

Output
{
  "array": [
    1,
    2.2,
    "3",
    "data_0",
    5",
    [
      5,
      6,
      7
    ]
  ]
}

Adding an Object to Array

To add an object to array, do the following:

Source code(UnigineScript)
Json json = new Json();
Json array = json.addChild("array");
array.setArray((1,2.2,"3"));
array.addChild(NULL,"object").setObject(("one": 5, "two": 6, "three": 7));

The result is:

Output
{
  "array": [
    1,
    2.2,
    "3",
    {
      "one": 5,
      "three": 7,
      "two": 6
    }
  ]
}
Notice
The setObject() function alphabetizes child nodes by using their names.

int parse (const char * source)

Parses a given string into the Json node.

Usage Example

Source code(UnigineScript)
Json json = new Json();
json.addChild("child_0", 1);

Json json_2 = new Json();
json_2.parse(json.getSubTree());

Now the json_2 node contains:

Source code
{
  "child_0": 1
}

Arguments

  • const char * source - String to parse.

Return value

1 if the string was parsed successfully; otherwise, 0.

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

Returns the parent node of the current Json node.

Return value

Parent 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 setBool (int arg1)

Sets a boolean value and type to the current Json node.

Arguments

  • int arg1 - Argument of one of the following types:
    • stringvalue - all strings except "true", set "false".
    • intvalue - all int values except 0, set "true".
    • doublevalue - all double values except 0.0, set "true".
    • floatvalue - all float values except 0.0f, set "true".

Ptr<Json> addChild (const char * name, 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:
    • intvalue - integer value.
    • floatvalue - float value.
    • stringvalue - string value.

Return value

Child Json node.

int isChild (const char * name)

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.

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:
    • intvalue - integer value.
    • floatvalue - float value.
    • stringvalue - string value.

Return value

Child Json node.

int save (const char * path)

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.

const char * getName ()

Returns the name of the current Json node.

Return value

The name of the Json node.

Ptr<Json> removeChild (const Ptr<Json> & json)

Removes the child Json node.

Arguments

  • const Ptr<Json> & json - Argument of one of the following types:
    • Jsonjson - Json node.
    • stringname - Name of the Json node.

Return value

Removed child 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 isString ()

Returns a value indicating if the Json node has a string type.

Return value

1 if the Json has a string type; otherwise, 0.

int isObject ()

Returns a value indicating if the Json node has an object type.

Return value

1 if the Json has an object type; otherwise, 0.

void setObject ()

Adds a given object to the current Json node.

Usage Example

Adding an Object

To add an object to the Json node, do the following:

Source code(UnigineScript)
Json json = new Json();
Json object = json.addChild("object");
object.setObject(("1": 5, "4": 6, "2": 7, "3": 8));
object.addChild("child_0", "data_0");

The result is:

Output
{
  "object": {
    "1": 5,
    "2": 7,
    "3": 8,
    "4": 6,
    "child_0": "data_0"
  }
}
Notice
The setObject() function alphabetizes child nodes by using their names.

Adding an Array to the Object

To add an array to the added object, do the following:

Source code(UnigineScript)
Json json = new Json();
Json object = json.addChild("object");
object.setObject(("1": 5, "4": 6, "2": 7, "3": 8));
object.addChild(class_remove(new Json("array"))).setArray((1,2,3));

The result is:

Output
{
  "object": {
    "1": 5,
    "2": 7,
    "3": 8,
    "4": 6,
    "array": [
      1,
      2,
      3
    ]
  }
}

Adding an Object to the Object

To add an object to the added object, do the following:

Source code(UnigineScript)
Json json = new Json();
Json object = json.addChild("object");
object.setObject(("1": 5, "4": 6, "2": 7, "3": 8));
object.addChild(class_remove(new Json("object"))).setObject(("one" : 1,"two" : 2,"three" : 3));

The result is:

Output
{
  "object": {
    "1": 5,
    "2": 7,
    "3": 8,
    "4": 6,
    "object": {
      "one": 1,
      "three": 3,
      "two": 2
    }
  }
}

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

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> removeChild (const char * name)

Removes the child Json node.

Arguments

  • const char * name - Argument of one of the following types:
    • Jsonjson - Json node.
    • stringname - Name of the Json node.

Return value

Removed 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:
    • intvalue - integer value.
    • floatvalue - float value.
    • stringvalue - string value.

Return value

Child Json node.
Last update: 2017-07-03
Build: ()