From 41bf492f6e1c699abfc53e40a43ec011b16b6f61 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Jan 2026 17:38:38 -0600 Subject: add type converter for serialization --- src/slskd/Common/CommonExtensions.cs | 1 + src/slskd/Common/JsonConverters/TypeConverter.cs | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/slskd/Common/JsonConverters/TypeConverter.cs (limited to 'src') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index ec76b745..815c0eb4 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -490,6 +490,7 @@ namespace slskd var options = new JsonSerializerOptions(); options.Converters.Add(new IPAddressConverter()); options.Converters.Add(new JsonStringEnumConverter()); + options.Converters.Add(new TypeConverter()); options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; return options; diff --git a/src/slskd/Common/JsonConverters/TypeConverter.cs b/src/slskd/Common/JsonConverters/TypeConverter.cs new file mode 100644 index 00000000..cb946667 --- /dev/null +++ b/src/slskd/Common/JsonConverters/TypeConverter.cs @@ -0,0 +1,38 @@ +// +// 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/. +// + +namespace slskd; + +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +public class TypeConverter : JsonConverter +{ + 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 -- cgit v1.2.3