{"record":{"id":"42fcfaeb6104a384","repo":"EllanJiang/GameFramework","slug":"can-not-convert-to-object-with-exception-0","errorCode":null,"errorMessage":"Can not convert to object with exception '{0}'.","messagePattern":"Can not convert to object with exception '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Json.cs","lineNumber":81,"sourceCode":"            public static T ToObject<T>(string json)\n            {\n                if (s_JsonHelper == null)\n                {\n                    throw new GameFrameworkException(\"JSON helper is invalid.\");\n                }\n\n                try\n                {\n                    return s_JsonHelper.ToObject<T>(json);\n                }\n                catch (Exception exception)\n                {\n                    if (exception is GameFrameworkException)\n                    {\n                        throw;\n                    }\n\n                    throw new GameFrameworkException(Text.Format(\"Can not convert to object with exception '{0}'.\", exception), exception);\n                }\n            }\n\n            /// <summary>\n            /// 将 JSON 字符串反序列化为对象。\n            /// </summary>\n            /// <param name=\"objectType\">对象类型。</param>\n            /// <param name=\"json\">要反序列化的 JSON 字符串。</param>\n            /// <returns>反序列化后的对象。</returns>\n            public static object ToObject(Type objectType, string json)\n            {\n                if (s_JsonHelper == null)\n                {\n                    throw new GameFrameworkException(\"JSON helper is invalid.\");\n                }\n\n                if (objectType == null)\n                {","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Json.cs#L63-L99","documentation":"Utility.Json.ToObject<T> wraps any exception thrown by the registered IJsonHelper.ToObject<T> implementation into a GameFrameworkException. This indicates the JSON string could not be deserialized into T. The InnerException contains the serializer's original error.","triggerScenarios":"Utility.Json.ToObject<T>(json) called with malformed JSON, JSON whose shape does not match T (wrong field types, missing required constructor parameters), or a null/empty string that the underlying serializer rejects.","commonSituations":"Loading config files saved with an older schema; hand-edited JSON config files with syntax errors; API/data responses that changed shape; deserializing empty files read from disk.","solutions":["Read the InnerException message to see the exact parse error (line/position or member mismatch)","Validate the JSON string with a linter or JSON.Parse in a test to confirm it is well-formed","Ensure the JSON shape matches type T (field names, casing, types) or add serializer attributes/contract settings","Check that saved configs come from a compatible serializer/version and migrate if needed"],"exampleFix":"// before\nvar cfg = Utility.Json.ToObject<Config>(emptyStringFromFile); // throws\n// after\nif (string.IsNullOrWhiteSpace(json)) { json = DefaultConfigJson; }\ntry { var cfg = Utility.Json.ToObject<Config>(json); }\ncatch (GameFrameworkException e) { Log.Error(\"Bad config JSON: {0}\", e); }","handlingStrategy":"try-catch","validationCode":"bool valid = !string.IsNullOrWhiteSpace(json) && json.TrimStart()[0] is '{' or '[';\nif (!valid) json = DefaultJson;","typeGuard":null,"tryCatchPattern":"try { var o = Utility.Json.ToObject<T>(json); }\ncatch (GameFrameworkException e) { Log.Error(\"Deserialize failed: {0}\", e.InnerException?.Message); o = new T(); }","preventionTips":["Validate/trim JSON strings read from files or network before deserializing","Version your serialized data and migrate on load","Keep type T in sync with the producing schema; test round-trips","Guard against empty/whitespace strings from file reads"],"tags":["json","deserialization","gameframework"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}