1

currently I'm struggling a little bit with the .NET MVC JSON parser. I've got a model which contains several DateTime properties. This model is POSTed to the server who tries to parse it.

The string which represents the DateTime is perfectly ISO8601 formatted. Example:

2018-11-14T19:14:20.858

or

2018-11-14T18:14:20.858Z

Obviously, the default MVC JSON parser uses DateTime.Parse() without any other parameters. The first example results in a DateTime with Kind = Unspecified while the second one, which should be UTC, results in Kind = Local. Both is not correct.

On the other hand, as soon as I use

DateTime.Parse("...", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)

the result is absolutely perfect.

Now my question to you: Is there a way to either

  • Tell the default MVC JSON parser to use DateTimeStyles.RoundtripKind to parse DateTime
  • Completely replace the MVC JSON parser by e.g. Newtonsoft.JSON

I'm totally fine with either of the solutions! I'm already using Newtonsoft to serialize my data and ensure DateTime objects are smoothly serialized as ISO8601.

Thank's a lot in advance :)

mrsubwoof
  • 183
  • 1
  • 11
  • Refer [Setting the default JSON serializer in ASP.NET MVC](https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc) –  Nov 14 '18 at 20:28
  • "Completely replace the MVC JSON parser by e.g. Newtonsoft.JSON"...what version of MVC? Newtonsoft has been the default for a while now. – ADyson Nov 14 '18 at 21:30
  • @ADyson I guess it's MVC5. I didn't know, that Newtonsoft is already the default for MVC now ... can I then just replace the Converters for DateTime somehow? – mrsubwoof Nov 14 '18 at 21:55
  • you can check your package versions instead of guessing... – ADyson Nov 14 '18 at 21:58
  • Anyway, maybe try https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DateTimeZoneHandling.htm and see also https://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api – ADyson Nov 14 '18 at 22:04
  • @ADyson Thanks a lot for your help - unfortunately, setting the DateTimeZoneHandling to RoundtripKind in the Global.asax using GlobalConfiguration did not work. I'm not sure, if MVC (not MVC WebApi) uses the same configuration. I had to manually reference the WebApi assemblies, before I could access the GlobalConfiguration object. I'm using MVC 5.2.6 btw. – mrsubwoof Nov 15 '18 at 07:52
  • Sorry, I missed that that question was about web API, I was rushing last night! – ADyson Nov 15 '18 at 09:25
  • Looking around I don't see much in the way of resources about where to put such a configuration in MVC. Hmm. Maybe it would go in one of the files in the app_start folder. Sorry I'm guessing a bit now. Or maybe you could extend the JsonResult class or something, – ADyson Nov 15 '18 at 09:34
  • @ADyson I already figured out how to replace the JSON serializer when returning a result _from_ a controller, and that works perfectly fine. For the second step I need to know how to change the behavior for the parameters being sent via e.g. POST _to_ the controller. That's where I'm struggling ... – mrsubwoof Nov 15 '18 at 10:19
  • ah ok gotcha. You'd think it would use the same settings in both directions – ADyson Nov 15 '18 at 10:26
  • One step further, I figured out how to replace the parser using this code https://gist.github.com/zacg/8211036 which works fine. Unfortunately the custom converter I've added to the serializer does not call the ReadJson method (https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) – mrsubwoof Nov 15 '18 at 10:28

0 Answers0