1
1
using System . Buffers ;
2
+ using System . Formats . Asn1 ;
2
3
using System . Text ;
3
4
using System . Text . Json ;
5
+ using System . Text . Json . Serialization ;
4
6
using Microsoft . AspNetCore . Hosting . Server ;
5
7
using Microsoft . AspNetCore . Http . Features ;
6
8
using Microsoft . Extensions . ObjectPool ;
7
9
8
- public class BenchmarkApp : IHttpApplication < IFeatureCollection >
10
+ namespace Kestrel ;
11
+
12
+ public sealed partial class BenchmarkApp : IHttpApplication < IFeatureCollection >
9
13
{
10
14
public IFeatureCollection CreateContext ( IFeatureCollection features ) => features ;
11
15
@@ -68,16 +72,14 @@ private static async Task Plaintext(IHttpResponseFeature res, IFeatureCollection
68
72
await body . Writer . FlushAsync ( ) ;
69
73
}
70
74
71
- private static readonly JsonSerializerOptions _jsonSerializerOptions = new ( JsonSerializerDefaults . Web ) ;
72
-
73
75
private static async Task JsonChunked ( IHttpResponseFeature res , IFeatureCollection features )
74
76
{
75
77
res . StatusCode = StatusCodes . Status200OK ;
76
78
res . Headers . ContentType = "application/json; charset=utf-8" ;
77
79
78
80
var body = features . GetResponseBodyFeature ( ) ;
79
81
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 ) ;
81
83
await body . Writer . FlushAsync ( ) ;
82
84
}
83
85
@@ -86,7 +88,7 @@ private static async Task JsonString(IHttpResponseFeature res, IFeatureCollectio
86
88
res . StatusCode = StatusCodes . Status200OK ;
87
89
res . Headers . ContentType = "application/json; charset=utf-8" ;
88
90
89
- var message = JsonSerializer . Serialize ( new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
91
+ var message = JsonSerializer . Serialize ( new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
90
92
res . Headers . ContentLength = Encoding . UTF8 . GetByteCount ( message ) ;
91
93
92
94
var body = features . GetResponseBodyFeature ( ) ;
@@ -104,7 +106,7 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
104
106
res . StatusCode = StatusCodes . Status200OK ;
105
107
res . Headers . ContentType = "application/json; charset=utf-8" ;
106
108
107
- var messageBytes = JsonSerializer . SerializeToUtf8Bytes ( new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
109
+ var messageBytes = JsonSerializer . SerializeToUtf8Bytes ( new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
108
110
res . Headers . ContentLength = messageBytes . Length ;
109
111
110
112
var body = features . GetResponseBodyFeature ( ) ;
@@ -130,7 +132,10 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
130
132
bufferWriter . ResetWrittenCount ( ) ;
131
133
jsonWriter . Reset ( bufferWriter ) ;
132
134
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 ) ;
134
139
135
140
res . Headers . ContentLength = bufferWriter . WrittenCount ;
136
141
@@ -139,9 +144,6 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
139
144
await body . StartAsync ( ) ;
140
145
body . Writer . Write ( bufferWriter . WrittenSpan ) ;
141
146
await body . Writer . FlushAsync ( ) ;
142
-
143
- _jsonWriterPool . Return ( jsonWriter ) ;
144
- _bufferWriterPool . Return ( bufferWriter ) ;
145
147
}
146
148
147
149
private struct JsonMessage
@@ -157,4 +159,14 @@ private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy<Utf8JsonWri
157
159
158
160
public bool Return ( Utf8JsonWriter obj ) => true ;
159
161
}
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
+
160
172
}
0 commit comments