11using System . Buffers ;
2+ using System . Formats . Asn1 ;
23using System . Text ;
34using System . Text . Json ;
5+ using System . Text . Json . Serialization ;
46using Microsoft . AspNetCore . Hosting . Server ;
57using Microsoft . AspNetCore . Http . Features ;
68using Microsoft . Extensions . ObjectPool ;
79
8- public class BenchmarkApp : IHttpApplication < IFeatureCollection >
10+ namespace Kestrel ;
11+
12+ public sealed partial class BenchmarkApp : IHttpApplication < IFeatureCollection >
913{
1014 public IFeatureCollection CreateContext ( IFeatureCollection features ) => features ;
1115
@@ -68,16 +72,14 @@ private static async Task Plaintext(IHttpResponseFeature res, IFeatureCollection
6872 await body . Writer . FlushAsync ( ) ;
6973 }
7074
71- private static readonly JsonSerializerOptions _jsonSerializerOptions = new ( JsonSerializerDefaults . Web ) ;
72-
7375 private static async Task JsonChunked ( IHttpResponseFeature res , IFeatureCollection features )
7476 {
7577 res . StatusCode = StatusCodes . Status200OK ;
7678 res . Headers . ContentType = "application/json; charset=utf-8" ;
7779
7880 var body = features . GetResponseBodyFeature ( ) ;
7981 await body . StartAsync ( ) ;
80- await JsonSerializer . SerializeAsync ( body . Writer , new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
82+ await JsonSerializer . SerializeAsync ( body . Writer , new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
8183 await body . Writer . FlushAsync ( ) ;
8284 }
8385
@@ -86,7 +88,7 @@ private static async Task JsonString(IHttpResponseFeature res, IFeatureCollectio
8688 res . StatusCode = StatusCodes . Status200OK ;
8789 res . Headers . ContentType = "application/json; charset=utf-8" ;
8890
89- var message = JsonSerializer . Serialize ( new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
91+ var message = JsonSerializer . Serialize ( new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
9092 res . Headers . ContentLength = Encoding . UTF8 . GetByteCount ( message ) ;
9193
9294 var body = features . GetResponseBodyFeature ( ) ;
@@ -104,7 +106,7 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
104106 res . StatusCode = StatusCodes . Status200OK ;
105107 res . Headers . ContentType = "application/json; charset=utf-8" ;
106108
107- var messageBytes = JsonSerializer . SerializeToUtf8Bytes ( new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
109+ var messageBytes = JsonSerializer . SerializeToUtf8Bytes ( new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
108110 res . Headers . ContentLength = messageBytes . Length ;
109111
110112 var body = features . GetResponseBodyFeature ( ) ;
@@ -130,7 +132,10 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
130132 bufferWriter . ResetWrittenCount ( ) ;
131133 jsonWriter . Reset ( bufferWriter ) ;
132134
133- JsonSerializer . Serialize ( jsonWriter , new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
135+ JsonSerializer . Serialize ( jsonWriter , new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
136+
137+ _jsonWriterPool . Return ( jsonWriter ) ;
138+ _bufferWriterPool . Return ( bufferWriter ) ;
134139
135140 res . Headers . ContentLength = bufferWriter . WrittenCount ;
136141
@@ -139,9 +144,6 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
139144 await body . StartAsync ( ) ;
140145 body . Writer . Write ( bufferWriter . WrittenSpan ) ;
141146 await body . Writer . FlushAsync ( ) ;
142-
143- _jsonWriterPool . Return ( jsonWriter ) ;
144- _bufferWriterPool . Return ( bufferWriter ) ;
145147 }
146148
147149 private struct JsonMessage
@@ -157,4 +159,14 @@ private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy<Utf8JsonWri
157159
158160 public bool Return ( Utf8JsonWriter obj ) => true ;
159161 }
162+
163+ private static readonly JsonContext SerializerContext = JsonContext . Default ;
164+
165+ [ JsonSourceGenerationOptions ( GenerationMode = JsonSourceGenerationMode . Serialization ) ]
166+ [ JsonSerializable ( typeof ( JsonMessage ) ) ]
167+ private partial class JsonContext : JsonSerializerContext
168+ {
169+
170+ }
171+
160172}
0 commit comments