0

I have 2 sections in my api. One is for newer stuff which I would like to keep outputs as modern/standard as possible. I also have a legacy section that has to talk with old programs, which I can not modify.

The old programs are expecting dates to be in this format.

"EndDateActual":"\/Date(1398272400000+0000)\/"

Currently I'm sending the Date object in this format.

"EndDateActual":"2014-04-23T17:00:00Z"

The dates are being returned from the stored procedure as a string rather than DateTime.

Which is being passed into a Task object that is decorated with the[DataMember] [DataContract] properties.

I'm passing everything back as an IEnumerable with the String that is actually a date being one propriety of the Task object.

[DataContract()]
public class Task
{
    [DataMember(Order = 1)]
    public Guid Id { get; set; }

    [DataMember(Order = 2)]
    public string StartDateActual { get; set; }

    [DataMember(Order = 3)]
    public string StartDateProjected { get; set; }

    [DataMember(Order = 4)]
    public string EndDateActual { get; set; }

    [DataMember(Order = 5)]
    public string EndDateProjected { get; set; } 

I haven't managed to get a sterilizer to work yet. However, will it work due to the EndDateActual being a string?

If possible I would like to make it work this way for the legacy objects and the standard way for the rest of the api, which I am guessing is the default so I would like to avoid Global options. However, I am still new to all this so please correct me if I am wrong.

Thanks in advance!

Kurohoshi
  • 103
  • 3
  • 12
  • How do you convert a date to that number? – DavidG Aug 19 '14 at 15:32
  • @DavidG I think that is what I need to figure out. I do know that number is the milliseconds since the beginning of the Unix Epoch with a time zone. http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx – Kurohoshi Aug 19 '14 at 15:39
  • In that case you need some custom formatting: http://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c – DavidG Aug 19 '14 at 15:41

1 Answers1

0

You need to update the Json.NET's configuration so that it doesn't use the default ISO date converter:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

You'll then need to pass these settings to the application's configuration, for example:

GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);
elolos
  • 4,310
  • 2
  • 28
  • 40
  • I tried to use this, however I don't know where to put the top code and when I put the second block in the Global config the JsonNetFormatter isn't found. What am I missing? Also, will this work if the object is a string? – Kurohoshi Aug 20 '14 at 14:19