I'm having an issue deserializing a dictionary that i've previously serialized.
The dictionary is using a custom field (<int, DialogueSave>
) so I'm wondering if the reason this isn't working is because JSON.net cant deserialize dictionaries into anything but <string,string>
... in which case I have no clue what to do or how I could go fixing THAT.
The following is the relevant code:
[System.Serializable]
public class DialogueSave
{
public string dialogue;
public int endActionID;
// other stuff
}
[System.Serializable]
public class DialogueSaveDictionary
{
[SerializeField] public Dictionary<int, DialogueSave> saveDictionary;
}
void Save()
{
DialogueSaveDictionary saveData = new DialogueSaveDictionary();
saveData.saveDictionary = new Dictionary<int, DialogueSave>();
// trying to read Json saved file and deserialize in order to add new
// items to dictionary and serizalize again
string jsonSavedDialoguePath = Application.dataPath + "/dialogueSave.json";
StreamReader reader = new StreamReader(jsonSavedDialoguePath);
saveData.saveDictionary = JsonConvert.DeserializeObject<Dictionary<int, DialogueSave>>(reader.ReadToEnd());
// add new data on top of already saved data
}
No error appears through this code, however when I start to add the actual new data to the deserialized saved data via dictionary entry, I get a NullReferenceException
deserializing the JSON is giving me a null dictionary for some reason
edit: this is an example of what the json serializes as
{
"saveDictionary": {
"123000": {
"dialogue": "hey",
"endActionID": 2
},
"123001": {
"dialogue": "bye",
"endActionID": 1
}
}