Skip to content

Commit

Permalink
WriteAsJsonAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Jan 11, 2023
1 parent 9677ac9 commit eb2b486
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Benchmarks/Middleware/JsonMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.Options;

namespace Benchmarks.Middleware
{
Expand All @@ -20,25 +22,22 @@ public class JsonMiddleware
private const int _bufferSize = 27;

private readonly RequestDelegate _next;
private readonly JsonSerializerOptions _jsonOptions;

public JsonMiddleware(RequestDelegate next) => _next = next;
public JsonMiddleware(RequestDelegate next, IOptions<JsonOptions> jsonOptions)
{
_next = next;
_jsonOptions = jsonOptions.Value.SerializerOptions;
}

public Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
httpContext.Response.StatusCode = 200;
httpContext.Response.ContentType = "application/json";
httpContext.Response.ContentLength = _bufferSize;

var syncIOFeature = httpContext.Features.Get<IHttpBodyControlFeature>();
if (syncIOFeature != null)
{
syncIOFeature.AllowSynchronousIO = true;
}

return JsonSerializer.SerializeAsync<JsonMessage>(httpContext.Response.Body, new JsonMessage { message = "Hello, World!" },
CustomJsonContext.Default.JsonMessage);
return httpContext.Response.WriteAsJsonAsync<JsonMessage>(new JsonMessage { message = "Hello, World!" }, _jsonOptions, httpContext.RequestAborted);
}

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

[JsonSourceGenerationOptions]
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(JsonMessage))]
internal partial class CustomJsonContext : JsonSerializerContext
{
Expand Down
6 changes: 6 additions & 0 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Runtime;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using Benchmarks.Configuration;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -84,6 +85,11 @@ public static void Main(string[] args)
}
#endif
})
.ConfigureHttpJsonOptions(jsonOptions =>
{
jsonOptions.SerializerOptions.Encoder = null;
jsonOptions.SerializerOptions.AddContext<Middleware.CustomJsonContext>();
})
)
.UseDefaultServiceProvider(
(context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment());
Expand Down

0 comments on commit eb2b486

Please sign in to comment.