<legend id='bbjx3'><style id='bbjx3'><dir id='bbjx3'><q id='bbjx3'></q></dir></style></legend>
      • <bdo id='bbjx3'></bdo><ul id='bbjx3'></ul>

        <tfoot id='bbjx3'></tfoot>

        <small id='bbjx3'></small><noframes id='bbjx3'>

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

        如何使用 Newtonsoft.Json 将对象序列化为带有类型信息的 json?

        时间:2023-05-22

              <bdo id='1buWx'></bdo><ul id='1buWx'></ul>

            • <legend id='1buWx'><style id='1buWx'><dir id='1buWx'><q id='1buWx'></q></dir></style></legend>
                <tbody id='1buWx'></tbody>
            • <small id='1buWx'></small><noframes id='1buWx'>

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

                  <tfoot id='1buWx'></tfoot>
                  本文介绍了如何使用 Newtonsoft.Json 将对象序列化为带有类型信息的 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  当我序列化某些类型的对象时,我希望在 JSON 中有一个类型名称为类型的属性.我写了一个转换器:

                  I want to have a property with type name in JSON when I serialize objects of certain types. I wrote a converter:

                  public class TypeInfoConverter : JsonConverter {
                      private readonly IEnumerable<Type> _types;
                  
                      public TypeInfoConverter(IEnumerable<Type> types) {
                          Contract.Requires(types != null);
                  
                          _types = types;
                      }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
                          var jObject = JObject.FromObject(value, serializer);
                          jObject.AddFirst(new JProperty("Type", value.GetType().Name));
                          jObject.WriteTo(writer);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
                          return serializer.Deserialize(reader, objectType);
                      }
                  
                      public override bool CanConvert(Type objectType) {
                          return _types.Any(t => t.IsAssignableFrom(objectType));
                      }
                  }
                  

                  但是当我尝试序列化对象时,我在这里有一个无限递归:var jObject = JObject.FromObject(value, serializer); 这很明显,因为我使用了与该转换器配置的相同 JsonSerializer 实例.

                  But when I'm trying to serialize object I have an infinity recursion here: var jObject = JObject.FromObject(value, serializer); It is obvious because I use the same instance of JsonSerializer which was configured with that converter.

                  如何防止使用这个转换器,但是我想使用为这个序列化器配置的其他转换器?

                  How to prevent using this converter, but I want to use other converters which configured for this serializer?

                  我要序列化的类型:

                  public interface ITaskResult {
                  }
                  
                  public class UserHasRejectedOffer : ITaskResult {
                      public string Message { get; set; }
                  }
                  
                  public class UserHasFilledForm : ITaskResult {
                      public string FormValue1 { get; set; }
                  
                      public string Formvalue2 { get; set; }
                  }
                  
                  ...
                  

                  推荐答案

                  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
                      var converters = serializer.Converters.Where(x => !(x is TypeInfoConverter)).ToArray();
                  
                      var jObject = JObject.FromObject(value);
                      jObject.AddFirst(new JProperty("Type", value.GetType().Name));
                      jObject.WriteTo(writer, converters);
                  }
                  

                  这篇关于如何使用 Newtonsoft.Json 将对象序列化为带有类型信息的 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Newtonsoft Json.net - 如何序列化流的内容? 下一篇:JSON.NET 如何删除节点

                  相关文章

                      <tfoot id='pt9NM'></tfoot>
                      • <bdo id='pt9NM'></bdo><ul id='pt9NM'></ul>
                      <legend id='pt9NM'><style id='pt9NM'><dir id='pt9NM'><q id='pt9NM'></q></dir></style></legend>

                      <small id='pt9NM'></small><noframes id='pt9NM'>

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