@@ -18,10 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
18
18
public class JsonOutputFormatter : TextOutputFormatter
19
19
{
20
20
private readonly IArrayPool < char > _charPool ;
21
-
22
- // Perf: JsonSerializers are relatively expensive to create, and are thread safe. We cache
23
- // the serializer and invalidate it when the settings change.
24
- private JsonSerializer _serializer ;
21
+ private JsonSerializerSettings _serializerSettings ;
25
22
26
23
/// <summary>
27
24
/// Initializes a new <see cref="JsonOutputFormatter"/> instance.
@@ -121,12 +118,12 @@ protected virtual JsonWriter CreateJsonWriter(TextWriter writer)
121
118
/// <returns>The <see cref="JsonSerializer"/> used during serialization and deserialization.</returns>
122
119
protected virtual JsonSerializer CreateJsonSerializer ( )
123
120
{
124
- if ( _serializer == null )
121
+ if ( _serializerSettings == null )
125
122
{
126
- _serializer = JsonSerializer . Create ( SerializerSettings ) ;
123
+ _serializerSettings = ShallowCopy ( SerializerSettings ) ;
127
124
}
128
125
129
- return _serializer ;
126
+ return JsonSerializer . Create ( _serializerSettings ) ;
130
127
}
131
128
132
129
/// <inheritdoc />
@@ -153,5 +150,43 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
153
150
await writer . FlushAsync ( ) ;
154
151
}
155
152
}
153
+
154
+ private static JsonSerializerSettings ShallowCopy ( JsonSerializerSettings settings )
155
+ {
156
+ var copiedSettings = new JsonSerializerSettings
157
+ {
158
+ FloatParseHandling = settings . FloatParseHandling ,
159
+ FloatFormatHandling = settings . FloatFormatHandling ,
160
+ DateParseHandling = settings . DateParseHandling ,
161
+ DateTimeZoneHandling = settings . DateTimeZoneHandling ,
162
+ DateFormatHandling = settings . DateFormatHandling ,
163
+ Formatting = settings . Formatting ,
164
+ MaxDepth = settings . MaxDepth ,
165
+ DateFormatString = settings . DateFormatString ,
166
+ Context = settings . Context ,
167
+ Error = settings . Error ,
168
+ SerializationBinder = settings . SerializationBinder ,
169
+ TraceWriter = settings . TraceWriter ,
170
+ Culture = settings . Culture ,
171
+ ReferenceResolverProvider = settings . ReferenceResolverProvider ,
172
+ EqualityComparer = settings . EqualityComparer ,
173
+ ContractResolver = settings . ContractResolver ,
174
+ ConstructorHandling = settings . ConstructorHandling ,
175
+ TypeNameAssemblyFormatHandling = settings . TypeNameAssemblyFormatHandling ,
176
+ MetadataPropertyHandling = settings . MetadataPropertyHandling ,
177
+ TypeNameHandling = settings . TypeNameHandling ,
178
+ PreserveReferencesHandling = settings . PreserveReferencesHandling ,
179
+ Converters = settings . Converters ,
180
+ DefaultValueHandling = settings . DefaultValueHandling ,
181
+ NullValueHandling = settings . NullValueHandling ,
182
+ ObjectCreationHandling = settings . ObjectCreationHandling ,
183
+ MissingMemberHandling = settings . MissingMemberHandling ,
184
+ ReferenceLoopHandling = settings . ReferenceLoopHandling ,
185
+ CheckAdditionalContent = settings . CheckAdditionalContent ,
186
+ StringEscapeHandling = settings . StringEscapeHandling ,
187
+ } ;
188
+
189
+ return copiedSettings ;
190
+ }
156
191
}
157
192
}
0 commit comments