@@ -20,10 +20,7 @@ public class NewtonsoftJsonOutputFormatter : TextOutputFormatter
20
20
{
21
21
private readonly IArrayPool < char > _charPool ;
22
22
private readonly MvcOptions _mvcOptions ;
23
-
24
- // Perf: JsonSerializers are relatively expensive to create, and are thread safe. We cache
25
- // the serializer and invalidate it when the settings change.
26
- private JsonSerializer _serializer ;
23
+ private JsonSerializerSettings _serializerSettings ;
27
24
28
25
/// <summary>
29
26
/// Initializes a new <see cref="NewtonsoftJsonOutputFormatter"/> instance.
@@ -99,12 +96,13 @@ protected virtual JsonWriter CreateJsonWriter(TextWriter writer)
99
96
/// <returns>The <see cref="JsonSerializer"/> used during serialization and deserialization.</returns>
100
97
protected virtual JsonSerializer CreateJsonSerializer ( )
101
98
{
102
- if ( _serializer == null )
99
+ if ( _serializerSettings == null )
103
100
{
104
- _serializer = JsonSerializer . Create ( SerializerSettings ) ;
101
+ // Lock the serializer settings once the first serialization has been initiated.
102
+ _serializerSettings = ShallowCopy ( SerializerSettings ) ;
105
103
}
106
104
107
- return _serializer ;
105
+ return JsonSerializer . Create ( _serializerSettings ) ;
108
106
}
109
107
110
108
/// <summary>
@@ -166,5 +164,43 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
166
164
}
167
165
}
168
166
}
167
+
168
+ private static JsonSerializerSettings ShallowCopy ( JsonSerializerSettings settings )
169
+ {
170
+ var copiedSettings = new JsonSerializerSettings
171
+ {
172
+ FloatParseHandling = settings . FloatParseHandling ,
173
+ FloatFormatHandling = settings . FloatFormatHandling ,
174
+ DateParseHandling = settings . DateParseHandling ,
175
+ DateTimeZoneHandling = settings . DateTimeZoneHandling ,
176
+ DateFormatHandling = settings . DateFormatHandling ,
177
+ Formatting = settings . Formatting ,
178
+ MaxDepth = settings . MaxDepth ,
179
+ DateFormatString = settings . DateFormatString ,
180
+ Context = settings . Context ,
181
+ Error = settings . Error ,
182
+ SerializationBinder = settings . SerializationBinder ,
183
+ TraceWriter = settings . TraceWriter ,
184
+ Culture = settings . Culture ,
185
+ ReferenceResolverProvider = settings . ReferenceResolverProvider ,
186
+ EqualityComparer = settings . EqualityComparer ,
187
+ ContractResolver = settings . ContractResolver ,
188
+ ConstructorHandling = settings . ConstructorHandling ,
189
+ TypeNameAssemblyFormatHandling = settings . TypeNameAssemblyFormatHandling ,
190
+ MetadataPropertyHandling = settings . MetadataPropertyHandling ,
191
+ TypeNameHandling = settings . TypeNameHandling ,
192
+ PreserveReferencesHandling = settings . PreserveReferencesHandling ,
193
+ Converters = settings . Converters ,
194
+ DefaultValueHandling = settings . DefaultValueHandling ,
195
+ NullValueHandling = settings . NullValueHandling ,
196
+ ObjectCreationHandling = settings . ObjectCreationHandling ,
197
+ MissingMemberHandling = settings . MissingMemberHandling ,
198
+ ReferenceLoopHandling = settings . ReferenceLoopHandling ,
199
+ CheckAdditionalContent = settings . CheckAdditionalContent ,
200
+ StringEscapeHandling = settings . StringEscapeHandling ,
201
+ } ;
202
+
203
+ return copiedSettings ;
204
+ }
169
205
}
170
206
}
0 commit comments