{"record":{"id":"2bad1961842735f8","repo":"egametang/ET","slug":"from-json-error-str-e","errorCode":null,"errorMessage":"from json error: {str}\n{e}","messagePattern":"from json error: (.+?)\n(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":64,"sourceCode":"                    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        }\n\n        public static T FromJson<T>(string str)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize<T>(str);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from json error: {str}\\n{e}\");\n            }\n        }\n\n        public static object FromJson(Type type, string str)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize(str, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from json error: {str}\\n{e}\");\n            }\n        }\n\n        public static byte[] Serialize(object obj)\n        {\n            try","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L46-L82","documentation":"Thrown by MongoHelper.FromJson<T>(string) when BsonSerializer.Deserialize<T>(str) fails. The helper re-wraps into a new Exception whose message includes the raw input string and the original exception text; here it DOES preserve the inner exception via ', e' (line 64), so the stack trace survives.","triggerScenarios":"Deserializing malformed JSON, JSON whose values do not map onto T (wrong field types, missing required fields), a JSON containing an unknown _t discriminator, or passing null/empty str.","commonSituations":"Loading saved entity state from JSON, reading config/network messages, schema migration where T changed but stored JSON did not, hand-edited JSON files, null strings after a failed read.","solutions":["Inspect the inner exception (preserved) and the printed input string to find the offending field/token.","Align T with the JSON: add missing members, register BsonClassMap, or add [BsonIgnoreExtraElements] for forward-compat.","Validate the string is non-empty and well-formed JSON before deserializing.","For migrated types use [BsonDefaultValue]/[BsonIgnoreExtraElements] to tolerate schema drift."],"exampleFix":"// before\nvar e = MongoHelper.FromJson<MyEntity>(json);\n\n// after\nif (string.IsNullOrWhiteSpace(json)) throw new ArgumentException(\"empty json\", nameof(json));\nvar e = MongoHelper.FromJson<MyEntity>(json);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(str)) throw new ArgumentException(\"json is empty\", nameof(str));","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.FromJson<T>(str); }\ncatch (Exception e) { Log.Error($\"FromJson<{typeof(T).Name}> failed: {e}\"); throw; }","preventionTips":["Tolerate schema drift with [BsonIgnoreExtraElements] on persisted types.","Validate input is non-null/non-empty first.","Keep a schema version field in persisted JSON."],"tags":["serialization","mongodb","json","schema-migration"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}