diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-01-10 17:38:38 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-01-10 17:38:38 -0600 |
| commit | 41bf492f6e1c699abfc53e40a43ec011b16b6f61 (patch) | |
| tree | f8d58c17eeff09d9e438288ca217912acb7ff317 /src | |
| parent | 42c01f5626df717cd7c3cb45765572ad60573551 (diff) | |
| download | slskd-41bf492f6e1c699abfc53e40a43ec011b16b6f61.tar.gz | |
add type converter for serialization
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Common/JsonConverters/TypeConverter.cs | 38 |
2 files changed, 39 insertions, 0 deletions
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 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 |
