Global Json.net Settings

Is there a way to specify global settings for Json.net?

The problem we are facing is that it puts all DateTimes in UTC (correctly). For obsolete purposes, we want to use Local Time by default. I do not want to post the following code everywhere:

var settings = New JsonSerializerSettings(); settings.DateTimeZoneHandling = DateTimeZoneHandling.Local; JsonConvert.DeserializeObject(json, settings); 
+7
source share
2 answers

So this was added in Json.net 5.0 Release 5

 JsonConvert.DefaultSettings = () => new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local }; 
+12
source

Yes, you can configure the default Json.Net settings, as Lodewijk explained. But the web API uses its own settings, and you have to set them separately.

Web API (.NET Core)

 services.AddMvc(opts => { var jsonFormatter = (JsonOutputFormatter) opts.OutputFormatters .First(formatter => formatter is JsonOutputFormatter); jsonFormatter.PublicSerializerSettings.Converters.Add(new StringEnumConverter()); }); 

Web Interface (.NET Framework)

 var config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SerializerSettings.Converters .Add(new StringEnumConverter()); 
+1
source

All Articles