You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Expose a JsonSerializerOptions.Default property #61093
JsonSerializerOptions currently exposes an internal s_defaultOptions instance for use by System.Text.Json. This is useful since JsonSerializerOptions holds the reflection-based serialization metadata cache, as such creating a default new JsonSerializerOptions() instance can be very expensive since it forces the regeneration of that cache.
I propose we make this singleton a public property so that users can read default configuration cheaply (e.g. the default JsonConverter for a requested type).
Here's an (artificial) example of how the property can be used to cheaply recover the default converter used by STJ for integers:
publicclassMyCustomConverter:JsonConverter<int>{privatereadonlystaticJsonConverter<int>s_defaultConverter=(JsonConverter<int>)JsonSerializerOptions.Default.GetConverter(typeof(int));// custom serialization logicpublicoverridevoidWrite(Utf8JsonWriterwriter,intvalue,JsonSerializerOptionsoptions){returnwriter.WriteStringValue(value.ToString());}// fall back to default deserialization logicpublicoverrideintRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){returns_defaultConverter.Read(refreader,typeToConvert,options);}}
It would not be. Moreover the implementation of this property would need to make sure that the instance is locked for modification before being returned.
Activity
TonyValenti commentedon Nov 2, 2021
Would the defaults be mutable? My recommendation is "No".
eiriktsarpalis commentedon Nov 2, 2021
It would not be. Moreover the implementation of this property would need to make sure that the instance is locked for modification before being returned.
terrajobst commentedon Nov 2, 2021
Video