diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-02-16 14:12:22 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-16 14:12:22 -0600 |
| commit | 17bbfce9804757e70db74522a9cce386d10531b0 (patch) | |
| tree | 59ee4a41ceae9bc82e50ef1bc25756f10b72f71a /src | |
| parent | 183645f2e1d99c8e5ef1334ade9acfe399f82be1 (diff) | |
| parent | 5ac69d7fe558d75c0b887605f83b678ed0c10854 (diff) | |
| download | slskd-17bbfce9804757e70db74522a9cce386d10531b0.tar.gz | |
Merge pull request #1645 from slskd/search-files
Add KnownUnsupportedTypeConverter to prevent crashes when serializing some types
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Common/JsonConverters/KnownUnsupportedTypeConverter.cs | 54 | ||||
| -rw-r--r-- | src/slskd/Common/JsonConverters/TypeConverter.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Events/Types/EventRecord.cs | 8 |
4 files changed, 62 insertions, 40 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index c189837c..11d419b4 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -524,7 +524,7 @@ namespace slskd var options = new JsonSerializerOptions(); options.Converters.Add(new IPAddressConverter()); options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new TypeConverter()); + options.Converters.Add(new KnownUnsupportedTypeConverter()); options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; return options; diff --git a/src/slskd/Common/JsonConverters/KnownUnsupportedTypeConverter.cs b/src/slskd/Common/JsonConverters/KnownUnsupportedTypeConverter.cs new file mode 100644 index 00000000..e7523f36 --- /dev/null +++ b/src/slskd/Common/JsonConverters/KnownUnsupportedTypeConverter.cs @@ -0,0 +1,54 @@ +// <copyright file="KnownUnsupportedTypeConverter.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd; + +using System; +using System.Reflection; +using System.Runtime.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; + +/// <summary> +/// Convert all of the Types System.Text.Json refuses to serialize to a simple string containing +/// the name of the type. +/// </summary> +public class KnownUnsupportedTypeConverter : JsonConverter<object> +{ + public override bool CanConvert(Type typeToConvert) + { + return typeof(Type).IsAssignableFrom(typeToConvert) + || typeof(Assembly).IsAssignableFrom(typeToConvert) + || typeof(MemberInfo).IsAssignableFrom(typeToConvert) // covers MethodBase, MethodInfo, ConstructorInfo, FieldInfo, PropertyInfo, EventInfo + || typeof(Module).IsAssignableFrom(typeToConvert) + || typeof(Delegate).IsAssignableFrom(typeToConvert) // covers Action, Func, EventHandler, MulticastDelegate + || typeof(SerializationInfo).IsAssignableFrom(typeToConvert) + || typeToConvert == typeof(IntPtr) + || typeToConvert == typeof(UIntPtr); + } + + public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + reader.Skip(); + return default; + } + + public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) + { + writer.WriteStringValue(value?.GetType().FullName ?? "null"); + } +} diff --git a/src/slskd/Common/JsonConverters/TypeConverter.cs b/src/slskd/Common/JsonConverters/TypeConverter.cs deleted file mode 100644 index cb946667..00000000 --- a/src/slskd/Common/JsonConverters/TypeConverter.cs +++ /dev/null @@ -1,38 +0,0 @@ -// <copyright file="TypeConverter.cs" company="slskd Team"> -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// </copyright> - -namespace slskd; - -using System; -using System.Text.Json; -using System.Text.Json.Serialization; - -public class TypeConverter : JsonConverter<Type> -{ - public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Type); - - public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var typeName = reader.GetString(); - return typeName is null ? null : Type.GetType(typeName); - } - - public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.AssemblyQualifiedName); - } -}
\ No newline at end of file diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs index 4323a42b..685a090c 100644 --- a/src/slskd/Events/Types/EventRecord.cs +++ b/src/slskd/Events/Types/EventRecord.cs @@ -38,10 +38,16 @@ public record EventRecord private static JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + DefaultIgnoreCondition = JsonIgnoreCondition.Never, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = false, + Converters = + { + new IPAddressConverter(), + new JsonStringEnumConverter(), + new KnownUnsupportedTypeConverter(), + }, }; /// <summary> |
