1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-30 08:08:33 +01:00
MLEM/MLEM.Data/Json/RectangleConverter.cs

26 lines
1.1 KiB
C#
Raw Normal View History

2020-02-27 18:56:49 +01:00
using System;
using System.Globalization;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
/// <inheritdoc />
2020-02-27 18:56:49 +01:00
public class RectangleConverter : JsonConverter<Rectangle> {
/// <inheritdoc />
2020-02-27 18:56:49 +01:00
public override void WriteJson(JsonWriter writer, Rectangle value, JsonSerializer serializer) {
writer.WriteValue(
value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture) + " " +
value.Width.ToString(CultureInfo.InvariantCulture) + " " + value.Height.ToString(CultureInfo.InvariantCulture));
}
/// <inheritdoc />
2020-02-27 18:56:49 +01:00
public override Rectangle ReadJson(JsonReader reader, Type objectType, Rectangle existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new Rectangle(
int.Parse(value[0], CultureInfo.InvariantCulture), int.Parse(value[1], CultureInfo.InvariantCulture),
int.Parse(value[2], CultureInfo.InvariantCulture), int.Parse(value[3], CultureInfo.InvariantCulture));
}
}
}