-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Tweak configuration of JsonSerializerOptions #49875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,8 @@ public class JsonOptions | |
|
||
// The JsonSerializerOptions.GetTypeInfo method is called directly and needs a defined resolver | ||
// setting the default resolver (reflection-based) but the user can overwrite it directly or by modifying | ||
// the TypeInfoResolverChain | ||
TypeInfoResolver = JsonSerializer.IsReflectionEnabledByDefault ? CreateDefaultTypeResolver() : null | ||
// the TypeInfoResolverChain. Use JsonTypeInfoResolver.Combine() to produce an empty TypeInfoResolver. | ||
TypeInfoResolver = JsonSerializer.IsReflectionEnabledByDefault ? CreateDefaultTypeResolver() : JsonTypeInfoResolver.Combine() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When trying to serialize with |
||
}; | ||
|
||
// Use a copy so the defaults are not modified. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should try setting a resolver here, otherwise the singleton itself would be susceptible to races as multiple threads attempt to initialize it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need to set the resolver here.
The JsonSerializerOptions and JsonTypeInfoResolver get
lazyinitialized in JsonOptions here:aspnetcore/src/Http/Http.Extensions/src/JsonOptions.cs
Lines 27 to 34 in 9427854
when
IsReflectionEnabledByDeafult=false
, the Resolver may be null. We should fix the thread safety concerns all up (RDG, RDF, and MVC) with #49849.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would depend on how
DefaultSerializerOptions
is defined. If it contains a resolver it would get picked up by the copy constructor.Apropos, does the singleton need to be
JsonOptions
here. Seems it might as well be aJsonSerializerOptions
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DefaultSerializerOptions
doesn't initialize a resolver AFAICT (it's justnew JsonSerlizerOptions(JsonDefaults.Web
) so I think being explicit here helps.True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is part of the above linked code. I missed a couple lines.
aspnetcore/src/Http/Http.Extensions/src/JsonOptions.cs
Lines 16 to 34 in 9427854
It should be using the
DefaultSerializerOptions
that is in JsonOptions because this is where our default JsonSerializerOptions is defined. We shouldn't be defining a new one here in generated code.