using System; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.Xna.Framework; namespace MonoGame.Extended.Serialization.Json; /// /// Converts a value to or from JSON. /// public class Vector2JsonConverter : JsonConverter { /// public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Vector2); /// /// /// Thrown if the JSON property does not contain a properly formatted value /// public override Vector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var values = reader.ReadAsMultiDimensional(options); if (values.Length == 2) { return new Vector2(values[0], values[1]); } if (values.Length == 1) { return new Vector2(values[0]); } throw new JsonException("Invalid Vector2 property value"); } /// /// /// Throw if is . /// public override void Write(Utf8JsonWriter writer, Vector2 value, JsonSerializerOptions options) { ArgumentNullException.ThrowIfNull(writer); writer.WriteStringValue($"{value.X} {value.Y}"); } }