I want to Serialize a Dictionary which contains different kinds of objects (dynamic) and save it to a file. When loading the file, the software needs to DeSerialize the data to a Dictionary again and set the objects references. When I try to set an object, it gives me the error:
Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'PartyCreator.Member'.
It seems like the DeSerialized Dictionary is working, i can access the member1 properties using loadedObjects["member1"].Name
for example but can't reference the object to an already existing object member1 created from my own Member class.
// Serializing the Dictionary
Dictionary<string, dynamic> savedObjects = new Dictionary<string, dynamic>();
savedObjects.Add("member1", member1);
savedObjects.Add("member2", member2);
savedObjects.Add("member3", member3);
savedObjects.Add("member4", member4);
var settings = new JsonSerializerSettings() { ContractResolver = new MyContractResolver() };
var saveData = JsonConvert.SerializeObject(savedObjects, settings);
// DeSerializing the Dictionary
Dictionary<string, dynamic> loadedObjects = new Dictionary<string, dynamic>();
loadedObjects = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(saveData);
member1 = loadedObjects["member1"]; // Giving an error: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'PartyCreator.Member'
// These are the settings i'm using with Json Serializer
public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Select(p => base.CreateProperty(p, memberSerialization))
.Union(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Select(f => base.CreateProperty(f, memberSerialization)))
.Where(p => !p.PropertyName.Contains("k__BackingField"))
.ToList();
props.ForEach(p => { p.Writable = true; p.Readable = true; });
return props;
}
}