0

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 ?

mashinkata
  • 55
  • 1
  • 1
  • 11
  • This is not enough to help you. Post the complete `Item` class that represents the json. Provide link to where you got JSONUtility from. – Programmer Nov 25 '16 at 18:19
  • Oh sry i forgot to post the class and also JSONUtility is contained in the default UnityEngine namespace here's a link to that https://docs.unity3d.com/ScriptReference/JsonUtility.html – mashinkata Nov 25 '16 at 18:20
  • Remove the `{ get; set; }` as mentioned in the duplicated answer. Unity does not support that. – Programmer Nov 25 '16 at 18:23
  • So I guess properties aren't supported :(. Is there any workaround ? – mashinkata Nov 25 '16 at 18:23
  • Sorry about that. No workaround. Simply remove it. It's not like removing it will affect performance to something.In fact, removing it will improve performance. You **only** have to remove it from classes that you convert to and from json/object. – Programmer Nov 25 '16 at 18:28
  • Thank you a lot for pointing me to your answer it covers all my concerns but i have 1 more questions how would one dynamically save player info and store it into a json file for example ? – mashinkata Nov 25 '16 at 18:56
  • You are welcome! To save player info with json see [here](http://stackoverflow.com/a/40097623/3785314) – Programmer Nov 25 '16 at 19:09
  • @Programmer I found a way to use properties in a json class. You can combine private variable with the `SerializeField` attribute and assign the json value to this variable while you have a public property which encapsulates it. – mashinkata Dec 02 '16 at 10:14
  • Yes, that would work too. The problem with that is that you will have 2 variables for each variable. I can't stand doing that just to use json. Go for it if that's what you want – Programmer Dec 02 '16 at 14:05

0 Answers0