Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from Newtonsoft.Json to System.Text.Json #672

Merged
merged 6 commits into from
Oct 30, 2024

Conversation

0xced
Copy link
Contributor

@0xced 0xced commented Mar 7, 2024

Everything is now truly async, no more fake Task.Factory.StartNew async.

@0xced
Copy link
Contributor Author

0xced commented Mar 7, 2024

Also, in commit 6a87013 I improved all the HTTP requests to deserialize the JSON payload directly from the HTTP response stream instead of reading the response as string then deserializing to model objects. This is an improvement in both speed and memory allocations.

using (var stream = await streamTask)
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true })
using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken)))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify this task can be cancelled by running the test CreateContainerAsync_TimeoutExpires_Fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the Skip reason and I can confirm that the tests with the 3 different timeouts are passing. ✅

@0xced 0xced force-pushed the System.Text.Json branch 2 times, most recently from bb945b0 to 2ff7b0e Compare July 4, 2024 18:58
@paulomorgado
Copy link

Also, in commit 6a87013 I improved all the HTTP requests to deserialize the JSON payload directly from the HTTP response stream instead of reading the response as string then deserializing to model objects. This is an improvement in both speed and memory allocations.

Why not just use HttpClientJsonExtensions with source generators?

I'm missing what the added value is for having generic methods with no constraints for making the HTTP requests.

@@ -5,6 +5,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't latest the default? What's the added benefit of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default language version depends on the target framework, see the C# language versioning documentation.

For .NET Standard 2.0 the language version is C# 7.3 and for .NET Standard 2.1 the language version is C# 8.0.

I don't remember everything I changed but there's a high probability that I used some feature that requires C# greater than 7.3.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake.

But it may be different for different SDK versions.

But, if you're aiming for a version higher than the supported for that target, you need to be very careful.

@0xced
Copy link
Contributor Author

0xced commented Sep 25, 2024

Why not just use HttpClientJsonExtensions with source generators?

I don't remember but it was probably because of the dozen of MakeRequestAsync variations that depend on ApiResponseErrorHandlingDelegate which whould have been a way too risky refactoring and also nearly impossible to review.

@galvesribeiro
Copy link
Member

It LGTM. For now, while we don't move toward the new code which drop the Go generator, this change to STJ will help others and yes, the extra allocations with strings will be dealt with in a separated PR. For now let's add it.

Also, the CI error is unrelated, so I'm good to push it as is. Will fix the CI separately.

Thank you @0xced!

@galvesribeiro galvesribeiro merged commit 14fe23c into dotnet:master Oct 30, 2024
1 of 2 checks passed
@0xced 0xced deleted the System.Text.Json branch October 31, 2024 06:51
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.

4 participants