DataContractJsonSerializerOperationFormatter 不使用 JSON.NET 反序列化

tha*_*mai 4 c# wcf serialization datetime json-deserialization

我正在使用 WCF RESTful 网络服务,但出现此错误:

尝试反序列化参数http://tempuri.org/:aa 时出错 。InnerException 消息是“反序列化 WcfService1.Test 类型的对象时出错。DateTime 内容 '2014-05-31T18:30:00.000Z' 不以 '/Date(' 开头并以 ')/' 结尾,这是 JSON 所要求的。'。有关更多详细信息,请参阅 InnerException。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:`

在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader 阅读器,PartInfo 部分) 在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader 阅读器,PartInfo 部分) 在 System.ServiceModel.Dispatcher.DataContractionFormatter.DeserializeParameterPart(XmlDictionaryReader 阅读器,PartInfo 部分) 在 System.ServiceModel.Dispatcher.DataContractionFormatter[PartInfo 阅读器(XmlDictionaryReader 阅读器,PartInfo 部分)中] 部分、Object[] 参数、PartInfo returnInfo、Object& returnValue) 在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest) at

来自 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;
                    }
                });
Run Code Online (Sandbox Code Playgroud)

WCF服务:

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

测试类:

    public class Test
    {
         [JsonProperty(ItemConverterType=typeof(IsoDateTimeConverter))]
         public DateTime xaaaaaa { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

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

我曾尝试修改路由表设置,但无法找到大多数库。如果我使用 JSON.NET,它会说它默认使用 ISO 格式。我如何设置它以在 WCF Web 服务中使用它?

tha*_*mai 5

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

测试.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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }
});
Run Code Online (Sandbox Code Playgroud)

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

选择日期 : 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;
    }
Run Code Online (Sandbox Code Playgroud)

这很好用!