Apply guid json converter only where we need it

This commit is contained in:
Archi
2024-03-17 02:44:49 +01:00
parent d7d24d5e47
commit ff55e09783
6 changed files with 9 additions and 14 deletions

View File

@@ -24,6 +24,7 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Helpers.Json;
@@ -32,9 +33,8 @@ namespace ArchiSteamFarm.Helpers.Json;
/// TODO: This class exists purely because STJ can't deserialize Guid in other formats than default, at least for now
/// https://github.com/dotnet/runtime/issues/30692
/// </summary>
internal sealed class GuidJsonConverter : JsonConverter<Guid> {
internal static readonly GuidJsonConverter Shared = new();
[PublicAPI]
public sealed class GuidJsonConverter : JsonConverter<Guid> {
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TryGetGuid(out Guid result)) {
// Great, we can work with it

View File

@@ -113,7 +113,6 @@ public static class JsonUtilities {
private static JsonSerializerOptions CreateDefaultJsonSerializerOptions(bool writeIndented = false) =>
new() {
AllowTrailingCommas = true,
Converters = { GuidJsonConverter.Shared },
PropertyNamingPolicy = null,
ReadCommentHandling = JsonCommentHandling.Skip,
TypeInfoResolver = new DefaultJsonTypeInfoResolver { Modifiers = { ApplyCustomModifiers } },

View File

@@ -31,7 +31,6 @@ using System.Net;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers.Json;
@@ -388,10 +387,6 @@ internal static class ArchiKestrel {
static options => {
JsonSerializerOptions jsonSerializerOptions = Debugging.IsUserDebugging ? JsonUtilities.IndentedJsonSerialierOptions : JsonUtilities.DefaultJsonSerialierOptions;
foreach (JsonConverter converter in jsonSerializerOptions.Converters) {
options.JsonSerializerOptions.Converters.Add(converter);
}
options.JsonSerializerOptions.PropertyNamingPolicy = jsonSerializerOptions.PropertyNamingPolicy;
options.JsonSerializerOptions.TypeInfoResolver = jsonSerializerOptions.TypeInfoResolver;
options.JsonSerializerOptions.WriteIndented = jsonSerializerOptions.WriteIndented;

View File

@@ -64,17 +64,17 @@ public sealed class InventoryDescription {
private init => Body.classid = value;
}
[JsonConverter(typeof(BooleanNumberConverter))]
[JsonInclude]
[JsonPropertyName("commodity")]
[JsonConverter(typeof(BooleanNumberConverter))]
public bool Commodity {
get => Body.commodity;
private init => Body.commodity = value;
}
[JsonConverter(typeof(BooleanNumberConverter))]
[JsonInclude]
[JsonPropertyName("currency")]
[JsonConverter(typeof(BooleanNumberConverter))]
public bool Currency {
get => Body.currency;
private init => Body.currency = value;
@@ -127,10 +127,10 @@ public sealed class InventoryDescription {
private init => Body.instanceid = value;
}
[JsonConverter(typeof(BooleanNumberConverter))]
[JsonInclude]
[JsonPropertyName("marketable")]
[JsonRequired]
[JsonConverter(typeof(BooleanNumberConverter))]
public bool Marketable {
get => Body.marketable;
private init => Body.marketable = value;
@@ -301,10 +301,10 @@ public sealed class InventoryDescription {
}
}
[JsonConverter(typeof(BooleanNumberConverter))]
[JsonInclude]
[JsonPropertyName("tradable")]
[JsonRequired]
[JsonConverter(typeof(BooleanNumberConverter))]
internal bool Tradable {
get => Body.tradable;
private init => Body.tradable = value;

View File

@@ -51,9 +51,9 @@ internal sealed class InventoryResponse : OptionalResultResponse {
[JsonPropertyName("last_assetid")]
internal ulong LastAssetID { get; private init; }
[JsonConverter(typeof(BooleanNumberConverter))]
[JsonInclude]
[JsonPropertyName("more_items")]
[JsonConverter(typeof(BooleanNumberConverter))]
internal bool MoreItems { get; private init; }
[JsonInclude]

View File

@@ -263,6 +263,7 @@ public sealed class GlobalConfig {
[JsonInclude]
public ArchiCryptoHelper.EHashingMethod IPCPasswordFormat { get; private init; } = DefaultIPCPasswordFormat;
[JsonConverter(typeof(GuidJsonConverter))]
[JsonInclude]
public Guid? LicenseID { get; private init; } = DefaultLicenseID;