5

Facts:

  1. In SQL Server there is the column type datetime and date
  2. Using EF, both datetime and date are mapped to C#'s DateTime
  3. In my datetime columns I'm always storing the UTC date.
  4. In my current database there are no date columns, but I need to use it in a new table I'm going to have
  5. My typical data flow is: SQL Server --> C# (server side) --> JSON (client side)

I've set my JSON Serializer to always return DateTime variables to the client with the format yyyy-MM-ddThh:mm:ssZ, which is a good format for JavaScript. The Z at the end ("zulu") declares that it is in UTC timezone and so my client app knows that it needs to fix the timezone at display. Until now, everything needed to be timezone-fixed at display (because everything was UTC)

Because I want to start using the SQL Server type date, that Z at the end will be bothering in those date cases (but still needed in those datetime cases). As everything in my serverside is DateTime, I'm not sure how to make the JSON Serializer know which kind of DateTime is he looking at...

My question is... is it possible for the JsonSerializerSettings to know if he's converting a DateTime that came from a date or a datetime? If not, what's a good approach to solve this?

This is my Serializer:

public class JsonNetResult : ActionResult
{
    public Encoding ContentEncoding { get; set; }
    public string ContentType { get; set; }
    public object Data { get; set; }

    public JsonSerializerSettings SerializerSettings { get; set; }
    public Formatting Formatting { get; set; }

    public JsonNetResult()
    {
        var isoDateTimeConverter = new IsoDateTimeConverter();
        isoDateTimeConverter.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";

        SerializerSettings = new JsonSerializerSettings();
        SerializerSettings.Converters.Add(isoDateTimeConverter);
    }

    public override void ExecuteResult(ControllerContext context)
    {
          ...
    }
}

And this is how I use it:

return new JsonNetResult()
{
    Data = new
    {
        persons = dbContext.Persons.Select(e => new 
        {
             name = e.Name,
             someDate1 = e.SomeDate1, // datetime
             someDate2 = e.SomeDate2  // date
        })
    }
};
sports
  • 7,851
  • 14
  • 72
  • 129
  • Can you apply Json.NET attributes to your model? You could apply a different subclass of `IsoDateTimeConverter` to `date` properties – dbc Apr 02 '17 at 16:41
  • Didn't thought of that, can you put an example? – sports Apr 02 '17 at 16:48
  • 1
    Found an example `OnlyDateConverter` from [Dates without time in ASP.NET Web API's JSON output](https://stackoverflow.com/q/16320762/3744182). – dbc Apr 02 '17 at 16:52
  • I will try it and post back the result. Looks promising @dbc – sports Apr 02 '17 at 16:53
  • The attribute solution wouldn't work for me because of how I serialize directly from projections: `(e => new { someDate1 = e.SomeDate1, someDate2 = e.SomeDate2 })` ...as you can see I'm not serializing my `Person` object. I'm serializing the projection itself (a `dynamic` object) – sports Apr 05 '17 at 16:42

0 Answers0