using System;
using System.Globalization;
using MLEM.Misc;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
///
/// Converts a to and from JSON
///
public class RectangleFConverter : JsonConverter {
/// Writes the JSON representation of the object.
/// The to write to.
/// The value.
/// The calling serializer.
public override void WriteJson(JsonWriter writer, RectangleF 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));
}
/// Reads the JSON representation of the object.
/// The to read from.
/// Type of the object.
/// The existing value of object being read. If there is no existing value then null will be used.
/// The existing value has a value.
/// The calling serializer.
/// The object value.
public override RectangleF ReadJson(JsonReader reader, Type objectType, RectangleF existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new RectangleF(
float.Parse(value[0], CultureInfo.InvariantCulture), float.Parse(value[1], CultureInfo.InvariantCulture),
float.Parse(value[2], CultureInfo.InvariantCulture), float.Parse(value[3], CultureInfo.InvariantCulture));
}
}
}