bookmark-manager-r4/Text/Json/JsonSizeConverter.cs
2021-09-07 12:32:24 +01:00

41 lines
1.1 KiB
C#

using System;
using System.Drawing;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace FizzyLauncher.Text.Json
{
public class JsonSizeConverter : JsonConverter<Size>
{
public override Size Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Size rs = new Size(0, 0);
if (reader.TokenType == JsonTokenType.String)
{
string[] parts = reader.GetString().Split(',');
if (parts.Length != 2)
{
return rs;
}
int w = 0;
int h = 0;
if (!int.TryParse(parts[0].Trim(), out w)) w = 0;
if (!int.TryParse(parts[1].Trim(), out h)) h = 0;
return new Size(w, h);
}
return rs;
}
public override void Write(Utf8JsonWriter writer, Size value, JsonSerializerOptions options)
{
writer.WriteStringValue(string.Format("{0}, {1}", value.Width.ToString(), value.Height.ToString()));
}
}
}