-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Ability to have different JsonSerializerSettings for input and output missing #4562
Comments
Curious what settings are you setting differently between input & output formatters? One of the reasons for both the input & output formatters to share the same settings is performance. Json.net serializer builds json contracts for types it serializes/deserializes. Having separate settings means having this cache in 2 places. You can new up the input and output formatters with your custom serializer settings and add them to the global formatters list. You can use |
In my case I have different TypeNameHandlig settings. I would expect that input and output are sufficiently different scenarios to warrant differences in settings. |
Newing up my own formatter instances poses the question: what to supply for the other constructor arguments. |
In general, you should set If you need to create non-default BTW what settings would you like to set differently for input and output formatters? |
TypeNameHandling and SerializationBinder need to be different in my case, as described above. I would think this is not an uncommon scenario. |
If you're worried about that then by default the input and output formatter could have the same instance of JsonSerializerSettings, and if a dev wants to they can set a different JsonSerializerSettings on the inputter/outputter then they can go ahead and do that. Also by default the contract resolver on JsonSerializerSettings is null so it won't override the default contract resolver and there won't be any perf hit for reflection over the same object multiple times. You will only get that situation if a user explicitly sets a new contract resolver on their JsonSerializerSettings. |
Putting in Backlog for now. We will reconsider this in a future release (we can easily add new properties that are backwards-compatible). |
I was going to open an issue exactly for that. My use-case is the reverse of @kwaclaw. |
I think these scenarios can be achieved today. public class CustomSerializerSettingsSetup : IConfigureOptions<MvcOptions>
{
private readonly ILoggerFactory _loggerFactory;
private readonly ArrayPool<char> _charPool;
private readonly ObjectPoolProvider _objectPoolProvider;
public CustomSerializerSettingsSetup(
ILoggerFactory loggerFactory,
ArrayPool<char> charPool,
ObjectPoolProvider objectPoolProvider)
{
_loggerFactory = loggerFactory;
_charPool = charPool;
_objectPoolProvider = objectPoolProvider;
}
public void Configure(MvcOptions options)
{
options.OutputFormatters.RemoveType<JsonOutputFormatter>();
options.InputFormatters.RemoveType<JsonInputFormatter>();
options.InputFormatters.RemoveType<JsonPatchInputFormatter>();
var outputSettings = new JsonSerializerSettings();
options.OutputFormatters.Add(new JsonOutputFormatter(outputSettings, _charPool));
var inputSettings = new JsonSerializerSettings();
var jsonInputLogger = _loggerFactory.CreateLogger<JsonInputFormatter>();
options.InputFormatters.Add(new JsonInputFormatter(
jsonInputLogger,
inputSettings,
_charPool,
_objectPoolProvider));
var jsonInputPatchLogger = _loggerFactory.CreateLogger<JsonPatchInputFormatter>();
options.InputFormatters.Add(new JsonPatchInputFormatter(
jsonInputPatchLogger,
inputSettings,
_charPool,
_objectPoolProvider));
}
} On your startup class you simply do this when configuring services: services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, CustomSerializerSettingsSetup>()); After you called AddMvc() |
One correction: Do not use |
@DougHu is correct, I was oversimplifying. |
- #4339: remove non-recommended JSON formatter constructors - affects `JsonInputFormatter`, `JsonOutputFormatter`, `JsonPatchInputFormatter` - `JsonOutputFormatter` cleanup also impacts `JsonHelper` - rename and make `SerializerSettingsProvider` class public; use it as appropriate - #4409: make `SerializerSetings` properties get-only and `protected` - affects `JsonInputFormatter`, `JsonOutputFormatter` Recommended patterns: - change `JsonSerializerSettings` values in `MvcJsonOptions` for almost all customizations - find `JsonOutputFormatter` in `MvcOptions.OutputFormatters` when limiting per-result formatters - start with `JsonSerializerSettingsProvider.CreateSerializerSettings()` when customizing a per-result formatter
@dougbu |
@weitzhandler either copy the properties of the |
Here you go, please participate, thanks. |
I'm not sure that there's anything left to do here, it's possible use different sets of settings for input and output as shown here #4562 (comment) Recommend Closing /cc @Eilon |
Until recently I was able to have different settings, but now the serializer settings on the Json formatters are not accessible anymore and I have to use AddJsonOptions(). This would be fine if MvcJsonOptions had different SerializerSettings for input and output.
What are the alternatives? Issue #4492 does not really explain it in a way that an "outsider" can easily understand.
The text was updated successfully, but these errors were encountered: