aspnetcore: Dotnet 8 JsonSerializerOptions Breaking change
I have a Attribute to change JSON serialization from camelCase to PascalCase, which worked fine in dotnet 7, but in dotnet 8 i am getting an error System.InvalidOperationException: JsonSerializerOptions instance must specify a TypeInfoResolver setting before being marked as read-only
public class PascalCaseJson : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result is ObjectResult objectResult)
objectResult.Formatters.Add(new SystemTextJsonOutputFormatter(new JsonSerializerOptions()));
}
}
Adding TypeInfoResolver = new DefaultJsonTypeInfoResolver() fixes the issue, but i couldn’t find any documentation on this breaking change.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 12
- Comments: 17 (5 by maintainers)
The simplest fix for this is to assign the default resolver before passing it to the formatter like so:
Also upvoting this. Running into this in a standard ASP.NET Core Web API application that is not AOT/Trimmed.
Adding an upvote. I also ran into this in a standard ASP.NET Core razor pages application that is not AOT/Trimmed.
@eiriktsarpalis i am having this issue for a regular aspnet app, without trimming or Native AOT
Hi, I have the same issue with an isolated azure function using .net8. None of our projects have
<PublishTrimmed>true</PublishTrimmed>or<PublishAot>true</PublishAot>specified.This is happening to me as well. We just upgraded to .net 8. None of our projects have
<PublishTrimmed>true</PublishTrimmed>or<PublishAot>true</PublishAot>.