2

I am trying to mod a Unity game using BepInEx. I want to read a list of a type of class I've created from a JSON, then read each object from that class. I used UnityEngine.JsonUtility to do this and created a wrapper class containing my list.

    [System.Serializable]
    public class StarSignDataList
    {
        public List<StarSignData> starSigns;
    }

    // Stores all data pertaining to star signs as read from JSON file.
    [System.Serializable]
    public class StarSignData
    {
        public string name;
        public int id;
        public string description;
    }

Then I read the json file from the correct file path, parsing that information into a string and called the JsonUtility.FromJason() method to convert the string into my StarSignDataList class.

       // Reads all star sign data from JSON and assigns values to list.
        public static void InitializeData()
        {
            log.LogInfo("Initializing Star Sign data.");
            string json = File.ReadAllText("BepInEx/plugins/StarSigns/starsigns.json");
            log.LogInfo(json);

            StarSignDataList list = UnityEngine.JsonUtility.FromJson<StarSignDataList>(json);
            log.LogWarning(list.starSigns.Count == 0 ? "Not null" : "Null!");
            log.LogInfo(list.starSigns[0].name);
            signs = list.starSigns;
            foreach (StarSignData data in signs)
            {
                log.LogInfo(data.name);
            }
        }

When I print out the contents of my json immediately after reading it, it comes out as expected, looking exactly like my json file. However when I convert the json string into my list class, the list is completely empty and then throws a NullReferenceException when trying to print the first object. I've trying using arrays instead of lists, I've checked that my json is correctly written, I've pored over countless google search results without success.

Here is my json file.

{
    "starSigns": [
        {
            "name": "Sign of the Fool",
            "id": 0,
            "description": "You are of normal birth."
        },
        {
            "name": "Sign of Loki",
            "id": 1,
            "description": "The sign of the god of betrayal shone above your birth place."
        },
        {
            "name": "Sign of Odin",
            "id": 2,
            "description": "A tumultuous child at birth, your muscles bear the strength of Odin himself."
        }
    ]
}

Out of desperation I tried converting a class like this to a json, and it just wrote to the file "{ }". When I try to do the same with just my StarSignData class it works as expected, but I need to put multiple objects in my json and the list class does not seem to work. I also can't use external libraries such as SimpleJson for this.

SpaceDrive
  • 21
  • 2
  • 1
    What if you followed this person's approach?: https://gamedev.stackexchange.com/a/166262 – EspressoBeans Mar 20 '21 at 06:12
  • In other words, deserialize to the collection directly instead of using a parent class with a nested list. Here's another example: https://stackoverflow.com/a/36244111/1188197. – EspressoBeans Mar 20 '21 at 06:22
  • 1
    @EspressoBeans none of the links directly deserializes into a list/array but rather exactly as OP tried it with a wrapper type since direkt deserialization to collections isn't supported by the built-in `JsonUtility` used by OP ... – derHugo Mar 20 '21 at 14:43
  • Are you sure there are no typos? Also the check `list.starSigns.Count == 0 ? "Not null" : "Null!"` sounds a bit odd to be ... nothing here is checking for `null` and if the list is empty (Count == 0) your print `"Not null"` and if it holds elements (Count != 0) you print `"Null"` ... ? I guess you would rather print e.g. `list == null ? "list Null" : (list.starSigns == null ? "startSigns Null" : (list.starSigns.Count == 0 ? "starSigns Empty" : "Worked!"))` or something similar – derHugo Mar 20 '21 at 15:51
  • I can't see any problems with your classes, the JSON in your question can be deserialized successfully using them with [tag:json.net]. Are you sure you haven't made a mistake in the question somewhere? Maybe you are using different classes from a different namespace, or `starSigns` is actually `internal` rather than `public`? – dbc Mar 20 '21 at 17:25
  • I did explain the question with some errors actually; the null check was wrong, but the code actually returns a NullReferenceException on that line when checking the list length, so I assume the list isn't getting created at all. None of my classes are marked as internal and everything is under namespace StarSigns. – SpaceDrive Mar 20 '21 at 21:01
  • Then hard to know what the problem might be, you're following all the recommendations from https://stackoverflow.com/a/36244111/1188197 and Json.NET handles this just fine, see https://dotnetfiddle.net/WulJaN. Sorry I can't help further. – dbc Mar 21 '21 at 14:20

0 Answers0