我有一个类似
的对象public MyObject
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("type")]
public Type Type { get; set; }
}
我使用序列化设置
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
};
序列化为
{
"id": "00000000-0000-0000-0000-000000000000",
"type": "SomeNamespace.SomeType, SomeAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
}
但随后无法反序列化,并显示错误
System.ArgumentException
无法转换或从System.String转换为System.Type。
Json.Net 9.0.x版是否有简单的修补程序?
答案 0 :(得分:0)
我运行了以下代码,它在Newtonsoft 9.0.1中正常工作
private void TestJson()
{
var jsonSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
};
var myObj = new MyObject
{
Type = typeof(string)
};
var serialize = JsonConvert.SerializeObject(myObj, jsonSettings);
var newObj = JsonConvert.DeserializeObject<MyObject>(serialize, jsonSettings);
}
输出:
DLL信息: