How to globally set default options for System.Text.Json.JsonSerializer? How to globally set default options for System.Text.Json.JsonSerializer? json json

How to globally set default options for System.Text.Json.JsonSerializer?


You can create an extension method. Here's an example

I use separate methods vs having to build special settings, so that all the settings will be in a single spot and easily reusable.

public static class DeserializeExtensions{    private static JsonSerializerOptions defaultSerializerSettings = new JsonSerializerOptions();    // set this up how you need to!    private static JsonSerializerOptions featureXSerializerSettings = new JsonSerializerOptions();    public static T Deserialize<T>(this string json)    {               return JsonSerializer.Deserialize<T>(json, defaultSerializerSettings);    }    public static T DeserializeCustom<T>(this string json, JsonSerializerOptions settings)    {        return JsonSerializer.Deserialize<T>(json, settings);    }    public static T DeserializeFeatureX<T>(this string json)    {        return JsonSerializer.Deserialize<T>(json, featureXSerializerSettings);    }}

Then you call it as a method on a string, whether literal or a variable.

    Car result = @"{""Wheels"": 4, ""Doors"": 2}".DeserializeFeatureX<Car>();


No, JsonSerializerOptions does not expose the default options. If you are using a particular web framework there may be a way to specify (de-)serialization settings through that. Otherwise, I suggest creating your own convenience methods.

See also this open issue.


The default options are not exposed in JsonSerializer for .NET Core 3.1. However, as of December, 2019 this has been added to the road map for 5.0.

The release of .NET 5.0 is expected November, 2020. But there's no guarantee this particular issue will be addressed at any particular time. Other than waiting, these answers suggest workarounds:

Also, I packaged my convenience extension methods, inspired by @ps2goat's answer and put them on nuget.org and github: