From 8ed97cef9957095fa945f14a80e2d92a4289abc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 7 Mar 2024 17:11:41 +0100 Subject: [PATCH] Avoid roundtrip to string by serializing directly to UTF-8 bytes --- src/Docker.DotNet/Endpoints/ImageOperations.cs | 7 ++----- src/Docker.DotNet/Endpoints/SwarmOperations.cs | 3 +-- src/Docker.DotNet/JsonSerializer.cs | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Docker.DotNet/Endpoints/ImageOperations.cs b/src/Docker.DotNet/Endpoints/ImageOperations.cs index 27e26d2f..6360da4b 100644 --- a/src/Docker.DotNet/Endpoints/ImageOperations.cs +++ b/src/Docker.DotNet/Endpoints/ImageOperations.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Net; using System.Net.Http; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -307,9 +306,7 @@ private Dictionary RegistryAuthHeaders(AuthConfig authConfig) { { RegistryAuthHeaderKey, - Convert.ToBase64String( - Encoding.UTF8.GetBytes( - this._client.JsonSerializer.SerializeObject(authConfig ?? new AuthConfig()))) + Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(authConfig ?? new AuthConfig())) .Replace("/", "_").Replace("+", "-") // This is not documented in Docker API but from source code (https://github.com/docker/docker-ce/blob/10e40bd1548f69354a803a15fde1b672cc024b91/components/cli/cli/command/registry.go#L47) // and from multiple internet sources it has to be base64-url-safe. @@ -325,7 +322,7 @@ private Dictionary RegistryConfigHeaders(IEnumerable { { RegistryConfigHeaderKey, - Convert.ToBase64String(Encoding.UTF8.GetBytes(this._client.JsonSerializer.SerializeObject(configDictionary))) + Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(configDictionary)) } }; } diff --git a/src/Docker.DotNet/Endpoints/SwarmOperations.cs b/src/Docker.DotNet/Endpoints/SwarmOperations.cs index 7a482a85..60635870 100644 --- a/src/Docker.DotNet/Endpoints/SwarmOperations.cs +++ b/src/Docker.DotNet/Endpoints/SwarmOperations.cs @@ -4,7 +4,6 @@ namespace Docker.DotNet using System.Collections.Generic; using System.Net; using System.Net.Http; - using System.Text; using System.Threading.Tasks; using System.Threading; using Models; @@ -226,7 +225,7 @@ private IDictionary RegistryAuthHeaders(AuthConfig authConfig) { { "X-Registry-Auth", - Convert.ToBase64String(Encoding.UTF8.GetBytes(this._client.JsonSerializer.SerializeObject(authConfig))) + Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(authConfig)) } }; } diff --git a/src/Docker.DotNet/JsonSerializer.cs b/src/Docker.DotNet/JsonSerializer.cs index b62937fd..c2143ad6 100644 --- a/src/Docker.DotNet/JsonSerializer.cs +++ b/src/Docker.DotNet/JsonSerializer.cs @@ -68,9 +68,9 @@ public T DeserializeObject(string json) return System.Text.Json.JsonSerializer.Deserialize(json, _options); } - public string SerializeObject(T value) + public byte[] SerializeObject(T value) { - return System.Text.Json.JsonSerializer.Serialize(value, _options); + return System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(value, _options); } public JsonContent GetHttpContent(T value)