From ebc4601ed4f84a5c658381e8473e9332edafe8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Sat, 8 Mar 2025 15:00:38 +0100 Subject: [PATCH] Add another useful json helper --- ArchiSteamFarm.Tests/Bot.cs | 3 +-- ArchiSteamFarm.Tests/MobileAuthenticator.cs | 2 +- ArchiSteamFarm/Helpers/Json/JsonUtilities.cs | 5 +++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ArchiSteamFarm.Tests/Bot.cs b/ArchiSteamFarm.Tests/Bot.cs index 8b72ec8c5..a69c41d26 100644 --- a/ArchiSteamFarm.Tests/Bot.cs +++ b/ArchiSteamFarm.Tests/Bot.cs @@ -26,7 +26,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; -using System.Text.Json; using System.Text.Json.Nodes; using ArchiSteamFarm.Core; using ArchiSteamFarm.Helpers.Json; @@ -50,7 +49,7 @@ internal sealed class Bot { throw new InvalidOperationException(nameof(constructor)); } - JsonElement emptyObject = new JsonObject().ToJsonElement(); + JsonObject emptyObject = new(); BotConfig? botConfig = emptyObject.ToJsonObject(); diff --git a/ArchiSteamFarm.Tests/MobileAuthenticator.cs b/ArchiSteamFarm.Tests/MobileAuthenticator.cs index a13bff2a1..29cf9277d 100644 --- a/ArchiSteamFarm.Tests/MobileAuthenticator.cs +++ b/ArchiSteamFarm.Tests/MobileAuthenticator.cs @@ -81,7 +81,7 @@ internal sealed class MobileAuthenticator { ["shared_secret"] = sharedSecret }; - Steam.Security.MobileAuthenticator? result = jsonObject.ToJsonElement().ToJsonObject(); + Steam.Security.MobileAuthenticator? result = jsonObject.ToJsonObject(); if (result == null) { throw new InvalidOperationException(nameof(result)); diff --git a/ArchiSteamFarm/Helpers/Json/JsonUtilities.cs b/ArchiSteamFarm/Helpers/Json/JsonUtilities.cs index fa2fd5633..d5750a537 100644 --- a/ArchiSteamFarm/Helpers/Json/JsonUtilities.cs +++ b/ArchiSteamFarm/Helpers/Json/JsonUtilities.cs @@ -28,6 +28,7 @@ using System.Linq; using System.Reflection; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization.Metadata; using System.Threading; using System.Threading.Tasks; @@ -55,6 +56,10 @@ public static class JsonUtilities { [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "We don't care about trimmed assemblies, as we need it to work only with the known (used) ones")] public static T? ToJsonObject(this JsonElement jsonElement) => jsonElement.Deserialize(DefaultJsonSerialierOptions); + [PublicAPI] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "We don't care about trimmed assemblies, as we need it to work only with the known (used) ones")] + public static T? ToJsonObject(this JsonNode jsonNode) => jsonNode.Deserialize(DefaultJsonSerialierOptions); + [PublicAPI] [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "We don't care about trimmed assemblies, as we need it to work only with the known (used) ones")] public static async ValueTask ToJsonObject(this Stream stream, CancellationToken cancellationToken = default) {