Skip to content

Deserialize from from Stream instead of from String#138

Merged
Prads- merged 7 commits intomasterfrom
httpclient/iserializer-stream
Oct 20, 2025
Merged

Deserialize from from Stream instead of from String#138
Prads- merged 7 commits intomasterfrom
httpclient/iserializer-stream

Conversation

@MrSmoke
Copy link
Member

@MrSmoke MrSmoke commented Sep 14, 2025

Change BaseRestClientRequest to deserialize the content using a stream and pass that stream to a new method on Serializer which accepts a Stream.

  • Added new ValueTask<T?> DeserializeAsync<T>(Stream, CancellationToken); to ISerializer
    • Uses Task for full fat .net
  • When calling HttpClient.SendAsync, use HttpCompletionOption.ResponseHeadersRead to avoid buffering the response internally
  • Update NewtonsoftJsonSerializer to use a single instead of JsonSerializer instead of the JsonConvert methods
    • I basically copied how the JsonConvert methods work

Some benchmarks showing the lower memory usage

Old:

 Method       | Mean     | Error     | StdDev    | Gen0     | Gen1     | Gen2    | Allocated |
|------------- |---------:|----------:|----------:|---------:|---------:|--------:|----------:|
| ExecuteAsync | 1.588 ms | 0.0316 ms | 0.0519 ms | 146.4844 | 109.3750 | 72.2656 | 951.09 KB |

New:

| Method       | Mean     | Error     | StdDev    | Gen0     | Gen1    | Gen2    | Allocated |
|------------- |---------:|----------:|----------:|---------:|--------:|--------:|----------:|
| ExecuteAsync | 1.503 ms | 0.0299 ms | 0.0508 ms | 109.3750 | 35.1563 | 35.1563 |  781.8 KB |

@MrSmoke MrSmoke requested a review from Prads- September 15, 2025 05:24
@MrSmoke MrSmoke changed the title Deserialize from from Stream instead of from String WIP: Deserialize from from Stream instead of from String Sep 22, 2025
@MrSmoke MrSmoke changed the title WIP: Deserialize from from Stream instead of from String Deserialize from from Stream instead of from String Oct 20, 2025
@MrSmoke MrSmoke force-pushed the httpclient/iserializer-stream branch from 0971c08 to e6510e8 Compare October 20, 2025 01:56
@MrSmoke MrSmoke force-pushed the httpclient/iserializer-stream branch from e6510e8 to c0d89f8 Compare October 20, 2025 01:56
@Prads- Prads- merged commit 423136f into master Oct 20, 2025
5 checks passed
@Prads- Prads- deleted the httpclient/iserializer-stream branch October 20, 2025 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants