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

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

          <bdo id='Lc5KU'></bdo><ul id='Lc5KU'></ul>

        JSON.NET 中基于属性的类型解析

        Property-based type resolution in JSON.NET(JSON.NET 中基于属性的类型解析)

          <tbody id='ELgWx'></tbody>

            <legend id='ELgWx'><style id='ELgWx'><dir id='ELgWx'><q id='ELgWx'></q></dir></style></legend>

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

                • <tfoot id='ELgWx'></tfoot>
                • <small id='ELgWx'></small><noframes id='ELgWx'>

                • 本文介绍了JSON.NET 中基于属性的类型解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  是否可以基于 JSON 对象的属性使用 JSON.NET 覆盖类型解析?基于现有的 API,看起来我需要一种接受 JsonPropertyCollection 并返回要创建的 Type 的方法.

                  Is it possible to override the type resolution using JSON.NET based on a property of the JSON object? Based on existing APIs, it looks like I need a way of accepting a JsonPropertyCollection and returning the Type to be created.

                  注意:我知道 TypeNameHandling 属性,但是添加一个 $type 属性.我无法控制源 JSON.

                  NOTE: I'm aware of the TypeNameHandling attribute, but that adds a $type property. I do not have control over the source JSON.

                  推荐答案

                  这似乎是通过创建自定义 JsonConverter 并在反序列化之前将其添加到 JsonSerializerSettings.Converters 中.

                  It would appear that this is handled by creating a custom JsonConverter and adding it to JsonSerializerSettings.Converters before deserialisation.

                  nonplus 在 JSON.NET 讨论板 在 Codeplex.我已修改示例以返回自定义 Type 并遵循默认创建机制,而不是当场创建对象实例.

                  nonplus has left a handy sample on the JSON.NET discussions board on Codeplex. I've modified the sample to return custom Type and deferring to the default creation mechanism, rather than creating the object instance on the spot.

                  abstract class JsonCreationConverter<T> : JsonConverter
                  {
                      /// <summary>
                      /// Create an instance of objectType, based properties in the JSON object
                      /// </summary>
                      protected abstract Type GetType(Type objectType, JObject jObject);
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return typeof(T).IsAssignableFrom(objectType);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, 
                          object existingValue, JsonSerializer serializer)
                      {
                          JObject jObject = JObject.Load(reader);
                  
                          Type targetType = GetType(objectType, jObject);
                  
                          // TODO: Change this to the Json.Net-built-in way of creating instances
                          object target = Activator.CreateInstance(targetType);
                  
                          serializer.Populate(jObject.CreateReader(), target);
                  
                          return target;
                      }
                  }
                  

                  这里是示例用法(也如上所述更新):

                  And here is the example usage (also updated as mentioned above):

                  class VehicleConverter : JsonCreationConverter<Vehicle>
                  {
                      protected override Type GetType(Type objectType, JObject jObject)
                      {
                          var type = (string)jObject.Property("Type");
                          switch (type)
                          {
                              case "Car":
                                  return typeof(Car);
                              case "Bike":
                                  return typeof(Bike);
                          }
                  
                          throw new ApplicationException(String.Format(
                              "The given vehicle type {0} is not supported!", type));
                      }
                  }
                  

                  这篇关于JSON.NET 中基于属性的类型解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <legend id='sivDt'><style id='sivDt'><dir id='sivDt'><q id='sivDt'></q></dir></style></legend>

                        <bdo id='sivDt'></bdo><ul id='sivDt'></ul>

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

                              <tbody id='sivDt'></tbody>
                            <tfoot id='sivDt'></tfoot>