I have an object like this:
public class Context
{
public Dictionary<string, object> Arguments { get; set; }
}
I serialize it using this method:
private static string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
}
I deserialize it like this:
public Context DesirializeContext
{
get
{
return JsonConvert.DeserializeObject<EventContext>(Context, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
}
}
The problem is that when I try to deserialize it, the values in the dictionary don't have the correct type.
For example, if I have {"key1", 1} and {"key2", objectb}, after deserialization, the types of the values will be Int64 and Newtonsoft.Json.Linq.JArray.
Now I know I can use the below code to cast a value to a target object (my target type in this case is a DataTable)
(DataTable)JsonConvert.DeserializeObject(Arguments["SubstituteData"].ToString(), typeof(DataTable));
But is there anyway to automatically cast the values in the dictionary to their original type when desirializing? This would make things much easier.