Must.Tek
Must.Tek

Reputation: 1370

WCF Rest Api Datetime JSON Serialization Issue

When i send the Javascript Date type to WCF Method as parameter, serialization problem occurs at the following. (Whereas they are both working with JSON).

DateTime content '2017-05-04T13:09:11.737Z' does not start with '\/Date(' and end with ')\/' as required for JSON.'. 

What is the best way to solve this serialization issue? It seems it may solve with string manipulation but Is there any generic way for all WCF Methods that contains type of DateTime? (Client side or Server side)

This is test Angular Http post

this.http.post(myTestDateUrl, JSON.stringify( {date : new Date()} ) , {headers: this.getHeaders()})
     .subscribe(
        data => {
          console.log(data);
        },
        error => {
          console.log(error);
        }
      );

private getHeaders() {

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return headers;
  }

This is WCF Test Method

[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
public Stream TestDate(DateTime date)
{
    try
    {
        dynamic result = new { status = "OK"};
        return Send(result);
    }
    catch (Exception ex)
    {
        dynamic result = new { status = "ERROR", error = ex.Message };
        return Send(result);
    }
}

Upvotes: 1

Views: 2177

Answers (2)

Must.Tek
Must.Tek

Reputation: 1370

i have handled this problem at client side with following converter.

export class DateConverter{

    static convertToWCFFormat(date:Date):string{
        return "\/Date(" + date.getTime()+date.getTimezoneOffset() + ")\/";
    }

     static convertToWCFFormatFromStr(date:string):string{
         let _date=new Date(date);
        return "\/Date(" + _date.getTime()+_date.getTimezoneOffset() + ")\/";
    }

    static converToISOFormat(dateStr:string):Date{
        return new Date(parseInt(dateStr.substr(6)));
    }
}

Upvotes: 1

Mohammad
Mohammad

Reputation: 2764

i suggest use ISO 8601 format according this:

There is no right format; The JSON specification does not specify a format for exchanging dates which is why there are so many different

ways to do it.

The best format is arguably a date represented in 8601 format; it is a well known and widely used format and can be handled across many

different languages, making it very well suited for interoperability. If you have control over the generated json, for example, you provide data to other systems in json format, choosing 8601 as the date interchange format is a good choice.

so you can create something like this:

    public Stream TestDate(string date)
    {
        try
        {
            DateTime d;
            DateTime.TryParseExact(date, @"yyyy-MM-dd\THH:mm:ss\Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d);
            dynamic result = new { status = "OK" };
            return Send(result);
        }
        catch (Exception ex)
        {
            dynamic result = new { status = "ERROR", error = ex.Message };
            return Send(result);
        }
    }

Upvotes: 1

Related Questions