4

I'm trying to deserialize my json items from a file using UnityEngine.JsonUtility. It works fine but my enum types are not getting properly converted. I tried using the EnumMember attribute but still had no luck.

How can I fix that ?

Note

I'm using this solution to read multiple files and store them in array.

[Serializable]
public class EquipementItem
{
    public enum ItemTypes
    {
        None,
        Armor,
        Weapon
    }

    public enum SlotTypes
    {
        Head,
        Shoulders,
        Chest,
        Bracers,
        Gloves,
        Waist,
        Legs,
        Boots,
        Weapon
    }

    public int ID;
    public string Name;

    public ItemTypes ItemType;
    public SlotTypes SlotType;
}

And the json file

{
"Items": [
{
  "ID": "1",
  "Name": "Basic Sword",
  "ItemType": "Weapon",
  "SlotType": "Weapon"
},
{
  "ID": "2",
  "Name": "Advanced Sword",
  "ItemType": "Weapon",
  "SlotType": "Weapon"
},
{
  "ID": "3",
  "Name": "Leather Chest",
  "ItemType": "Armor",
  "SlotType": "Chest"
}
]}

This is the class where I load the json file:

public class Items : MonoBehaviour
{
    public static EquipementItem[] EquipableItems;

    private void Awake()
    {
        string jsonFile = File.ReadAllText(Application.dataPath + "/Scripts/Databases/EquipableItemsDB.json");
        EquipableItems = JsonHelper.FromJson<EquipementItem>(jsonFile);
    }
}
Fab
  • 14,327
  • 5
  • 49
  • 68
mashinkata
  • 55
  • 1
  • 1
  • 11
  • Enum's are integers underneath. Have you tried setting "ItemType" in your JSON to 1, and "SlotType" to 2? – ColinM Nov 25 '16 at 20:24
  • Input json and the model is there, but I can't see **your code** – L.B Nov 25 '16 at 20:26
  • Can you post this as an answer ? It worked ! – mashinkata Nov 25 '16 at 20:26
  • @L.B What else do you need ? I noted that I'm using the solution shown in the linked answer I'm reading the json the same way it's done there `JsonHelper.FromJson(jsonString);` That's pretty much the entire project as that's just a test. – mashinkata Nov 25 '16 at 20:28
  • @mashinkata When I test a code in a question, all I want is a code ready to copy and paste.... See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – L.B Nov 25 '16 at 20:37
  • @L.B I've updated the question now you can see where I load the json but you still need to create an object to hold it for you. – mashinkata Nov 25 '16 at 20:52

1 Answers1

11

Your JSON properties are all strings and so they can only be deserialized to a String, while Enum values are actually integers.

You should be able to change your JSON to the following and it'll deserialize just fine

{
    "Items": [
    {
      "ID": "1",
      "Name": "Basic Sword",
      "ItemType": 2,
      "SlotType": 8
    },
    {
      "ID": "2",
      "Name": "Advanced Sword",
      "ItemType": 2,
      "SlotType": 8
    },
    {
      "ID": "3",
      "Name": "Leather Chest",
      "ItemType": 1,
      "SlotType": 2
    }
]}

Update

At the time of writing this it had slipped my mind that StringEnumConverter existed. If you would like to retain readable names in your JSON model

[Serializable]
public class EquipementItem
{
    public enum ItemTypes
    {
        None,
        Armor,
        Weapon
    }

    public enum SlotTypes
    {
        Head,
        Shoulders,
        Chest,
        Bracers,
        Gloves,
        Waist,
        Legs,
        Boots,
        Weapon
    }

    public int ID { get; set; }

    public string Name { get; set; }

    [JsonConverter(typeof(StringEnumConverter))]
    public ItemTypes ItemType { get; set; }

    [JsonConverter(typeof(StringEnumConverter))]
    public SlotTypes SlotType { get; set; }
}
ColinM
  • 2,622
  • 17
  • 29
  • Can you provide an example of how would one do the same thing but with `Enum.Parse` ? – mashinkata Nov 25 '16 at 20:34
  • I've updated my answer, there's actually an easier way, if you're using Newtonsoft.Json, to deserialize an Enum – ColinM Nov 25 '16 at 20:38
  • Will newtonsoft.Json combine well with Unity's JasonUtility ? – mashinkata Nov 25 '16 at 20:45
  • Apologies, glanced past the mentioning of `UnityEngine.JsonUtility`, updated answer again - for simplicity of code it's best to use integers as I suspect you'll be serializing in code which will output integers, and deserialize in code, as opposed to manually editing files. – ColinM Nov 25 '16 at 20:47
  • But I want to use readable string values in my json file. Can I override enum deserialisation logic to transform string values to my enum so I can do this: "ItemType": "Armor", "SlotType": "Chest" – Konstantin Abakumov Jan 02 '20 at 10:33
  • 1
    @KonstantinAbakumov decorate your enum members with `[JsonConverter(typeof(StringEnumConverter))]`. I have updated my answer to reflect this. – ColinM Jan 02 '20 at 13:25