egametang/ET · error · Exception

from json error

Error message

from json error: {str}
{e}

What it means

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.

Solutions

  1. Inspect the inner exception (preserved) and the printed input string to find the offending field/token.
  2. Align T with the JSON: add missing members, register BsonClassMap, or add [BsonIgnoreExtraElements] for forward-compat.
  3. Validate the string is non-empty and well-formed JSON before deserializing.
  4. For migrated types use [BsonDefaultValue]/[BsonIgnoreExtraElements] to tolerate schema drift.

Example fix

// before
var e = MongoHelper.FromJson<MyEntity>(json);

// after
if (string.IsNullOrWhiteSpace(json)) throw new ArgumentException("empty json", nameof(json));
var e = MongoHelper.FromJson<MyEntity>(json);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(str)) throw new ArgumentException("json is empty", nameof(str));

Try / catch

try { return MongoHelper.FromJson<T>(str); }
catch (Exception e) { Log.Error($"FromJson<{typeof(T).Name}> failed: {e}"); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/2bad1961842735f8. Report an issue: GitHub.

Appendix: source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs:64

                    supportInitialize.BeginInit();
                }
                return obj.ToJson(settings);
            }
            catch (Exception e)
            {
                throw new Exception($"to json error {obj.GetType().FullName}\n{e}");
            }
        }

        public static T FromJson<T>(string str)
        {
            try
            {
                return BsonSerializer.Deserialize<T>(str);
            }
            catch (Exception e)
            {
                throw new Exception($"from json error: {str}\n{e}");
            }
        }

        public static object FromJson(Type type, string str)
        {
            try
            {
                return BsonSerializer.Deserialize(str, type);
            }
            catch (Exception e)
            {
                throw new Exception($"from json error: {str}\n{e}");
            }
        }

        public static byte[] Serialize(object obj)
        {
            try

View on GitHub (pinned to 5cab01f7a8)