© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
1 reply
Ploxi

❔ [System.Text.Json] When to close the stream

I have a very large json file (500 mb or more), so naturally i would use
System.Text.Json
System.Text.Json
in combination with
IAsyncEnumerable<FileMetaDataDto>
IAsyncEnumerable<FileMetaDataDto>
.
Now i want to open a stream to a file using the following:
public static async Task<T?> ReadAsJson<T>(this IContentProvider contentProvider, Uri uri, JsonSerializerOptions? options = null)
        {
            using var stream = await contentProvider.OpenContentStream(uri);
            return await JsonSerializer.DeserializeAsync<T>(stream, options);
        }
public static async Task<T?> ReadAsJson<T>(this IContentProvider contentProvider, Uri uri, JsonSerializerOptions? options = null)
        {
            using var stream = await contentProvider.OpenContentStream(uri);
            return await JsonSerializer.DeserializeAsync<T>(stream, options);
        }

But what if the serializer needs the stream afterwards? If i leave it open, the stream would be dangling in limbo until the GC decides that it really wouldnt neeed that referencing memory anymore....
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ System.Text.Json paths
C#CC# / help
3y ago
System.Text.Json Source Generators
C#CC# / help
3y ago
✅ System.Text.Json serialization problem
C#CC# / help
4y ago
Create JSON objects from System.Text.Json
C#CC# / help
2y ago