The JSON format allows storing multiple data into a single string. In this way, you haven’t to create a Database table column for each value, but just create one column and store the various values as a single JSON string. In general, the process of reading and using a JSON string follows these steps:
- convert an object into a JSON string (with Unity’s JsonUtility class or other C# techniques);
- write this string into a Database table;
- when needed, read this string from the table;
- convert this string back to the original object (with Unity’s JsonUtility class or other C# techniques).
However, this approach has two weak points you should keep in mind:
- if you have to update one value only from a JSON string, you have to operate with the whole string (read the entire string from the Database, convert it, update one value, reconvert it, write it into the Database);
- for the JSON format, you have to use the JsonUtility Unity class or other third-party classes that usually work with objects (so you have to create a specific class for that object).
The UniREST JSON class
In order to make the use of the JSON format simpler, the UniREST solution comes with a built-in JSON class. This class allows working with the JSON format on-the-fly, without using third-party tools, JsonUtility or creating additional classes.
Declaration
The first thing to do is to declare an instance of the UniREST Json class. The declaration is needed for both creating a new JSON and parsing a JSON string. In the following examples, the first row declares a new JSON structure, the second row converts a JSON string (myJString) into a JSON structure.
var myNewJ = new Json();
var myJData = new Json(myJString);
Creating a new JSON structure
Once you have declared an instance of the JSON class, your variable is ready to collect your data. To add a new value to your JSON structure you have just to use the Add() method:
variable.Add(name, value);
Parameter | Data-type | Description |
name | string | The name that identifies this value. It must be an unique string. |
value | object | The value to save into the JSON structure, identified by the given name. It should be a common data-type like string, int, float, bool, etc. |
Converting a JSON structure to string
Once you have filled the JSON structure with the data you need, you must convert it into a single string so as to save it into your Database table. You have just to use the ToString() method:
variable.ToString();
var myJData = new Json();
myJData.Add("player", "Conan");
myJData.Add("health", 100);
myJData.Add("strength", 10.6);
myJData.Add("hasWeapon", true);
var stringToSave = myJData.ToString();
Converting a JSON string to a JSON structure
Once you have got a JSON string, you can easily convert it into a JSON structure simply declaring an instance passing that string as a class parameter:
var myJData = new Json(myJString);
The Json class automatically convert the JSON string into a C# dictionary where each value is assigned to the given names. To read those values you have to use the Get() method, specifying the original value data-type:
variable.Get<TYPE>(name);
Parameter | Data-type | Description |
TYPE | data-type | The data-type the value must be converted to. |
name | string | The name that identifies the value you want to read. |
var myJData = new Json(myJString);
var player = myJData.Get<string>("player");
var health = myJData.Get<int>("health");
Working with Json Array
The UniREST Json class works with a single JSON structure. If you need to work with a JSON array, you have to use the UniREST JsonArray class. The JsonArray class generates an array of Json classes. The JsonArray methods are similar to the Json methods:
var myJData01 = new Json();
myJData01.Add("player", "Conan");
var myJArray = new JsonArray();
myJArray.Add(myJData01);
var arrayToSave = myJArray.ToString();
The Get() method just requires the index that locates the Json data into the array:
var myJArrayData = new JsonArray(myJArrayString);
var player = myJArrayData.Get<string>(0, "player");