<legend id='8gOCj'><style id='8gOCj'><dir id='8gOCj'><q id='8gOCj'></q></dir></style></legend>
        <tfoot id='8gOCj'></tfoot>
      1. <small id='8gOCj'></small><noframes id='8gOCj'>

          <bdo id='8gOCj'></bdo><ul id='8gOCj'></ul>
        <i id='8gOCj'><tr id='8gOCj'><dt id='8gOCj'><q id='8gOCj'><span id='8gOCj'><b id='8gOCj'><form id='8gOCj'><ins id='8gOCj'></ins><ul id='8gOCj'></ul><sub id='8gOCj'></sub></form><legend id='8gOCj'></legend><bdo id='8gOCj'><pre id='8gOCj'><center id='8gOCj'></center></pre></bdo></b><th id='8gOCj'></th></span></q></dt></tr></i><div id='8gOCj'><tfoot id='8gOCj'></tfoot><dl id='8gOCj'><fieldset id='8gOCj'></fieldset></dl></div>

        NewtonSoft.Json 具有 IEnumerable&lt;ISomeInterface&

        时间:2023-08-22
        <i id='kVt4b'><tr id='kVt4b'><dt id='kVt4b'><q id='kVt4b'><span id='kVt4b'><b id='kVt4b'><form id='kVt4b'><ins id='kVt4b'></ins><ul id='kVt4b'></ul><sub id='kVt4b'></sub></form><legend id='kVt4b'></legend><bdo id='kVt4b'><pre id='kVt4b'><center id='kVt4b'></center></pre></bdo></b><th id='kVt4b'></th></span></q></dt></tr></i><div id='kVt4b'><tfoot id='kVt4b'></tfoot><dl id='kVt4b'><fieldset id='kVt4b'></fieldset></dl></div>
          <bdo id='kVt4b'></bdo><ul id='kVt4b'></ul>
            <tbody id='kVt4b'></tbody>
            1. <legend id='kVt4b'><style id='kVt4b'><dir id='kVt4b'><q id='kVt4b'></q></dir></style></legend>
                <tfoot id='kVt4b'></tfoot>
                • <small id='kVt4b'></small><noframes id='kVt4b'>

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

                  问题描述

                  我正在尝试移动一些代码以使用 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&lt;ISomeInterface&gt; 类型属性的序列化和反序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将 JToken 添加到 JObject? 下一篇:在 C# 中使用 newtonsoft 查找并返回 JSON 差异?

                  相关文章

                  最新文章

                  1. <i id='7uR3X'><tr id='7uR3X'><dt id='7uR3X'><q id='7uR3X'><span id='7uR3X'><b id='7uR3X'><form id='7uR3X'><ins id='7uR3X'></ins><ul id='7uR3X'></ul><sub id='7uR3X'></sub></form><legend id='7uR3X'></legend><bdo id='7uR3X'><pre id='7uR3X'><center id='7uR3X'></center></pre></bdo></b><th id='7uR3X'></th></span></q></dt></tr></i><div id='7uR3X'><tfoot id='7uR3X'></tfoot><dl id='7uR3X'><fieldset id='7uR3X'></fieldset></dl></div>
                  2. <small id='7uR3X'></small><noframes id='7uR3X'>

                    <tfoot id='7uR3X'></tfoot>

                      <bdo id='7uR3X'></bdo><ul id='7uR3X'></ul>

                      <legend id='7uR3X'><style id='7uR3X'><dir id='7uR3X'><q id='7uR3X'></q></dir></style></legend>