Skip to content

Commit eb2b486

Browse files
committed
WriteAsJsonAsync
1 parent 9677ac9 commit eb2b486

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Benchmarks/Middleware/JsonMiddleware.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.AspNetCore.Builder;
1212
using Microsoft.AspNetCore.Http;
1313
using Microsoft.AspNetCore.Http.Features;
14+
using Microsoft.AspNetCore.Http.Json;
15+
using Microsoft.Extensions.Options;
1416

1517
namespace Benchmarks.Middleware
1618
{
@@ -20,25 +22,22 @@ public class JsonMiddleware
2022
private const int _bufferSize = 27;
2123

2224
private readonly RequestDelegate _next;
25+
private readonly JsonSerializerOptions _jsonOptions;
2326

24-
public JsonMiddleware(RequestDelegate next) => _next = next;
27+
public JsonMiddleware(RequestDelegate next, IOptions<JsonOptions> jsonOptions)
28+
{
29+
_next = next;
30+
_jsonOptions = jsonOptions.Value.SerializerOptions;
31+
}
2532

2633
public Task Invoke(HttpContext httpContext)
2734
{
2835
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
2936
{
3037
httpContext.Response.StatusCode = 200;
31-
httpContext.Response.ContentType = "application/json";
3238
httpContext.Response.ContentLength = _bufferSize;
3339

34-
var syncIOFeature = httpContext.Features.Get<IHttpBodyControlFeature>();
35-
if (syncIOFeature != null)
36-
{
37-
syncIOFeature.AllowSynchronousIO = true;
38-
}
39-
40-
return JsonSerializer.SerializeAsync<JsonMessage>(httpContext.Response.Body, new JsonMessage { message = "Hello, World!" },
41-
CustomJsonContext.Default.JsonMessage);
40+
return httpContext.Response.WriteAsJsonAsync<JsonMessage>(new JsonMessage { message = "Hello, World!" }, _jsonOptions, httpContext.RequestAborted);
4241
}
4342

4443
return _next(httpContext);
@@ -50,7 +49,7 @@ public static class JsonMiddlewareExtensions
5049
public static IApplicationBuilder UseJson(this IApplicationBuilder builder) => builder.UseMiddleware<JsonMiddleware>();
5150
}
5251

53-
[JsonSourceGenerationOptions]
52+
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
5453
[JsonSerializable(typeof(JsonMessage))]
5554
internal partial class CustomJsonContext : JsonSerializerContext
5655
{

src/Benchmarks/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Reflection;
88
using System.Runtime;
99
using System.Text.RegularExpressions;
10+
using System.Text.Json.Serialization.Metadata;
1011
using System.Threading;
1112
using Benchmarks.Configuration;
1213
using Microsoft.AspNetCore.Hosting;
@@ -84,6 +85,11 @@ public static void Main(string[] args)
8485
}
8586
#endif
8687
})
88+
.ConfigureHttpJsonOptions(jsonOptions =>
89+
{
90+
jsonOptions.SerializerOptions.Encoder = null;
91+
jsonOptions.SerializerOptions.AddContext<Middleware.CustomJsonContext>();
92+
})
8793
)
8894
.UseDefaultServiceProvider(
8995
(context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment());

0 commit comments

Comments
 (0)