ASP.Net Core 2.2 allows to set serializer settings using MvcJsonOptions.SerializerSettings
property. The problem is that it affects both input and output. Is there
Finally found this workaround: https://github.com/aspnet/Mvc/issues/4562#issuecomment-226100352
public class CustomSerializerSettingsSetup : IConfigureOptions
{
private readonly ILoggerFactory _loggerFactory;
private readonly ArrayPool _charPool;
private readonly ObjectPoolProvider _objectPoolProvider;
public CustomSerializerSettingsSetup(
ILoggerFactory loggerFactory,
ArrayPool charPool,
ObjectPoolProvider objectPoolProvider)
{
_loggerFactory = loggerFactory;
_charPool = charPool;
_objectPoolProvider = objectPoolProvider;
}
public void Configure(MvcOptions options)
{
options.OutputFormatters.RemoveType();
options.InputFormatters.RemoveType();
options.InputFormatters.RemoveType();
var outputSettings = new JsonSerializerSettings();
options.OutputFormatters.Add(new JsonOutputFormatter(outputSettings, _charPool));
var inputSettings = new JsonSerializerSettings();
var jsonInputLogger = _loggerFactory.CreateLogger();
options.InputFormatters.Add(new JsonInputFormatter(
jsonInputLogger,
inputSettings,
_charPool,
_objectPoolProvider));
var jsonInputPatchLogger = _loggerFactory.CreateLogger();
options.InputFormatters.Add(new JsonPatchInputFormatter(
jsonInputPatchLogger,
inputSettings,
_charPool,
_objectPoolProvider));
}
}
and
services.TryAddEnumerable(
ServiceDescriptor.Transient, CustomSerializerSettingsSetup>());
in service provider configuration