6

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);
    }
}
6
  • Enum's are integers underneath. Have you tried setting "ItemType" in your JSON to 1, and "SlotType" to 2? Commented Nov 25, 2016 at 20:24
  • Input json and the model is there, but I can't see your code Commented Nov 25, 2016 at 20:26
  • Can you post this as an answer ? It worked ! Commented Nov 25, 2016 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<EquipementItem>(jsonString); That's pretty much the entire project as that's just a test. Commented Nov 25, 2016 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 Commented Nov 25, 2016 at 20:37

1 Answer 1

15

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; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Can you provide an example of how would one do the same thing but with Enum.Parse ?
I've updated my answer, there's actually an easier way, if you're using Newtonsoft.Json, to deserialize an Enum
Will newtonsoft.Json combine well with Unity's JasonUtility ?
@KonstantinAbakumov decorate your enum members with [JsonConverter(typeof(StringEnumConverter))]. I have updated my answer to reflect this.
For some reasons, Unity decided not to support com.unity.nuget.newtonsoft-json (docs.unity3d.com/Packages/[email protected]/…) anymore, and using UnityEngine.JsonUtility still doesn't respect the [JsonConverter] attributes. I don't see what is the modern solution now... we either have to use integer or an obsolete package. Is there a third solution I don't see?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.