1
1
using System . Buffers ;
2
- using System . Diagnostics . CodeAnalysis ;
3
- using System . Formats . Asn1 ;
4
2
using System . Text ;
5
3
using System . Text . Json ;
6
4
using System . Text . Json . Serialization ;
5
+ using System . Text . Json . Serialization . Metadata ;
7
6
using Microsoft . AspNetCore . Hosting . Server ;
8
7
using Microsoft . AspNetCore . Http . Features ;
9
- using Microsoft . Extensions . ObjectPool ;
10
8
11
9
namespace Kestrel ;
12
10
@@ -118,27 +116,12 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
118
116
await body . Writer . FlushAsync ( ) ;
119
117
}
120
118
121
- private static readonly ObjectPoolProvider _objectPoolProvider = new DefaultObjectPoolProvider ( ) ;
122
- private static readonly ObjectPool < ArrayBufferWriter < byte > > _bufferWriterPool = _objectPoolProvider . Create < ArrayBufferWriter < byte > > ( ) ;
123
- private static readonly ObjectPool < Utf8JsonWriter > _jsonWriterPool = _objectPoolProvider . Create ( new Utf8JsonWriterPooledObjectPolicy ( ) ) ;
124
-
125
119
private static async Task Json ( IHttpResponseFeature res , IFeatureCollection features )
126
120
{
127
121
res . StatusCode = StatusCodes . Status200OK ;
128
122
res . Headers . ContentType = "application/json; charset=utf-8" ;
129
123
130
- //var bufferWriter = _bufferWriterPool.Get();
131
- //var jsonWriter = _jsonWriterPool.Get();
132
-
133
- //var bufferWriter = new ArrayBufferWriter<byte>(64);
134
- //await using var jsonWriter = new Utf8JsonWriter(bufferWriter, new() { Indented = false, SkipValidation = true });
135
-
136
- //bufferWriter.ResetWrittenCount();
137
- //jsonWriter.Reset(bufferWriter);
138
-
139
- //JsonSerializer.Serialize(jsonWriter, new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
140
-
141
- var messageSpan = WriteMessage ( new JsonMessage { message = "Hello, World!" } ) ;
124
+ var messageSpan = JsonSerializeToUtf8Span ( new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
142
125
res . Headers . ContentLength = messageSpan . Length ;
143
126
144
127
var body = features . GetResponseBodyFeature ( ) ;
@@ -147,25 +130,22 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
147
130
148
131
await body . StartAsync ( ) ;
149
132
await body . Writer . FlushAsync ( ) ;
150
-
151
- //_jsonWriterPool.Return(jsonWriter);
152
- //_bufferWriterPool.Return(bufferWriter);
153
133
}
154
134
155
135
[ ThreadStatic ]
156
136
private static ArrayBufferWriter < byte > ? _bufferWriter ;
157
137
[ ThreadStatic ]
158
138
private static Utf8JsonWriter ? _jsonWriter ;
159
139
160
- private static ReadOnlySpan < byte > WriteMessage ( JsonMessage message )
140
+ private static ReadOnlySpan < byte > JsonSerializeToUtf8Span < T > ( T value , JsonTypeInfo < T > jsonTypeInfo )
161
141
{
162
142
var bufferWriter = _bufferWriter ??= new ( 64 ) ;
163
143
var jsonWriter = _jsonWriter ??= new ( _bufferWriter , new ( ) { Indented = false , SkipValidation = true } ) ;
164
144
165
145
bufferWriter . ResetWrittenCount ( ) ;
166
146
jsonWriter . Reset ( bufferWriter ) ;
167
147
168
- JsonSerializer . Serialize ( jsonWriter , new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
148
+ JsonSerializer . Serialize ( jsonWriter , value , jsonTypeInfo ) ;
169
149
170
150
return bufferWriter . WrittenSpan ;
171
151
}
@@ -175,15 +155,6 @@ private struct JsonMessage
175
155
public required string message { get ; set ; }
176
156
}
177
157
178
- private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy < Utf8JsonWriter >
179
- {
180
- private static readonly ArrayBufferWriter < byte > _dummyBufferWriter = new ( 256 ) ;
181
-
182
- public Utf8JsonWriter Create ( ) => new ( _dummyBufferWriter , new ( ) { Indented = false , SkipValidation = true } ) ;
183
-
184
- public bool Return ( Utf8JsonWriter obj ) => true ;
185
- }
186
-
187
158
private static readonly JsonContext SerializerContext = JsonContext . Default ;
188
159
189
160
// BUG: Can't use GenerationMode = JsonSourceGenerationMode.Serialization here due to https://github.com/dotnet/runtime/issues/111477
0 commit comments