Add another useful json helper

This commit is contained in:
Łukasz Domeradzki
2025-03-08 15:00:38 +01:00
parent a478c02967
commit ebc4601ed4
3 changed files with 7 additions and 3 deletions

View File

@@ -26,7 +26,6 @@ using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using ArchiSteamFarm.Core; using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers.Json; using ArchiSteamFarm.Helpers.Json;
@@ -50,7 +49,7 @@ internal sealed class Bot {
throw new InvalidOperationException(nameof(constructor)); throw new InvalidOperationException(nameof(constructor));
} }
JsonElement emptyObject = new JsonObject().ToJsonElement(); JsonObject emptyObject = new();
BotConfig? botConfig = emptyObject.ToJsonObject<BotConfig>(); BotConfig? botConfig = emptyObject.ToJsonObject<BotConfig>();

View File

@@ -81,7 +81,7 @@ internal sealed class MobileAuthenticator {
["shared_secret"] = sharedSecret ["shared_secret"] = sharedSecret
}; };
Steam.Security.MobileAuthenticator? result = jsonObject.ToJsonElement().ToJsonObject<Steam.Security.MobileAuthenticator>(); Steam.Security.MobileAuthenticator? result = jsonObject.ToJsonObject<Steam.Security.MobileAuthenticator>();
if (result == null) { if (result == null) {
throw new InvalidOperationException(nameof(result)); throw new InvalidOperationException(nameof(result));

View File

@@ -28,6 +28,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Metadata; using System.Text.Json.Serialization.Metadata;
using System.Threading; using System.Threading;
using System.Threading.Tasks; 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")] [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<T>(this JsonElement jsonElement) => jsonElement.Deserialize<T>(DefaultJsonSerialierOptions); public static T? ToJsonObject<T>(this JsonElement jsonElement) => jsonElement.Deserialize<T>(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<T>(this JsonNode jsonNode) => jsonNode.Deserialize<T>(DefaultJsonSerialierOptions);
[PublicAPI] [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")] [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<T?> ToJsonObject<T>(this Stream stream, CancellationToken cancellationToken = default) { public static async ValueTask<T?> ToJsonObject<T>(this Stream stream, CancellationToken cancellationToken = default) {