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

Serialize to UTF-8 bytes with System.Text.Json. #4175

Merged
merged 1 commit into from
Sep 28, 2023

Conversation

olegd-superoffice
Copy link
Contributor

Serializing to a UTF-8 byte array is about 5-10% faster than using the string-based methods.

@ovska
Copy link
Contributor

ovska commented Oct 13, 2022

This can be further improved by using JsonContent so the payload is serialized directly to Http stream: JsonContent.Create(payload, options: _settings.Value). The downside is that this api seems to be available only from .NET 5 onwards, so a #if NET5_0_OR_GREATER preprocessor symbol might be required, which make the method more complex.
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.json.jsoncontent.create?view=net-6.0

@olegd-superoffice
Copy link
Contributor Author

This can be further improved by using JsonContent so the payload is serialized directly to Http stream: JsonContent.Create(payload, options: _settings.Value). The downside is that this api seems to be available only from .NET 5 onwards

@ovska I think this API is available for any supported .Net version, but it adds dependency on System.Net.Http.Json NuGet package. Could be worth another PR when/if this one is accepted.

@olegd-superoffice
Copy link
Contributor Author

I updated this PR to use UTF-8 byte array serialization when producing application/x-www-form-urlencoded in generated client.

@RicoSuter
Copy link
Owner

Can you fix the conflicts, otherwise looks good.

Serializing to a UTF-8 byte array is about 5-10% faster
than using the string-based methods.
@olegd-superoffice
Copy link
Contributor Author

@RicoSuter done.

@RicoSuter RicoSuter merged commit 54fa141 into RicoSuter:master Sep 28, 2023
1 check passed
lahma pushed a commit to lahma/NSwag that referenced this pull request Jan 20, 2024
Serializing to a UTF-8 byte array is about 5-10% faster
than using the string-based methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants