Add/Remove TypeInfoResolver to existing JsonSerializerOptions
Starting with .NET 8, the JsonSerializerOptions
class features a new TypeInfoResolverChain
property in addition to the existing TypeInfoResolver
property. With this new property, you are no longer required to specify all the resolvers in the same place, instead, you can add them later as needed.
Let’s look at an example:
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(
new ResolverA(),
new ResolverB()
);
};
options.TypeInfoResolverChain.Add(new ResolverC());
Code language: C# (cs)
Besides adding new type resolvers to an existing JsonSerializerOptions
, TypeInfoResolverChain
also allows you to remove type info resolvers from the serializer options.
options.TypeInfoResolverChain.RemoveAt(0);
Code language: C# (cs)
If you want to prevent changes to the type info resolver chain, you can do it by making the JsonSerializerOptions
instance read-only. That is done by calling the MakeReadOnly()
method on the options instance and will force the following InvalidOperationException
in case anyone attempts to modify the type info resolver chain after the fact.
Unhandled exception. System.InvalidOperationException: This JsonSerializerOptions instance is read-only or has already been used in serialization or deserialization.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerOptionsReadOnly(JsonSerializerContext context)
at System.Text.Json.JsonSerializerOptions.VerifyMutable()
at System.Text.Json.JsonSerializerOptions.OptionsBoundJsonTypeInfoResolverChain.OnCollectionModifying()
at System.Text.Json.Serialization.ConfigurationList`1.Add(TItem item)
Code language: plaintext (plaintext)
One Comment