how to use <insert here> in CSQC

Discuss CSQC related programming.
gnounc
Posts: 428
Joined: Mon Apr 06, 2009 6:26 am

Re: how to use <insert here> in CSQC

Post by gnounc »

Json tutorial continued.

Starting with a json string borrowed from https://json.org/example.html

Code: Select all

	string json_string_data = "{\"widget\": {\"debug\": \"on\", \"window\": {\"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500 }, \"image\": {\"src\": \"Images/Sun.png\", \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\"}, \"text\": {\"data\": \"Click Here\", \"size\": 36, \"style\": \"bold\", \"name\": \"text1\", \"hOffset\": 250, \"vOffset\": 100, \"alignment\": \"center\", \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"} }}";
We parse the string into a json node.

Code: Select all

jsonnode root = json_parse(json_string_data);
Next we probably want the data, we grab that using the apropriate json_get_<type> method.
Here we will grab the text size.

Code: Select all

float f_text_size = json_get_float(root.widget.text.size);
If instead of the data, we had just wanted to know what that data was called, we would grab its name

Code: Select all

string nodeName = json_get_name(root.widget.text.size);
If we wanted to iterate over the top level objects of the node, we could use a for loop
over the length, with a child index like so:

Code: Select all

for (int idx = 0; idx < json_get_length(root); idx++)
{
	jsonnode node = json_get_child_at_index(idx);

	string nodeName = json_get_name(node);
}
We loop over array nodes the same way.

If we are parsing json data that we have no previous knowledge of, we will need to query for the data types so we know which json_get_<type>() function to call.

Code: Select all

float data_type = json_get_value_type(node);
Calling that function will result in one of the values from this enum:

Code: Select all

enum json_type_e : int
{
	JSON_TYPE_STRING,
	JSON_TYPE_NUMBER,
	JSON_TYPE_OBJECT,
	JSON_TYPE_ARRAY,
	JSON_TYPE_TRUE,
	JSON_TYPE_FALSE,
	JSON_TYPE_NULL
};
Lastly, when we are done with our jsonnode, we should free it back up with a call to:

Code: Select all

json_free(root);
gnounc
Posts: 428
Joined: Mon Apr 06, 2009 6:26 am

Re: how to use <insert here> in CSQC

Post by gnounc »

In the previous post we used json_get_float(node) to get a float out of the node.

This can be simplified using some syntactic sugar.
instead of using json_get_float(), we could just append a ".f" to the nodepath as follows:

Code: Select all

float f_text_size = root.widget.text.size.f;
This also works for strings

Code: Select all

string s_window_title = root.widget.window.title.s;
and integers.

Code: Select all

int i_text_size = root.widget.text.size.i;
accessors are also provided for the other functions.

json_get_value_type

Code: Select all

float valueType = root.widget.text.size.type;
json_get_length

Code: Select all

float nodeLength = root.length;
json_get_child_at_index

Code: Select all

jsonnode widgetNode =  root.a[0];
json_find_object_child

Code: Select all

jsonnode widgetNode =  root["widget"];
json_get_name

Code: Select all

float valueType = root.widget.text.size.name;
Post Reply