New json converter implemented.

This commit is contained in:
BaronGreenback 2020-12-21 09:01:59 +00:00
parent 7a66761981
commit 3633996a53
3 changed files with 30 additions and 0 deletions

View File

@ -9,6 +9,7 @@ using System.Text.Json;
using MediaBrowser.Common;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Json.Converters;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Plugins;
@ -50,6 +51,7 @@ namespace Emby.Server.Implementations.Plugins
_pluginsPath = pluginsPath;
_appVersion = appVersion ?? throw new ArgumentNullException(nameof(appVersion));
_jsonOptions = JsonDefaults.GetOptions();
_jsonOptions.Converters.Add(new JsonGuidDashConverter());
_jsonOptions.WriteIndented = true;
_config = config;
_appHost = appHost;

View File

@ -0,0 +1,26 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MediaBrowser.Common.Json.Converters
{
/// <summary>
/// Converts a GUID object or value to/from JSON.
/// </summary>
public class JsonGuidDashConverter : JsonConverter<Guid>
{
/// <inheritdoc />
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var guidStr = reader.GetString();
return guidStr == null ? Guid.Empty : new Guid(guidStr);
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}

View File

@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Text.Json.Serialization;
using MediaBrowser.Common.Json.Converters;
using MediaBrowser.Model.Plugins;
namespace MediaBrowser.Common.Plugins
@ -32,6 +33,7 @@ namespace MediaBrowser.Common.Plugins
/// Gets or sets the Global Unique Identifier for the plugin.
/// </summary>
[JsonPropertyName("guid")]
[JsonConverter(typeof(JsonGuidDashConverter))]
public Guid Id { get; set; }
/// <summary>