Saving Data as JSON in Unity
Hello everyone, i hope this article useful for anyone who wants to learn about saving game data in JSON file. First thing we should know is about JSON itself. What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition — December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. (Source: https://www.json.org/)
Structure Data in JSON
Three important thing about JSON we should know. Object, Array, Values in JSON structure. What are those mean? Let see the sample below
Object, An object is an unordered set of name/value pairs. An object begins with { left brace and ends with } right brace. Each name is followed by : colon and the name/value pairs are separated by , comma. (source: https://www.json.org/).
Array, An array is an ordered collection of values. An array begins with [ left bracket and ends with ] right bracket. Values are separated by , comma. (source: https://www.json.org/). In the sample above, you can see an array start at line 4 until line 13.
Values, A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested. (source: https://www.json.org/). Potion name has value and the value is magic potion, value has a number value, effect has an array object value.
Then how I save data into JSON in Unity?
Unity provide a class that can be used to serialized a class into JSON file or otherwise, the class is JsonUtility. For further information you can access unity documentation (https://docs.unity3d.com/ScriptReference/JsonUtility.html).
First thing!
Prepare the class. So I made a script in Unity and name it SaveData.cs.
You should prepare class that the name of variable is represent the name of value in JSON. One important thing again that you should know is about array of object as value. If the value is array of object, you should prepare the class for the object. One object, one class!!.
There are two classes with serialization attribute. In JSON sample above there are two object, first object is all the JSON data itself, PotionData class. And the second one is array of object as value, Effect class. For the array of object value, i made it as a list. Make sure add serialization attribute to the class that you want to serialize into JSON.
Add saving method
In SaveData class I add the method for save the data into JSON. Because JSON is text based format, I use WriteAllText method to save into text file in storage. You can read the documentation from microsoft about WriteAllText (https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netframework-4.8).
Setup it in unity!
I Add SaveData.cs as component, and add button in my canvas.
Then, I add the SaveIntoJson method in OnClick trigger button.
Play the scene, try to change the value of PotionData variable in SaveData object. Then click the button.
Then click the button to start SaveIntoJson method. After click the button for save, check the file in persistentDataPath. If you using windows you can check in C:\Users\username\AppData\Local\company\game. If you use mac, you can /Users/username/Library/Application Support/company/game. If the saving process is sucess, there will be a file JSON.
Then open the JSON file with text editor or your IDE to code.
Conclusion
There are several ways to save your data. One of them is like mine, save the data into JSON. If you are interested, you can clone from my repository github
But for the security, in my opinion, we should encrypt JSON data before we save it into text file. Because text file can be opened by someone outside there if they know the file in our storage.
Hope you get benefit from my sharing, drop your comment or anything you want to share. Cheerss!!