NewtonSoft.Json Serialize and Deserialize class with property of type IEnumerablelt;ISomeInterfacegt;(NewtonSoft.Json 具有 IEnumerablelt;ISomeInterfacegt; 类型属性的序列化和反序列化类)
问题描述
我正在尝试移动一些代码以使用 ASP.NET MVC Web API 生成的 Json 数据而不是 SOAP Xml.
I am trying to move some code to consume ASP.NET MVC Web API generated Json data instead of SOAP Xml.
我在序列化和反序列化以下类型的属性时遇到了问题:
I have run into a problem with serializing and deserializing properties of type:
IEnumerable<ISomeInterface>.
这是一个简单的例子:
public interface ISample{
int SampleId { get; set; }
}
public class Sample : ISample{
public int SampleId { get; set; }
}
public class SampleGroup{
public int GroupId { get; set; }
public IEnumerable<ISample> Samples { get; set; }
}
}
我可以通过以下方式轻松序列化 SampleGroup 的实例:
I can serialize instances of SampleGroup easily with:
var sz = JsonConvert.SerializeObject( sampleGroupInstance );
但是对应的反序列化失败了:
However the corresponding deserialize fails:
JsonConvert.DeserializeObject<SampleGroup>( sz );
带有此异常消息:
无法创建 JsonSerializationExample.ISample 类型的实例.类型是接口或抽象类,无法实例化."
"Could not create an instance of type JsonSerializationExample.ISample. Type is an interface or abstract class and cannot be instantated."
如果我派生一个 JsonConverter,我可以如下装饰我的属性:
If I derive a JsonConverter I can decorate my property as follows:
[JsonConverter( typeof (SamplesJsonConverter) )]
public IEnumerable<ISample> Samples { get; set; }
这是 JsonConverter:
Here is the JsonConverter:
public class SamplesJsonConverter : JsonConverter{
public override bool CanConvert( Type objectType ){
return ( objectType == typeof (IEnumerable<ISample>) );
}
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer ){
var jA = JArray.Load( reader );
return jA.Select( jl => serializer.Deserialize<Sample>( new JTokenReader( jl ) ) ).Cast<ISample>( ).ToList( );
}
public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer ){
... What works here?
}
}
这个转换器解决了反序列化问题,但我不知道如何编写 WriteJson 方法以使序列化再次工作.
This converter solves the deserialization problem but I cannot figure how to code the WriteJson method to get serialization working again.
有人可以帮忙吗?
这是解决问题的正确"方法吗?
Is this a "correct" way to solve the problem in the first place?
推荐答案
你不需要使用 JsonConverterAttribute
,只要保持你的模型干净,使用 CustomCreationConverter
,代码更简单:
You don't need to use JsonConverterAttribute
, just keep your model clean and use CustomCreationConverter
instead, the code is simpler:
public class SampleConverter : CustomCreationConverter<ISample>
{
public override ISample Create(Type objectType)
{
return new Sample();
}
}
然后:
var sz = JsonConvert.SerializeObject( sampleGroupInstance );
JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter());
文档:使用 CustomCreationConverter 反序列化
这篇关于NewtonSoft.Json 具有 IEnumerable<ISomeInterface> 类型属性的序列化和反序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NewtonSoft.Json 具有 IEnumerable<ISomeInterface> 类型属性的序列化和反序列化类


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01