41 lines
1.1 KiB
C#
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 JsonPointConverter : JsonConverter<Point>
|
|||
|
{
|
|||
|
public override Point Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|||
|
{
|
|||
|
Point rs = new Point(0, 0);
|
|||
|
|
|||
|
if (reader.TokenType == JsonTokenType.String)
|
|||
|
{
|
|||
|
string[] parts = reader.GetString().Split(',');
|
|||
|
if (parts.Length != 2)
|
|||
|
{
|
|||
|
return rs;
|
|||
|
}
|
|||
|
|
|||
|
int x = 0;
|
|||
|
int y = 0;
|
|||
|
|
|||
|
if (!int.TryParse(parts[0].Trim(), out x)) x = 0;
|
|||
|
if (!int.TryParse(parts[1].Trim(), out y)) y = 0;
|
|||
|
|
|||
|
return new Point(x, y);
|
|||
|
}
|
|||
|
|
|||
|
return rs;
|
|||
|
}
|
|||
|
|
|||
|
public override void Write(Utf8JsonWriter writer, Point value, JsonSerializerOptions options)
|
|||
|
{
|
|||
|
writer.WriteStringValue(string.Format("{0}, {1}", value.X.ToString(), value.Y.ToString()));
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|