{"record":{"id":"5390964ac196801f","repo":"CoplayDev/unity-mcp","slug":"cannot-deserialize-vector2-from-token-type-to","errorCode":null,"errorMessage":"Cannot deserialize Vector2 from {token.Type}: '{token}'","messagePattern":"Cannot deserialize Vector2 from (.+?): '(.+?)'","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs","lineNumber":59,"sourceCode":"    public class Vector2Converter : JsonConverter<Vector2>\n    {\n        public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer)\n        {\n            writer.WriteStartObject();\n            writer.WritePropertyName(\"x\");\n            writer.WriteValue(value.x);\n            writer.WritePropertyName(\"y\");\n            writer.WriteValue(value.y);\n            writer.WriteEndObject();\n        }\n\n        public override Vector2 ReadJson(JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer)\n        {\n            JToken token = JToken.Load(reader);\n            if (token is JArray arr && arr.Count >= 2)\n                return new Vector2((float)arr[0], (float)arr[1]);\n            if (token is not JObject jo)\n                throw new JsonSerializationException($\"Cannot deserialize Vector2 from {token.Type}: '{token}'\");\n            return new Vector2(\n                (float)jo[\"x\"],\n                (float)jo[\"y\"]\n            );\n        }\n    }\n\n    public class QuaternionConverter : JsonConverter<Quaternion>\n    {\n        public override void WriteJson(JsonWriter writer, Quaternion value, JsonSerializer serializer)\n        {\n            writer.WriteStartObject();\n            writer.WritePropertyName(\"x\");\n            writer.WriteValue(value.x);\n            writer.WritePropertyName(\"y\");\n            writer.WriteValue(value.y);\n            writer.WritePropertyName(\"z\");\n            writer.WriteValue(value.z);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs#L41-L77","documentation":"Thrown by Vector2Converter.ReadJson when the JSON token is neither a JArray with count >= 2 nor a JObject. Vector2 accepts [x, y] array form or {\"x\":..,\"y\":..} object form. Any other token type or an array with fewer than 2 elements is rejected.","triggerScenarios":"Deserializing a Vector2 field from JSON where the value is a scalar, string, boolean, or array with < 2 elements. Common when a 2D coordinate is sent as a single value or a comma-separated string.","commonSituations":"Client sends UV or 2D scale as a single number; AI outputs a string for a vector field; array with one element; type confusion between Vector2 and Vector3.","solutions":["Send Vector2 as [x, y] array: \"scale\": [1.0, 1.0].","Or send as object: \"scale\": {\"x\": 1.0, \"y\": 1.0}.","Ensure arrays have exactly 2 numeric elements.","Do not pass scalar or string values for vector fields."],"exampleFix":"// before\nparams = {\"scale\": \"1,1\"}\n// after\nparams = {\"scale\": [1.0, 1.0]}","handlingStrategy":"type-guard","validationCode":"def is_valid_vector2(val) -> bool:\n    if isinstance(val, (list, tuple)) and len(val) >= 2:\n        return all(isinstance(v, (int, float)) for v in val[:2])\n    if isinstance(val, dict):\n        return all(k in val for k in ('x', 'y'))\n    return False","typeGuard":"function isVector2(val: unknown): val is number[] | {x:number;y:number} {\n  if (Array.isArray(val) && val.length >= 2)\n    return val.slice(0, 2).every(v => typeof v === 'number');\n  if (val && typeof val === 'object' && 'x' in val && 'y' in val)\n    return true;\n  return false;\n}","tryCatchPattern":"try\n{\n    var vec = serializer.Deserialize<Vector2>(reader);\n}\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Cannot deserialize Vector2\"))\n{\n    return new ErrorResponse($\"Vector2 must be [x,y] or {{x,y}}.\");\n}","preventionTips":["Send Vector2 as [x, y] arrays with exactly 2 numeric elements.","Don't confuse Vector2 with Vector3 — check the field's expected type.","Validate on the client before sending."],"tags":["serialization","vector2","json","deserialization","type-converter"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}