System.Text.Json.JsonSerializer DeserializeAsyncEnumerable works slowly with large json files (10+mb) #100581
-
I'm using await using var fileStream = File.OpenRead(filePath);
using var content = new MultipartFormDataContent();
using var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = Path.GetFileName(filePath)
};
content.Add(streamContent);
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/v1/streaming")
{
Content = content
};
var response = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead,
cancellationToken);
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
var streamWrapper = new StreamWrapper(responseStream);
var bufferSize = 10 * 1024 * 1024;
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = {new JsonElementToObjectConverter()},
DefaultBufferSize = bufferSize
};
await foreach (var batchPart in JsonSerializer.DeserializeAsyncEnumerable<BatchPart>(streamWrapper, options,
cancellationToken))
{
} After investigation, I discovered that
In general, it takes ~15 minutes, but the same case only takes ~2 seconds if I am not using chunked-reading. var response = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead,
cancellationToken); The inner implementation explicitly uses
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Marking as answered since there's a tracking issue for this now: #100582 |
Beta Was this translation helpful? Give feedback.
Marking as answered since there's a tracking issue for this now: #100582