I'm using Unity's JSON helper library JSONUtility
but it doesn't seems to work at all. I've made a simple test object that contains a single int,string and enum value which i tried to serialize and deserialize but with no luck. Here's my code
private void Start()
{
string[] jsonFile = File.ReadAllLines(Application.dataPath + "/Resources/ItemsDatabase.json");
Item testDeserialization = Item.CreateFromJSON(jsonFile[1]);
Item testSerialization = new Item(1, "Test", Item.ItemTypes.Weapon);
string a = JsonUtility.ToJson(testSerialization);
}
and this is the json file
{"Items":[
{
"ID": "1",
"Name": "Basic Sword",
"ItemType": "Weapon"
},
{
"ID": "2",
"Name": "Advanced Sword",
"ItemType": "Weapon"
}
]}
This is the Item class
[System.Serializable]
public class Item
{
public enum ItemTypes
{
Weapon,
Armor
}
public int ID { get; set; }
public string Name { get; set; }
public ItemTypes ItemType { get; set; }
public Item(int id, string name, ItemTypes itemType)
{
ID = id;
Name = name;
ItemType = itemType;
}
public static Item CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Item>(jsonString);
}
}
The result in testDeserialization
are just the default values of all the variables and the result in testSerialization
are just 2 curly brackets {}
. Any tips how can I make this work ?