DataContractJsonSerializerOperationFormatter不使用JSON.NET反序列化

时间:2014-06-06 11:28:28

标签: c# wcf datetime serialization json-deserialization

我正在使用WCF RESTful webservice,我收到此错误:

  

尝试反序列化参数时出错   http://tempuri.org/:aa。 InnerException消息是'有一个   错误反序列化WcfService1.Test类型的对象。约会时间   内容' 2014-05-31T18:30:00.000Z'不以' /日期开头('和   结束')/'根据JSON的要求。'。请参阅InnerException   更多细节。'。请参阅服务器日志以获取更多详异常堆栈   追踪是:`

     

在   System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader   读者,PartInfo部分)at   System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader   读者,PartInfo部分)at   System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader   reader,PartInfo [] parts,Object []参数,PartInfo returnInfo,   对象和放大器; returnValue)at   System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader   

中的reader,Object []参数,Boolean isRequest)

来自jQuery的输入:

          $.ajax({
                    url: "Restful.svc/new/one",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                    data: JSON.stringify({ aa: { xaaaaaa: "2014-05-31T18:30:00.000Z" } }),
                    dataType: 'json',
                    processData: true,
                    success: function (msg) {
                        alert(msg.helloWorldResult);
                    },
                    error: function (msg) {
                        var y = 0;
                    }
                });

WCF服务:

        [OperationContract]
        [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "new/one")]
        String helloWorld(Test aa);

测试类:

    public class Test
    {
         [JsonProperty(ItemConverterType=typeof(IsoDateTimeConverter))]
         public DateTime xaaaaaa { get; set; }
    }

如果我将输入xaaaaaa传递为:/Date(new Date.valueOf().toString())/它将接受。如何更改WCF服务中的默认日期格式化程序以使用IsoDateFormat进行序列化和反序列化。

我尝试修改路由表设置但是我找不到大多数库。如果我使用JSON.NET,它表示它默认使用ISO格式。如何将其设置为在WCF Web服务中获取它?

1 个答案:

答案 0 :(得分:4)

我正在使用Newtonsoft序列化日期格式以保存ISODate格式。我能够解决我的问题:

test.cs中:

[DataContract]
public class Test
{
    public DateTime xaaaaaa { get; set; }

    [DataMember(Name = "xaaaaaa")]
    private string HiredForSerialization { get; set; }

    [OnSerializing]
    void OnSerializing(StreamingContext ctx)
    {
        this.HiredForSerialization = JsonConvert.SerializeObject(this.xaaaaaa).Replace('"',' ').Trim();
    }

    [OnDeserialized]
    void OnDeserialized(StreamingContext ctx)
    {
        this.xaaaaaa = DateTime.Parse(this.HiredForSerialization);
    }

}

jQuery的:

$.ajax({
    url: "Transfer.svc/new/one",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
    },
    data: JSON.stringify({ aa: { xaaaaaa: "2014-05-31T18:30:00.000Z" } }),
    dataType: 'json',
    processData: true,
    success: function (msg) {
    tester = msg.helloWorldResult; //"2014-06-01T00:00:00+05:30"
    },
    error: function (msg) {
    var y = 0;
    }
});

我从日期选择器(jQuery)中选择的日期:

Selected date : 1st-Jun-2014

WCF服务看起来像这样:

    [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "new/one")]
    public Test helloWorld(Test aa)
    {
        return aa;
    }

这很棒!