Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
The project I'm working on right now is using Json.NET in four places: Web API server, Web API client, ViewResult from MVC and HtmlHelper extension for MVC views.
The problem is I'm using custom serialization settings and I have to make sure each place uses the same settings. While configuring all these places to use the same JsonSerializerSettings was simple enough, it made me wish I could do it once and then not have to think about it anymore.
Also configuring each place individually requires you to know how it is done in each different library. For example the way to customize Json.NET in Web API and SignalR are completely different.
SignalR - https://github.com/SignalR/SignalR/wiki/Extensibility#replacing-the-ijsonserializer
To solve this issue I want to add the ability to set global default settings. Set once with JsonConvert.DefaultSettings, the default settings would automatically be used by all calls to JsonConvert.SerializeObject/DeserializeObject, and JToken.ToObject/FromObject. Any user supplied settings to these calls would override the default settings. If someone decided they want to configure some default settings then it would still be possible to customize SignalR using its current configuration functional if required.
JsonSerializers created from the constructor or with JsonSerializer.Create(settings) would not use DefaultSettings to provide the option for library authors to opt-out if they don't want users to ever change how they serialize JSON. Library authors who do want users to be able to set default settings (i.e. Web API, SignalR, RavenDB) would call new factory methods JsonSerializer.CreateDefault() or JsonSerializer.CreateDefault(settings) when creating a JsonSerializer.
All calls to SerializeObject/DeserializeObject, FromObject/ToObject and any libraries that create their serializer with JsonSerializer.CreateDefault() will automatically indent JSON and serialize enums to their string name. The developer no longer needs to worry about JSON configuration.
I'm pretty certain that developers being able to set default settings once and have all their code and libraries using Json.NET pick them up is something people will find useful.
What I'm wondering is are there security problems with being able to set default settings globally? All the popular libraries that use Json.NET already provide hooks to customize their JSON serialization so that doesn't seem like a big deal. Additionally to be sure I've made it so you have to opt-in when creating a JsonSerializer.
Thoughts? Are there any issues I haven't though of? Do my names suck?