{"record":{"id":"799ff58a6c800a2b","repo":"egametang/ET","slug":"to-json-error-obj-gettype-fullname-e","errorCode":null,"errorMessage":"to json error {obj.GetType().FullName}\n{e}","messagePattern":"to json error (.+?)\n(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":36,"sourceCode":"            Indent = true, \n            IndentChars = \"\\t\", \n            NewLineChars = \"\\n\", \n            OutputMode = JsonOutputMode.Shell \n        };\n        \n        public static string ToJson(object obj)\n        {\n            try\n            {\n                if (obj is ISupportInitialize supportInitialize)\n                {\n                    supportInitialize.BeginInit();\n                }\n                return obj.ToJson(defaultSettings);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"to json error {obj.GetType().FullName}\\n{e}\");\n            }\n        }\n\n        public static string ToJson(object obj, JsonWriterSettings settings)\n        {\n            try\n            {\n                if (obj is ISupportInitialize supportInitialize)\n                {\n                    supportInitialize.BeginInit();\n                }\n                return obj.ToJson(settings);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"to json error {obj.GetType().FullName}\\n{e}\");\n            }\n        }","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L18-L54","documentation":"Thrown by ET.MongoHelper.ToJson(object) when the MongoDB BSON driver's obj.ToJson(defaultSettings) raises during JSON serialization. It wraps any inner failure into a single Exception whose Message embeds the target type FullName and the original exception text. Note: the rethrow at line 36 does NOT pass the original exception as InnerException (no ', e'), so the stack trace is lost and only the formatted string survives.","triggerScenarios":"Calling MongoHelper.ToJson(obj) on an object whose runtime type has no registered BsonClassMap/serializer, a type with an unserializable member (e.g. an interface or Dictionary with non-serializable key), a circular reference, or passing obj == null (which also makes the catch itself NullReference via obj.GetType()).","commonSituations":"Sharing entities/messages over JSON (network, logs, config dumps), serializing dynamically-typed or polymorphic objects without [BsonKnownTypes], migrating classes and forgetting to register class maps, or null args after a refactor.","solutions":["Inspect the embedded inner text in the message (the type FullName + inner message); fix the named type's serialization registration.","Ensure the type has a registered serializer: BsonSerializer.RegisterSerializer(...) or a BsonClassMap, and for polymorphism add [BsonDiscriminator]/[BsonKnownTypes].","If obj may be null, null-check before calling ToJson (the catch will itself NRE on obj.GetType()).","Consider passing the inner exception: throw new Exception(..., e) so the real stack trace is preserved."],"exampleFix":"// before\nstring s = MongoHelper.ToJson(maybeNullObj);\n\n// after\nif (maybeNullObj == null) return null;\ntry { return MongoHelper.ToJson(maybeNullObj); }\ncatch (Exception e) { Log.Error($\"serialize {maybeNullObj.GetType()} failed: {e}\"); throw; }","handlingStrategy":"try-catch","validationCode":"// validate before serialize\nif (obj == null) throw new ArgumentNullException(nameof(obj));\nif (!BsonSerializer.IsTypeRegistered(obj.GetType()))\n    throw new InvalidOperationException($\"no bson serializer for {obj.GetType().FullName}\");","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.ToJson(obj); }\ncatch (Exception e) { /* message already contains inner text; log obj type + e.Message */ Log.Error(e); throw; }","preventionTips":["Never pass null to MongoHelper.ToJson - the catch block NREs on obj.GetType().","Register BsonClassMaps at startup for all serialized types.","Add [BsonKnownTypes] on polymorphic bases."],"tags":["serialization","mongodb","json","null-safety"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}