我遇到了反序列化词典的问题。我不能将对象从Dictionary转换为我的类型Remiza ...我使用的是Json.net而且我能看到的是Dictionary中的对象是JObject而不是Object而我无法将它们转换为我的风格。这是我的序列化/反序列化代码:
private static Dictionary<Type, List<Object>> _ekstensje = new Dictionary<Type, List<Object>>();
public static void SerializeDictionary()
{
string json = JsonConvert.SerializeObject(_ekstensje);
System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\tmp\dictionary.json");
file.WriteLine(json);
file.Close();
}
public static void DeserializeDictionary()
{
string json;
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\tmp\dictionary.json");
json = file.ReadToEnd();
file.Close();
_ekstensje = JsonConvert.DeserializeObject<Dictionary<Type, List<Object>>>(json);//Deserializacja Dictionary
Debug.WriteLine(_ekstensje);
}
public static List<Object> GetEkstensja(Type className)
{
List<Object> list = _ekstensje[className];
return list;
}
Exectution:
ObjectPlus.DeserializeDictionary();
List<Object> list = ObjectPlus.GetEkstensja(typeof(Remiza));
foreach (Object o in list)
{
Remiza r = (Remiza) o;
listaRemiz.Add(r);
}
我的问题是什么时候投射到&#34; Remiza&#34;我有这个例外:
An exception of type 'System.InvalidCastException' occurred in Osek_MAS_WPF.exe but was not handled in user code. Additional information: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Osek_MAS_WPF.Remiza'.
感谢您的帮助!
答案 0 :(得分:1)
这应该允许您将JObect转换为您的Remiza类型。
ObjectPlus.DeserializeDictionary();
List<Object> list = ObjectPlus.GetEkstensja(typeof(Remiza));
foreach (Object o in list)
{
Remiza r = o.ToObject<Remiza>();
listaRemiz.Add(r);
}
我从下面链接的stackoverflow回答中得到了这个。如果我放的东西不起作用,请查看链接,它应该可以帮助您运行它。
答案 1 :(得分:0)
为了使用Json.NET成功序列化和反序列化多态类型,您需要设置TypeNameHandling = TypeNameHandling.Auto
,如下所示:
public class ObjectPlus
{
// Replace with whatever file name is appropriate. My computer doesn't have a "c:\tmp" directory.
static string JsonFileName { get { return Path.Combine(Path.GetTempPath(), "dictionary.json"); } }
static JsonSerializerSettings JsonSettings { get { return new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Formatting = Formatting.Indented }; } }
private static Dictionary<Type, List<Object>> _ekstensje = new Dictionary<Type, List<Object>>();
public static void SerializeDictionary()
{
var path = JsonFileName;
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
using (var writer = new StreamWriter(stream))
{
var serializer = JsonSerializer.CreateDefault(JsonSettings);
serializer.Serialize(writer, _ekstensje);
}
}
public static void DeserializeDictionary()
{
var path = JsonFileName;
try
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var serializer = JsonSerializer.CreateDefault(JsonSettings);
_ekstensje = serializer.Deserialize<Dictionary<Type, List<Object>>>(jsonReader);
}
}
catch (FileNotFoundException)
{
// File was not created yet, dictionary should be empty.
_ekstensje.Clear();
}
}
public static List<Object> GetEkstensja(Type className)
{
List<Object> list = _ekstensje[className];
return list;
}
public static void AddEkstensja<T>(T obj)
{
List<Object> list;
if (!_ekstensje.TryGetValue(obj.GetType(), out list))
list = _ekstensje[obj.GetType()] = new List<object>();
list.Add(obj);
}
internal static string ShowJsonContents()
{
if (!File.Exists(JsonFileName))
return string.Empty;
return File.ReadAllText(JsonFileName);
}
}
现在,您应该可以在字典包含Remiza
的实例时对其进行序列化和反序列化。
这适用于序列化为对象或集合的类型。但是,如果您的字典包含序列化为JSON基元的类型 - 例如enum
或long
- 您可能需要将它们封装在Deserialize specific enum into system.enum in Json.Net行的类型包装器中
(顺便提一下,您的_ekstensje
词典不是线程安全的。)