(使用 Newtonsoft JSON.NET)
您好我在反序列化事件列表时遇到问题,其中事件是基本类型,例如ResourceEvent 是一个子类。如果我只是进行序列化然后反序列化,一切正常并且列表包含 ResourceEvents - 但是我将使用 EF7 的那些存储到 postgres 的 jsonb 列中。
当我从 postgres 获取 JSON 负载时,它已重新排序(但 JSON 合法)属性。当我尝试使用 Newtonsoft JSON.net 进行反序列化时,它只给我一个包含基本类型 Event 而不是子类 ResourceEvent 的列表。
我添加了两个高度剥离的示例,不同之处在于“Type”属性位于不同的位置。其他属性的顺序也已在类中被打乱。我根本没有关于这些类的注释。
此负载成功反序列化:
json
{
"$type": "System.Collections.Generic.List`1[[Event, Tests]], mscorlib",
"$values": [
{
"$type": "ResourceConfigurationEvent, Tests",
/* Properties */
"Resources": {
/* Properties */
},
"Type": 1
}
]
}
此负载反序列化失败:
json
{
"$type": "System.Collections.Generic.List`1[[Event, Tests]], mscorlib",
"$values": [
{
"Type": 1,
"$type": "ResourceConfigurationEvent, Tests",
/* Properties */
"Resources": {
/* Properties */
},
}
]
}
最佳答案
Json.Net 通常希望 $type
元数据成为每个对象的第一个属性,以实现反序列化的最佳效率。如果 $type
没有首先出现,则 Json.Net 假定它不存在。这就是为什么在重新排序属性时会得到不同结果的原因。
幸运的是,Json.Net 提供了一个 MetadataPropertyHandling
设置以使其能够应对这种情况。如果将 MetadataPropertyHandling
设置为 ReadAhead
,它应该可以解决您的问题。请注意,这会对性能产生影响。
这是一个code sample来自文档。
string json = @"{
'Name': 'James',
'Password': 'Password1',
'$type': 'MyNamespace.User, MyAssembly'
}";
object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
// $type no longer needs to be first
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
});
User u = (User)o;
Console.WriteLine(u.Name);
// James
关于c# - 继承 Newtonsoft JSON.NET 的反序列化无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37563603/