Is it possible to set JSON.Net such that if you want to serialize a DateTime, it is serialized to UTC format. When deserializing from a string, it is deserialized to 'LocalTime'. For example:
2012-01-01T16:00:00Z2012-01-01T15:00:00Z2012-01-01T16:00:00Z -> Deserialized to 01/01/2012 17:00 +1GMT (Kind = Local)2012-01-01T15:00:00Z -> Deserialized to 01/01/2012 16:00 +1GMT (Kind = Local)However, I would like this to happen 'automatically', and not having to specify it with setttings each time - Like a global setting. Is this possible?
In ASP.NET WebAPI, it is possible. Try this:
// Converters will be userd during serialization (override DateTimeZoneHandling)
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AdjustToUniversal });
// DateTimeZoneHandling will be effective during deserialization
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With