Pass the formatter context to the CreateJsonSerializer. It gives ability to override and create serializer specific to the context (#8358)
* Pass the formatter context to the CreateJsonSerializer so that it gives ability to override and create serializer specific to the context
This commit is contained in:
parent
314f67ec6e
commit
2a08c6e54d
|
|
@ -25,6 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
public virtual Microsoft.AspNetCore.Mvc.Formatters.InputFormatterExceptionPolicy ExceptionPolicy { get { throw null; } }
|
public virtual Microsoft.AspNetCore.Mvc.Formatters.InputFormatterExceptionPolicy ExceptionPolicy { get { throw null; } }
|
||||||
protected Newtonsoft.Json.JsonSerializerSettings SerializerSettings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]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() { throw null; }
|
||||||
|
protected virtual Newtonsoft.Json.JsonSerializer CreateJsonSerializer(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context) { throw null; }
|
||||||
[System.Diagnostics.DebuggerStepThroughAttribute]
|
[System.Diagnostics.DebuggerStepThroughAttribute]
|
||||||
public override System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.Formatters.InputFormatterResult> ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context, System.Text.Encoding encoding) { throw null; }
|
public override System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.Formatters.InputFormatterResult> ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context, System.Text.Encoding encoding) { throw null; }
|
||||||
protected virtual void ReleaseJsonSerializer(Newtonsoft.Json.JsonSerializer serializer) { }
|
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<char> charPool) { }
|
public NewtonsoftJsonOutputFormatter(Newtonsoft.Json.JsonSerializerSettings serializerSettings, System.Buffers.ArrayPool<char> charPool) { }
|
||||||
protected Newtonsoft.Json.JsonSerializerSettings SerializerSettings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]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() { 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; }
|
protected virtual Newtonsoft.Json.JsonWriter CreateJsonWriter(System.IO.TextWriter writer) { throw null; }
|
||||||
[System.Diagnostics.DebuggerStepThroughAttribute]
|
[System.Diagnostics.DebuggerStepThroughAttribute]
|
||||||
public override System.Threading.Tasks.Task WriteResponseBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context, System.Text.Encoding selectedEncoding) { throw null; }
|
public override System.Threading.Tasks.Task WriteResponseBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context, System.Text.Encoding selectedEncoding) { throw null; }
|
||||||
|
|
|
||||||
|
|
@ -202,7 +202,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = context.ModelType;
|
var type = context.ModelType;
|
||||||
var jsonSerializer = CreateJsonSerializer();
|
var jsonSerializer = CreateJsonSerializer(context);
|
||||||
jsonSerializer.Error += ErrorHandler;
|
jsonSerializer.Error += ErrorHandler;
|
||||||
object model;
|
object model;
|
||||||
try
|
try
|
||||||
|
|
@ -244,7 +244,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called during deserialization to get the <see cref="JsonSerializer"/>.
|
/// Called during deserialization to get the <see cref="JsonSerializer"/>. The formatter context
|
||||||
|
/// that is passed gives an ability to create serializer specific to the context.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>
|
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
|
|
@ -261,6 +262,21 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
return _jsonSerializerPool.Get();
|
return _jsonSerializerPool.Get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called during deserialization to get the <see cref="JsonSerializer"/>. The formatter context
|
||||||
|
/// that is passed gives an ability to create serializer specific to the context.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">A context object used by an input formatter for deserializing the request body into an object.</param>
|
||||||
|
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method works in tandem with <see cref="ReleaseJsonSerializer(JsonSerializer)"/> to
|
||||||
|
/// manage the lifetimes of <see cref="JsonSerializer"/> instances.
|
||||||
|
/// </remarks>
|
||||||
|
protected virtual JsonSerializer CreateJsonSerializer(InputFormatterContext context)
|
||||||
|
{
|
||||||
|
return CreateJsonSerializer();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Releases the <paramref name="serializer"/> instance.
|
/// Releases the <paramref name="serializer"/> instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called during serialization to create the <see cref="JsonSerializer"/>.
|
/// Called during serialization to create the <see cref="JsonSerializer"/>.The formatter context
|
||||||
|
/// that is passed gives an ability to create serializer specific to the context.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The <see cref="JsonSerializer"/> used during serialization and deserialization.</returns>
|
/// <returns>The <see cref="JsonSerializer"/> used during serialization and deserialization.</returns>
|
||||||
protected virtual JsonSerializer CreateJsonSerializer()
|
protected virtual JsonSerializer CreateJsonSerializer()
|
||||||
|
|
@ -98,6 +99,17 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
return _serializer;
|
return _serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called during serialization to create the <see cref="JsonSerializer"/>.The formatter context
|
||||||
|
/// that is passed gives an ability to create serializer specific to the context.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">A context object for <see cref="IOutputFormatter.WriteAsync(OutputFormatterWriteContext)"/>.</param>
|
||||||
|
/// <returns>The <see cref="JsonSerializer"/> used during serialization and deserialization.</returns>
|
||||||
|
protected virtual JsonSerializer CreateJsonSerializer(OutputFormatterWriteContext context)
|
||||||
|
{
|
||||||
|
return CreateJsonSerializer();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
|
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
|
||||||
{
|
{
|
||||||
|
|
@ -123,7 +135,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
{
|
{
|
||||||
using (var jsonWriter = CreateJsonWriter(writer))
|
using (var jsonWriter = CreateJsonWriter(writer))
|
||||||
{
|
{
|
||||||
var jsonSerializer = CreateJsonSerializer();
|
var jsonSerializer = CreateJsonSerializer(context);
|
||||||
jsonSerializer.Serialize(jsonWriter, context.Object);
|
jsonSerializer.Serialize(jsonWriter, context.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -474,7 +474,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
var formatter = new TestableJsonInputFormatter(settings);
|
var formatter = new TestableJsonInputFormatter(settings);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var actual = formatter.CreateJsonSerializer();
|
var actual = formatter.CreateJsonSerializer(null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Same(settings.ContractResolver, actual.ContractResolver);
|
Assert.Same(settings.ContractResolver, actual.ContractResolver);
|
||||||
|
|
@ -578,7 +578,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
|
|
||||||
public new JsonSerializerSettings SerializerSettings => base.SerializerSettings;
|
public new JsonSerializerSettings SerializerSettings => base.SerializerSettings;
|
||||||
|
|
||||||
public new JsonSerializer CreateJsonSerializer() => base.CreateJsonSerializer();
|
public new JsonSerializer CreateJsonSerializer(InputFormatterContext _) => base.CreateJsonSerializer(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ILogger GetLogger()
|
private static ILogger GetLogger()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue