DataContractJsonSerializerOperationFormatter not deserializing using JSON.NET

I use the WCF RESTful web service and I get this error:

An error occurred while trying to deserialize the parameter http://tempuri.org/:aa . The InnerException message was "There was an error deserializing an object of type WcfService1.Test. DateTime content '2014-05-31T18: 30: 00.000Z' does not start with '/ Date (' and end with ') /', as required for JSON. '. See InnerException for more details. '. See Server Logs for more details. Trace exception stack: `

in the System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart (XmlDictionaryReader reader, Part PartInfo) in the System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter.DerminalDermoderatorDermoderatorDermoderatorDermodelter ] parts, Object [], PartInfo returnInfo, Object & returnValue) in System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore (XmlDictionaryReader reader, Object [], Boolean isRequest) in

Login from 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 Service:

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

Testing Class:

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

xaaaaaa : /Date(new Date.valueOf().toString())/, . WCF IsoDateFormat .

, . JSON.NET, , ISO. - WCF?

+4
1

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;
    }

!

+4

Source: https://habr.com/ru/post/1543550/


All Articles