NewtonSoft.Json序列化和反序列化类IEnumerable类型&LT的财产; ISomeInterface> [英] NewtonSoft.Json Serialize and Deserialize class with property of type IEnumerable<ISomeInterface>

查看:596
本文介绍了NewtonSoft.Json序列化和反序列化类IEnumerable类型&LT的财产; ISomeInterface>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些代码来使用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>.

下面是一个简单的例子:

Here is a simple example:

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的一个实例。类型是一个接口或抽象类,不能instantated。

"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, keep your model clean, also use CustomCreationConverter, the code is simpler:

public class SampleConverter : CustomCreationConverter<ISample>
{
    public override ISample Create(Type objectType)
    {
        return new Sample();
    }
}



然后:

Then:

var sz = JsonConvert.SerializeObject( sampleGroupInstance );
JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter());



文件:的 与反序列化CustomCreationConverter

这篇关于NewtonSoft.Json序列化和反序列化类IEnumerable类型&LT的财产; ISomeInterface&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆