I'm trying some different serialization formats for a project. I'm fairly new to serialization, so I had been using the BinaryFormatter because it works out of the box for my solution, but I wanted to do some testing and see how Newtonsoft JSON fared in comparison.
Here is a rough outline of the Dictionary I am trying to (de)serialize.
Dictionary<string, MyObjectList> myDictionary;
where each MyObject in that list of Dictionary values also has a List of different objects
I had been serializing by doing
using(Filestream file = File.Create(path))
using(Streamwriter sw = new Streamwriter(file))
{
sw.Write(JsonConvert.SerializeObject(dictionary, Formatting.Indented));
}
And de-serializing by doing
dictionary = JsonConvert.DeserializeObject<Dictionary<string, MyObjectList>>(File.ReadAllText(path));
I get returned the exception:
JsonSerializationException: Cannot create and populate list type MyObjectList
*edit to add List class code as requested
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
// List of BossLog instances that makes up the Values in BossLogDictionary
[System.Serializable]
public class BossLogList : IEnumerable
{
public string bossName { get; private set; }
private List<BossLog> data { get; set; }
public BossLogList()
{
data = new List<BossLog>();
}
public BossLogList(string bossName)
{
this.bossName = bossName;
data = new List<BossLog>();
}
// Wrapper for add
public void Add(in string logName)
{
data.Add(new BossLog(bossName, logName));
}
// Wrapper for List.RemoveAll using just a passed string and omitting the int return value
public void RemoveAll(string logName)
{
data.RemoveAll(log => log.logName.CompareTo(logName) == 0);
}
// IEnumerable interface
public IEnumerator GetEnumerator()
{
return ((IEnumerable)data).GetEnumerator();
}
// Wrapper for List.Contains checking with a passed logName parameter
public bool Exists(string logName)
{
return data.Exists(bossLog => bossLog.logName.CompareTo(logName) == 0);
}
// Wrapper for List.FindIndex checking with a passed logName parameter
public int FindIndex(string logName)
{
return data.FindIndex(bossLog => bossLog.logName.CompareTo(logName) == 0);
}
// Wrapper for List.Find checking with a passed logName parameter
public BossLog Find(string logName)
{
return data.Find(bossLog => bossLog.logName.CompareTo(logName) == 0);
}
// Wrapper for List.Count
public int Count { get { return data.Count; } }
// Return a custom struct with our total data
public LogDataStruct GetBossTotalsData()
{
LogDataStruct logDataStruct = new LogDataStruct();
foreach(BossLog log in data)
{
logDataStruct.kills += log.kills;
logDataStruct.time += log.time;
logDataStruct.loot += log.loot;
}
return logDataStruct;
}
}