如何指定用于给定客户端上的所有 Restsharp 请求的 DateFormat?
我的休息服务使用 ISO 8601 作为所有请求和响应的日期格式。有没有办法在 RestClient
上设置它,或者我需要在每个单独的 RestRequest
上设置它?
My rest service uses ISO 8601 as the date format for all requests and responses. Is there a way to set this on the RestClient
or do I need to set it on each individual RestRequest
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来没有任何内置的能力可以做到这一点。 Newtonsoft.Json 提供了一个 IsoDateTimeConverter ,可以完成您正在寻找的序列化。为了使用它进行序列化,我认为您需要在自己的 ISerializer 中复制 RestSharp.Serializers.JsonSerializer ,该 ISerializer 的功能几乎与 RestSharp 版本完全相同,但在创建的实例中调整 NewtonSoft.Json.JsonSerializer.Converters 属性<一href="https://github.com/restsharp/RestSharp/blob/master/RestSharp/Serializers/JsonSerializer.cs#L44" rel="nofollow">此处 并添加/替换日期/时间转换器。
就反序列化而言,您应该能够在新的 JsonDeserializer 上设置 IDeserializer.DateFormat 属性并将其设置为处理程序:
It doesn't look like there's any built in ability to do so. Newtonsoft.Json provides an IsoDateTimeConverter that can do the serialization you are looking for. In order to use it for serialization, I think you would need to mostly duplicate the RestSharp.Serializers.JsonSerializer in your own ISerializer that does almost exactly what the RestSharp version does, but tweaksthe NewtonSoft.Json.JsonSerializer.Converters property in the instance created here and adds/replaces the date/time converter.
As far as deserialization, you should be able to set the IDeserializer.DateFormat property on a new JsonDeserializer and setting that as your handler: