|
@@ -4,18 +4,18 @@
|
|
using System;
|
|
using System;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text;
|
|
|
|
+using System.Text.Json;
|
|
|
|
+using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using Benchmarks.Configuration;
|
|
using Benchmarks.Configuration;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
-using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace Benchmarks.Middleware
|
|
namespace Benchmarks.Middleware
|
|
{
|
|
{
|
|
public class JsonMiddleware
|
|
public class JsonMiddleware
|
|
{
|
|
{
|
|
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.Json));
|
|
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.Json));
|
|
- private static readonly JsonSerializer _json = new JsonSerializer();
|
|
|
|
private static readonly UTF8Encoding _encoding = new UTF8Encoding(false);
|
|
private static readonly UTF8Encoding _encoding = new UTF8Encoding(false);
|
|
private const int _bufferSize = 27;
|
|
private const int _bufferSize = 27;
|
|
|
|
|
|
@@ -34,12 +34,7 @@ namespace Benchmarks.Middleware
|
|
httpContext.Response.ContentType = "application/json";
|
|
httpContext.Response.ContentType = "application/json";
|
|
httpContext.Response.ContentLength = _bufferSize;
|
|
httpContext.Response.ContentLength = _bufferSize;
|
|
|
|
|
|
- using (var sw = new StreamWriter(httpContext.Response.Body, _encoding, bufferSize: _bufferSize))
|
|
|
|
- {
|
|
|
|
- _json.Serialize(sw, new { message = "Hello, World!" });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return Task.CompletedTask;
|
|
|
|
|
|
+ return JsonSerializer.SerializeAsync<JsonMessage>(httpContext.Response.Body, new JsonMessage { message = "Hello, World!" });
|
|
}
|
|
}
|
|
|
|
|
|
return _next(httpContext);
|
|
return _next(httpContext);
|
|
@@ -53,4 +48,9 @@ namespace Benchmarks.Middleware
|
|
return builder.UseMiddleware<JsonMiddleware>();
|
|
return builder.UseMiddleware<JsonMiddleware>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public struct JsonMessage
|
|
|
|
+ {
|
|
|
|
+ public string message { get; set; }
|
|
|
|
+ }
|
|
}
|
|
}
|