diff --git a/src/Mvc/Mvc.NewtonsoftJson/ref/Microsoft.AspNetCore.Mvc.NewtonsoftJson.netcoreapp3.0.cs b/src/Mvc/Mvc.NewtonsoftJson/ref/Microsoft.AspNetCore.Mvc.NewtonsoftJson.netcoreapp3.0.cs index 36c93b6fa1..066dba3dd5 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/ref/Microsoft.AspNetCore.Mvc.NewtonsoftJson.netcoreapp3.0.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/ref/Microsoft.AspNetCore.Mvc.NewtonsoftJson.netcoreapp3.0.cs @@ -25,6 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters public virtual Microsoft.AspNetCore.Mvc.Formatters.InputFormatterExceptionPolicy ExceptionPolicy { get { throw null; } } protected Newtonsoft.Json.JsonSerializerSettings SerializerSettings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } protected virtual Newtonsoft.Json.JsonSerializer CreateJsonSerializer() { throw null; } + protected virtual Newtonsoft.Json.JsonSerializer CreateJsonSerializer(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context) { throw null; } [System.Diagnostics.DebuggerStepThroughAttribute] public override System.Threading.Tasks.Task ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context, System.Text.Encoding encoding) { throw null; } protected virtual void ReleaseJsonSerializer(Newtonsoft.Json.JsonSerializer serializer) { } @@ -34,6 +35,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters public NewtonsoftJsonOutputFormatter(Newtonsoft.Json.JsonSerializerSettings serializerSettings, System.Buffers.ArrayPool charPool) { } protected Newtonsoft.Json.JsonSerializerSettings SerializerSettings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } protected virtual Newtonsoft.Json.JsonSerializer CreateJsonSerializer() { throw null; } + protected virtual Newtonsoft.Json.JsonSerializer CreateJsonSerializer(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context) { throw null; } protected virtual Newtonsoft.Json.JsonWriter CreateJsonWriter(System.IO.TextWriter writer) { throw null; } [System.Diagnostics.DebuggerStepThroughAttribute] public override System.Threading.Tasks.Task WriteResponseBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context, System.Text.Encoding selectedEncoding) { throw null; } diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs index db1dab6b5a..254ebcb7b3 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs @@ -202,7 +202,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } var type = context.ModelType; - var jsonSerializer = CreateJsonSerializer(); + var jsonSerializer = CreateJsonSerializer(context); jsonSerializer.Error += ErrorHandler; object model; try @@ -244,7 +244,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } /// - /// Called during deserialization to get the . + /// Called during deserialization to get the . The formatter context + /// that is passed gives an ability to create serializer specific to the context. /// /// The used during deserialization. /// @@ -261,6 +262,21 @@ namespace Microsoft.AspNetCore.Mvc.Formatters return _jsonSerializerPool.Get(); } + /// + /// Called during deserialization to get the . The formatter context + /// that is passed gives an ability to create serializer specific to the context. + /// + /// A context object used by an input formatter for deserializing the request body into an object. + /// The used during deserialization. + /// + /// This method works in tandem with to + /// manage the lifetimes of instances. + /// + protected virtual JsonSerializer CreateJsonSerializer(InputFormatterContext context) + { + return CreateJsonSerializer(); + } + /// /// Releases the instance. /// diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs index 6e71a1c00b..934cf824ff 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs @@ -85,7 +85,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } /// - /// Called during serialization to create the . + /// Called during serialization to create the .The formatter context + /// that is passed gives an ability to create serializer specific to the context. /// /// The used during serialization and deserialization. protected virtual JsonSerializer CreateJsonSerializer() @@ -98,6 +99,17 @@ namespace Microsoft.AspNetCore.Mvc.Formatters return _serializer; } + /// + /// Called during serialization to create the .The formatter context + /// that is passed gives an ability to create serializer specific to the context. + /// + /// A context object for . + /// The used during serialization and deserialization. + protected virtual JsonSerializer CreateJsonSerializer(OutputFormatterWriteContext context) + { + return CreateJsonSerializer(); + } + /// public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { @@ -123,7 +135,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { using (var jsonWriter = CreateJsonWriter(writer)) { - var jsonSerializer = CreateJsonSerializer(); + var jsonSerializer = CreateJsonSerializer(context); jsonSerializer.Serialize(jsonWriter, context.Object); } diff --git a/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonInputFormatterTest.cs b/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonInputFormatterTest.cs index 35b2cadebf..211f5d87a8 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonInputFormatterTest.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonInputFormatterTest.cs @@ -474,7 +474,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters var formatter = new TestableJsonInputFormatter(settings); // Act - var actual = formatter.CreateJsonSerializer(); + var actual = formatter.CreateJsonSerializer(null); // Assert Assert.Same(settings.ContractResolver, actual.ContractResolver); @@ -578,7 +578,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters public new JsonSerializerSettings SerializerSettings => base.SerializerSettings; - public new JsonSerializer CreateJsonSerializer() => base.CreateJsonSerializer(); + public new JsonSerializer CreateJsonSerializer(InputFormatterContext _) => base.CreateJsonSerializer(null); } private static ILogger GetLogger()