dotnet / dotnet/aspnetcore

Dotnet 8 JsonSerializerOptions Breaking change

Open
#55,692 17 comments 12 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.