Question

I'm working on a iOS application, that used to work using WCF. We're changing this product to use MVC Web API instead of WCF. I'm facing a problem with the dates! they must bebin JSON format such like

 /Date(1373476260000-0600)/

But what is returned actually is of this format

/Date(1379484000000)/

which is not accepted by iOS controller and produces the default date value (like if it's null and it's just initializing it to the default value (12/31/1969))

I've tried to parse the date to the wanted JSON format date string, but it resulted in an exception because it's expecting a DateTime object instead.

I've also tried to add the following line:

GlobalConfiguration.Configuration.Formatters.JsonFormatter. SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset;

to the WebApiConfig.cs file but it's not working, then I've tried to add it to the AttributeRoutingHttpConfig.cs file then to the Global.asax but no response!

Then I've tried:

    var appXmlType = GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

also added them to the 3 files mentioned above but it didn't work!

Any ideas how to solve this?

P.S: I only have access to the Web API code! I can't alter the iOS code!

Thanks.

Was it helpful?

Solution

First, be sure you have set:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
    .DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

Otherwise you will get the ISO8601 format instead of the Microsoft format. (ISO8601 is much better, but you said you can't change the iOS app.)

Then, you need to realize that for DateTime values, the .Kind has an effect on how the serialization works. If you have one with DateTimeKind.Utc, then it will not contain an offset because that's how this particular format works.

If you want to ensure that an offset is always produced, then use the DateTimeOffset value instead. This will provide an offset of +0000 for UTC.

For example:

var settings = new JsonSerializerSettings
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                };

var dt = DateTimeOffset.UtcNow;
var json = JsonConvert.SerializeObject(dt, settings);
Debug.WriteLine(json); // "\/Date(1383153418477+0000)\/"

But you need to be very careful with this approach that all consumers honor the offset. For example, if a client receives this and parses it into a DateTime using WCF's DataContractJsonSerializer, there's a known bug that any offset will be treated as if it was the local time of the receiving computer, regardless of what the value of that offset actually is.

If at all possible, you should switch over both the server and the application to use ISO8601 formatting instead.

OTHER TIPS

It seems the time zone portion of the date gets lost - try setting this in WebApiConfig:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top