using Newtonsoft.Json; using System; using UnityEngine; namespace MetaCity.Core.Common { public class VectorConverter : JsonConverter { public override bool CanRead => true; public override bool CanWrite => true; public override bool CanConvert(Type objectType) { return typeof(Vector2) == objectType || typeof(Vector2Int) == objectType || typeof(Vector3) == objectType || typeof(Vector3Int) == objectType || typeof(Vector4) == objectType; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return objectType switch { var t when t == typeof(Vector2) => JsonConvert.DeserializeObject(serializer.Deserialize(reader) .ToString()), var t when t == typeof(Vector2Int) => JsonConvert.DeserializeObject(serializer .Deserialize(reader).ToString()), var t when t == typeof(Vector3) => JsonConvert.DeserializeObject(serializer.Deserialize(reader) .ToString()), var t when t == typeof(Vector3Int) => JsonConvert.DeserializeObject(serializer .Deserialize(reader).ToString()), var t when t == typeof(Vector4) => JsonConvert.DeserializeObject(serializer.Deserialize(reader) .ToString()), _ => throw new Exception("Unexpected Error Occurred"), }; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteStartObject(); switch (value) { case Vector2 v: writer.WritePropertyName("x"); writer.WriteValue(v.x); writer.WritePropertyName("y"); writer.WriteValue(v.y); break; case Vector2Int v: writer.WritePropertyName("x"); writer.WriteValue(v.x); writer.WritePropertyName("y"); writer.WriteValue(v.y); break; case Vector3 v: writer.WritePropertyName("x"); writer.WriteValue(v.x); writer.WritePropertyName("y"); writer.WriteValue(v.y); writer.WritePropertyName("z"); writer.WriteValue(v.z); break; case Vector3Int v: writer.WritePropertyName("x"); writer.WriteValue(v.x); writer.WritePropertyName("y"); writer.WriteValue(v.y); writer.WritePropertyName("z"); writer.WriteValue(v.z); break; case Vector4 v: writer.WritePropertyName("x"); writer.WriteValue(v.x); writer.WritePropertyName("y"); writer.WriteValue(v.y); writer.WritePropertyName("z"); writer.WriteValue(v.z); writer.WritePropertyName("w"); writer.WriteValue(v.w); break; default: throw new Exception("Unexpected Error Occurred"); } writer.WriteEndObject(); } } }