• <legend id='yQp6i'><style id='yQp6i'><dir id='yQp6i'><q id='yQp6i'></q></dir></style></legend>

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

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

        如何添加元数据来描述 JSON.Net 中哪些属性是日期

        How to add metadata to describe which properties are dates in JSON.Net(如何添加元数据来描述 JSON.Net 中哪些属性是日期)
      1. <tfoot id='GSa3w'></tfoot>

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

                • <small id='GSa3w'></small><noframes id='GSa3w'>

                • 本文介绍了如何添加元数据来描述 JSON.Net 中哪些属性是日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想在我的 json 中添加一个元数据属性,以便客户端可以知道哪些属性是日期.

                  I would like to add a metadata property to my json so that the client side can know what properties are dates.

                  例如,如果我有这样的对象:

                  For example if I had an object like this:

                  {
                    "notADate": "a value",
                    "aDate": "2017-04-23T18:25:43.511Z",
                    "anotherDate": "2017-04-23T18:25:43.511Z"
                  }
                  

                  我想添加一个元数据属性来告诉消费者哪些属性被视为日期,如下所示:

                  I would like to add a metadata property to tell the consumer which properties to treat as dates something like this:

                  {
                    "_date_properties_": ["aDate", "anotherDate"],
                    "notADate": "a value",
                    "aDate": "2017-04-23T18:25:43.511Z",
                    "anotherDate": "2017-04-23T18:25:43.511Z"
                  }
                  

                  任何帮助都会很棒,谢谢!

                  Any help would be great, thanks!

                  推荐答案

                  你可以创建一个 自定义 ContractResolver 将合成 "_date_properties_" 属性插入到每个序列化对象的合约中.

                  You could create a custom ContractResolver that inserts a synthetic "_date_properties_" property into the contract of every object that is serialized.

                  为此,首先子类 DefaultContractResolver 允许在应用程序添加的事件处理程序创建合同后流畅地自定义合同:

                  To do this, first subclass DefaultContractResolver to allow contracts to be fluently customized after they have been created by application-added event handlers:

                  public class ConfigurableContractResolver : DefaultContractResolver
                  {
                      readonly object contractCreatedPadlock = new object();
                      event EventHandler<ContractCreatedEventArgs> contractCreated;
                      int contractCount = 0;
                  
                      void OnContractCreated(JsonContract contract, Type objectType)
                      {
                          EventHandler<ContractCreatedEventArgs> created;
                          lock (contractCreatedPadlock)
                          {
                              contractCount++;
                              created = contractCreated;
                          }
                          if (created != null)
                          {
                              created(this, new ContractCreatedEventArgs(contract, objectType));
                          }
                      }
                  
                      public event EventHandler<ContractCreatedEventArgs> ContractCreated
                      {
                          add
                          {
                              lock (contractCreatedPadlock)
                              {
                                  if (contractCount > 0)
                                  {
                                      throw new InvalidOperationException("ContractCreated events cannot be added after the first contract is generated.");
                                  }
                                  contractCreated += value;
                              }
                          }
                          remove
                          {
                              lock (contractCreatedPadlock)
                              {
                                  if (contractCount > 0)
                                  {
                                      throw new InvalidOperationException("ContractCreated events cannot be removed after the first contract is generated.");
                                  }
                                  contractCreated -= value;
                              }
                          }  
                      }
                  
                      protected override JsonContract CreateContract(Type objectType)
                      {
                          var contract = base.CreateContract(objectType);
                          OnContractCreated(contract, objectType);
                          return contract;
                      }
                  }
                  
                  public class ContractCreatedEventArgs : EventArgs
                  {
                      public JsonContract Contract { get; private set; }
                      public Type ObjectType { get; private set; }
                  
                      public ContractCreatedEventArgs(JsonContract contract, Type objectType)
                      {
                          this.Contract = contract;
                          this.ObjectType = objectType;
                      }
                  }
                  
                  public static class ConfigurableContractResolverExtensions
                  {
                      public static ConfigurableContractResolver Configure(this ConfigurableContractResolver resolver, EventHandler<ContractCreatedEventArgs> handler)
                      {
                          if (resolver == null || handler == null)
                              throw new ArgumentNullException();
                          resolver.ContractCreated += handler;
                          return resolver;
                      }
                  }
                  

                  接下来,创建一个扩展方法以将所需的属性添加到 JsonObjectContract:

                  Next, create an extension method to add the desired property to a JsonObjectContract:

                  public static class JsonContractExtensions
                  {
                      const string DatePropertiesName = "_date_properties_";
                  
                      public static void AddDateProperties(this JsonContract contract)
                      {
                          var objectContract = contract as JsonObjectContract;
                          if (objectContract == null)
                              return;
                          var properties = objectContract.Properties.Where(p => p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?)).ToList();
                          if (properties.Count > 0)
                          {
                              var property = new JsonProperty
                              {
                                  DeclaringType = contract.UnderlyingType,
                                  PropertyName = DatePropertiesName,
                                  UnderlyingName = DatePropertiesName,
                                  PropertyType = typeof(string[]),
                                  ValueProvider = new FixedValueProvider(properties.Select(p => p.PropertyName).ToArray()),
                                  AttributeProvider = new NoAttributeProvider(),
                                  Readable = true,
                                  Writable = false,
                                  // Ensure // Ensure PreserveReferencesHandling and TypeNameHandling do not apply to the synthetic property.
                                  ItemIsReference = false, 
                                  TypeNameHandling = TypeNameHandling.None,
                              };
                              objectContract.Properties.Insert(0, property);
                          }
                      }
                  
                      class FixedValueProvider : IValueProvider
                      {
                          readonly object properties;
                  
                          public FixedValueProvider(object value)
                          {
                              this.properties = value;
                          }
                  
                          #region IValueProvider Members
                  
                          public object GetValue(object target)
                          {
                              return properties;
                          }
                  
                          public void SetValue(object target, object value)
                          {
                              throw new NotImplementedException("SetValue not implemented for fixed properties; set JsonProperty.Writable = false.");
                          }
                  
                          #endregion
                      }
                  
                      class NoAttributeProvider : IAttributeProvider
                      {
                          #region IAttributeProvider Members
                  
                          public IList<Attribute> GetAttributes(Type attributeType, bool inherit) { return new Attribute[0]; }
                  
                          public IList<Attribute> GetAttributes(bool inherit) { return new Attribute[0]; }
                  
                          #endregion
                      }
                  }
                  

                  最后,将您的示例类型序列化如下:

                  Finally, serialize your example type as follows:

                  var settings = new JsonSerializerSettings
                  {
                      ContractResolver = new ConfigurableContractResolver
                      {
                          // Here I am using CamelCaseNamingStrategy as is shown in your JSON.
                          // If you don't want camel case, leave NamingStrategy null.
                          NamingStrategy = new CamelCaseNamingStrategy(),
                      }.Configure((s, e) => { e.Contract.AddDateProperties(); }),
                  };
                  
                  var json = JsonConvert.SerializeObject(example, Formatting.Indented, settings);
                  

                  此解决方案仅处理静态类型的 DateTimeDateTime? 属性.如果您有 object 值属性,这些属性有时具有 DateTime 值,或 Dictionary<string, DateTime> 或 扩展数据 包含 DateTime 值,您将需要更复杂的解决方案.

                  This solution only handles statically typed DateTime and DateTime? properties. If you have object-valued properties that sometimes have DateTime values, or a Dictionary<string, DateTime>, or extension data containing DateTime values, you will need a more complex solution.

                  (作为替代实现,您可以改为子类 DefaultContractResolver.CreateObjectContract 并使用 JsonContractExtensions.AddDateProperties() 硬编码所需的属性,但是我认为创建一个通用的、可流畅配置的合同解析器会更有趣,以防万一以后有必要插入不同的自定义项.)

                  (As an alternative implementation, you could instead subclass DefaultContractResolver.CreateObjectContract and hardcode the required properties there using JsonContractExtensions.AddDateProperties(), however I thought it would be more interesting to create a general-purpose, fluently configurable contract resolver in case it becomes necessary to plug in different customizations later.)

                  您可能希望缓存合约解析器以获得最佳性能.

                  You may want to cache the contract resolver for best performance.

                  示例 .Net fiddle.

                  这篇关于如何添加元数据来描述 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 自定义合约序列化和集合)

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

                    <tbody id='Be9tF'></tbody>
                    <bdo id='Be9tF'></bdo><ul id='Be9tF'></ul>
                    <legend id='Be9tF'><style id='Be9tF'><dir id='Be9tF'><q id='Be9tF'></q></dir></style></legend>

                  • <tfoot id='Be9tF'></tfoot>

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