{"record":{"id":"32d61b1db0e576d0","repo":"EllanJiang/GameFramework","slug":"can-not-convert-to-json-with-exception-0","errorCode":null,"errorMessage":"Can not convert to JSON with exception '{0}'.","messagePattern":"Can not convert to JSON with exception '(.+?)'\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Json.cs","lineNumber":53,"sourceCode":"            public static string ToJson(object obj)\n            {\n                if (s_JsonHelper == null)\n                {\n                    throw new GameFrameworkException(\"JSON helper is invalid.\");\n                }\n\n                try\n                {\n                    return s_JsonHelper.ToJson(obj);\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 JSON with exception '{0}'.\", exception), exception);\n                }\n            }\n\n            /// <summary>\n            /// 将 JSON 字符串反序列化为对象。\n            /// </summary>\n            /// <typeparam name=\"T\">对象类型。</typeparam>\n            /// <param name=\"json\">要反序列化的 JSON 字符串。</param>\n            /// <returns>反序列化后的对象。</returns>\n            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                {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Json.cs#L35-L71","documentation":"Utility.Json.ToJson wraps any exception thrown by the registered IJsonHelper.ToJson implementation into a GameFrameworkException with this message. The library catches all exceptions from the helper and rethrows them uniformly so callers only need to handle GameFrameworkException. The inner exception preserves the original cause.","triggerScenarios":"Utility.Json.ToJson(obj) is called with a registered JSON helper whose serialization fails, e.g. the object graph contains circular references, unsupported types (delegates, Unity Object references), or the underlying serializer (Newtonsoft/LitJson) throws for the given object.","commonSituations":"Serializing Unity objects or classes with event handlers/circular references; switching JSON helper implementations and hitting serializer-specific limits; serializing types with read-only or incompatible properties.","solutions":["Inspect the InnerException (the original '{0}' text) to find the real serialization failure","Remove or mark unsupported members (circular refs, delegates, Unity Objects) with [JsonIgnore] or equivalent","Test the same object with the underlying serializer directly to confirm the cause","Register a different IJsonHelper implementation that handles your object graph"],"exampleFix":"// before\nUtility.Json.ToJson(gameObject); // circular/Unity refs -> throws\n// after\nvar dto = new SaveData { level = 1, items = 3 }; // plain data class\nstring json = Utility.Json.ToJson(dto);","handlingStrategy":"try-catch","validationCode":"if (obj == null) throw new ArgumentException(\"obj is null\");","typeGuard":null,"tryCatchPattern":"try { string json = Utility.Json.ToJson(obj); }\ncatch (GameFrameworkException e) { Log.Error(\"JSON serialize failed: {0}\", e.InnerException ?? e); }","preventionTips":["Serialize only plain data classes (POCOs), never Unity Objects or delegates","Mark non-serializable members with [JsonIgnore] or NonSerialized","Keep one well-tested IJsonHelper registered for the whole app","Round-trip test critical objects (ToJson -> ToObject) in unit tests"],"tags":["json","serialization","gameframework"],"backgroundTag":"json-marshal-failed","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}