11using System . Buffers ;
2- using System . Diagnostics . CodeAnalysis ;
3- using System . Formats . Asn1 ;
42using System . Text ;
53using System . Text . Json ;
64using System . Text . Json . Serialization ;
5+ using System . Text . Json . Serialization . Metadata ;
76using Microsoft . AspNetCore . Hosting . Server ;
87using Microsoft . AspNetCore . Http . Features ;
9- using Microsoft . Extensions . ObjectPool ;
108
119namespace Kestrel ;
1210
@@ -118,27 +116,12 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
118116 await body . Writer . FlushAsync ( ) ;
119117 }
120118
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-
125119 private static async Task Json ( IHttpResponseFeature res , IFeatureCollection features )
126120 {
127121 res . StatusCode = StatusCodes . Status200OK ;
128122 res . Headers . ContentType = "application/json; charset=utf-8" ;
129123
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 ) ;
142125 res . Headers . ContentLength = messageSpan . Length ;
143126
144127 var body = features . GetResponseBodyFeature ( ) ;
@@ -147,25 +130,22 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
147130
148131 await body . StartAsync ( ) ;
149132 await body . Writer . FlushAsync ( ) ;
150-
151- //_jsonWriterPool.Return(jsonWriter);
152- //_bufferWriterPool.Return(bufferWriter);
153133 }
154134
155135 [ ThreadStatic ]
156136 private static ArrayBufferWriter < byte > ? _bufferWriter ;
157137 [ ThreadStatic ]
158138 private static Utf8JsonWriter ? _jsonWriter ;
159139
160- private static ReadOnlySpan < byte > WriteMessage ( JsonMessage message )
140+ private static ReadOnlySpan < byte > JsonSerializeToUtf8Span < T > ( T value , JsonTypeInfo < T > jsonTypeInfo )
161141 {
162142 var bufferWriter = _bufferWriter ??= new ( 64 ) ;
163143 var jsonWriter = _jsonWriter ??= new ( _bufferWriter , new ( ) { Indented = false , SkipValidation = true } ) ;
164144
165145 bufferWriter . ResetWrittenCount ( ) ;
166146 jsonWriter . Reset ( bufferWriter ) ;
167147
168- JsonSerializer . Serialize ( jsonWriter , new JsonMessage { message = "Hello, World!" } , SerializerContext . JsonMessage ) ;
148+ JsonSerializer . Serialize ( jsonWriter , value , jsonTypeInfo ) ;
169149
170150 return bufferWriter . WrittenSpan ;
171151 }
@@ -175,15 +155,6 @@ private struct JsonMessage
175155 public required string message { get ; set ; }
176156 }
177157
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-
187158 private static readonly JsonContext SerializerContext = JsonContext . Default ;
188159
189160 // BUG: Can't use GenerationMode = JsonSourceGenerationMode.Serialization here due to https://github.com/dotnet/runtime/issues/111477
0 commit comments