diff --git a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs index a310bddb..409d9ea8 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs @@ -14,7 +14,7 @@ namespace SPTarkov.Server.Core.Utils; [Injectable(InjectionType.Singleton)] public class JsonUtil { - private static readonly JsonSerializerOptions jsonSerializerOptionsNoIndent = new() + private static JsonSerializerOptions jsonSerializerOptionsNoIndent = new() { WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, @@ -55,7 +55,7 @@ public class JsonUtil } }; - private static readonly JsonSerializerOptions jsonSerializerOptionsIndented = new(jsonSerializerOptionsNoIndent) + private static JsonSerializerOptions jsonSerializerOptionsIndented = new(jsonSerializerOptionsNoIndent) { WriteIndented = true }; @@ -154,4 +154,43 @@ public class JsonUtil { return obj == null ? null : JsonSerializer.Serialize(obj, type, indented ? jsonSerializerOptionsIndented : jsonSerializerOptionsNoIndent); } + + private void AddConverter(JsonSerializerOptions options, JsonConverter newConverter) + { + if (!options.Converters.Any(c => c.GetType() == newConverter.GetType())) + { + options.Converters.Add(newConverter); + } + } + + /// + /// Register a Json converter to serializer options + /// + /// The converter to add + public void RegisterJsonConverter(JsonConverter converter) + { + // This might actually be a terrible thing to do, but it is what it is for now + + if (!jsonSerializerOptionsNoIndent.IsReadOnly) + { + AddConverter(jsonSerializerOptionsNoIndent, converter); + } + else + { + var noIndentConverter = new JsonSerializerOptions(jsonSerializerOptionsNoIndent); + AddConverter(noIndentConverter, converter); + jsonSerializerOptionsNoIndent = noIndentConverter; + } + + if (!jsonSerializerOptionsIndented.IsReadOnly) + { + AddConverter(jsonSerializerOptionsIndented, converter); + } + else + { + var indentedConverter = new JsonSerializerOptions(jsonSerializerOptionsIndented); + AddConverter(indentedConverter, converter); + jsonSerializerOptionsIndented = indentedConverter; + } + } }