【问题标题】:Rest Sharp is not serializing datesRest Sharp 没有序列化日期
【发布时间】:2014-11-12 16:59:32
【问题描述】:

当然,最新的 Restsharp 中的默认值应该能够序列化一个 `Datetime。

以下是我的请求构建

var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
//request.JsonSerializer = new MyJsonSerializer(); // When applying JSON.Net serialisation 
request.Resource = "SaveBooking";
request.AddHeader("Content-Type", "application/json");
request.AddBody(booking);

return Execute<bool>(request);

我已经尝试实现 JSON.NET Serializer 并与 restsharp as suggested in this question 集成,它链接到他们的笔记。但我也有同样的问题。

它正在构建的 JSON 如下:

 "BookingDate": "2014-11-13T09:31:02.0667967+00:00",
 "StartTime": "2014-11-13T10:30:00",
 "EndTime": "2014-11-13T17:00:00",

在尝试反序列化时,我得到的错误如下所示。

DateTime content '2014-11-12T16:20:30.4635576Z' does not start with '\/Date(' and end with ')\/' as required for JSON.'.

很明显它没有序列化日期。但我不知道为什么。或者如何解决它。

我正在反序列化其余端点

     [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveBooking")]
    Boolean SaveBooking(RoomBookingEntry Booking);

带参数

  public class RoomBookingEntry
{
    public DateTime BookingDate { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; } 
}

}

【问题讨论】:

  • 您收到的错误消息与您提供的 JSON 不匹配 - 是否有空格?你是如何反序列化的?
  • @JonSkeet 它们具有相同的符号,但 json 是从同一事物的早期调试中保存的。又名,时代不匹配。我正在反序列化,如我的问题所附。
  • 它们不一样——一个在日期/时间值中有空格(这很奇怪),另一个没有。值是否实际上看起来像"2014-11-12T16: 34: 16.9681998Z""2014-11-12T16:34:16.9681998Z"
  • @JonSkeet 哦,我现在明白了。好的,我把第一个放在 Json 格式化程序中。所以也许它被编辑得很糟糕。第二个肯定是正确的,因为这是我回来的错误。我会仔细检查。
  • 问题在于,在 JSON 中没有单一的“正确”方式来表示 DateTime。见hanselman.com/blog/…。我找不到任何说服 RestSharp 将 DateTime 序列化为 new Date(...) 格式的方法...您可能想看看是否可以说服 DataContractJsonSerializer 接受 ISO-8601 格式...

标签: c# json serialization json.net restsharp


【解决方案1】:

您可以告诉 JSON.Net 在序列化日期时使用正确的日期,如下所示:

var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
JsonConvert.SerializeObject(obj, settings);

默认情况下还有另一个 JSON 序列化库:https://www.nuget.org/packages/ServiceStack.Text/

var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.Resource = "SaveBooking";

var json = JsonSerializer.SerializeToString(booking);
request.AddParameter("application/json", json, ParameterType.RequestBody);

【讨论】:

    【解决方案2】:

    使用您的项目中可能已经拥有的 Newtonsoft.Json 序列化程序。您只需手动将参数添加到请求中,绕过有问题的 RestSharp 序列化程序。

    var request = new RestRequest(Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.Resource = "SaveBooking";
    request.AddHeader("Content-Type", "application/json");
    
    var settings = new JsonSerializerSettings() { DateFormatHandling=DateFormatHandling.MicrosoftDateFormat };
    var json = JsonConvert.SerializeObject(booking,settings);
    
    request.AddParameter("application/json",json,null,ParameterType.RequestBody);
    
    
    
    return Execute<bool>(request);
    

    【讨论】:

    • 这对我有用。还必须确保我的 [DataMember] 已在我的 Datetime 字段的 [DataContract] 中设置。
    【解决方案3】:

    正如 Jon Skeet 所提到的。他们期待不同的格式,所以不认识对方。

    使用此处找到的序列化程序: https://github.com/khalidabuhakmeh/Ducksboard/tree/master/Ducksboard/Serializers

    我已经能够让RestSharpDataContractJson 期望的格式发送datetime

    绑定使用:

       var request = new RestRequest(Method.POST);
       request.RequestFormat = DataFormat.Json;
    
       request.JsonSerializer = new RestSharpDataContractJsonSerializer();
    
       request.Resource = "SaveBooking";
       request.AddHeader("Content-Type", "application/json"); // May not be needed
       request.AddBody(booking);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2015-03-21
      • 2021-03-07
      • 2020-04-21
      相关资源
      最近更新 更多