增加对Color的定制序列化逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Win32;
|
||||
using FFMpegCore;
|
||||
using Microsoft.Win32;
|
||||
using NLog;
|
||||
using SpineViewer.Models;
|
||||
using SpineViewer.Services;
|
||||
@@ -9,7 +10,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace SpineViewer.Utils
|
||||
{
|
||||
@@ -17,6 +20,11 @@ namespace SpineViewer.Utils
|
||||
{
|
||||
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
static JsonHelper()
|
||||
{
|
||||
_jsonOptions.Converters.Add(new ColorJsonConverter());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存 Json 文件的格式参数
|
||||
/// </summary>
|
||||
@@ -85,4 +93,28 @@ namespace SpineViewer.Utils
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class ColorJsonConverter : JsonConverter<Color>
|
||||
{
|
||||
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// 解析 JSON 对象
|
||||
var jsonObject = JsonDocument.ParseValue(ref reader).RootElement;
|
||||
var r = jsonObject.GetProperty("R").GetByte();
|
||||
var g = jsonObject.GetProperty("G").GetByte();
|
||||
var b = jsonObject.GetProperty("B").GetByte();
|
||||
var a = jsonObject.GetProperty("A").GetByte();
|
||||
return Color.FromArgb(a, r, g, b);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WriteNumber("R", value.R);
|
||||
writer.WriteNumber("G", value.G);
|
||||
writer.WriteNumber("B", value.B);
|
||||
writer.WriteNumber("A", value.A);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user