EllanJiang/GameFramework · error · GameFrameworkException

Can not convert to object with exception

Error message

Can not convert to object with exception '{0}'.

What it means

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.

Solutions

  1. Read the InnerException message to see the exact parse error (line/position or member mismatch)
  2. Validate the JSON string with a linter or JSON.Parse in a test to confirm it is well-formed
  3. Ensure the JSON shape matches type T (field names, casing, types) or add serializer attributes/contract settings
  4. Check that saved configs come from a compatible serializer/version and migrate if needed

Example fix

// before
var cfg = Utility.Json.ToObject<Config>(emptyStringFromFile); // throws
// after
if (string.IsNullOrWhiteSpace(json)) { json = DefaultConfigJson; }
try { var cfg = Utility.Json.ToObject<Config>(json); }
catch (GameFrameworkException e) { Log.Error("Bad config JSON: {0}", e); }
Defensive patterns

Strategy: try-catch

Validate before calling

bool valid = !string.IsNullOrWhiteSpace(json) && json.TrimStart()[0] is '{' or '[';
if (!valid) json = DefaultJson;

Try / catch

try { var o = Utility.Json.ToObject<T>(json); }
catch (GameFrameworkException e) { Log.Error("Deserialize failed: {0}", e.InnerException?.Message); o = new T(); }

Prevention

When it happens

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

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

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/42fcfaeb6104a384. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Utility/Utility.Json.cs:81

            public static T ToObject<T>(string json)
            {
                if (s_JsonHelper == null)
                {
                    throw new GameFrameworkException("JSON helper is invalid.");
                }

                try
                {
                    return s_JsonHelper.ToObject<T>(json);
                }
                catch (Exception exception)
                {
                    if (exception is GameFrameworkException)
                    {
                        throw;
                    }

                    throw new GameFrameworkException(Text.Format("Can not convert to object with exception '{0}'.", exception), exception);
                }
            }

            /// <summary>
            /// 将 JSON 字符串反序列化为对象。
            /// </summary>
            /// <param name="objectType">对象类型。</param>
            /// <param name="json">要反序列化的 JSON 字符串。</param>
            /// <returns>反序列化后的对象。</returns>
            public static object ToObject(Type objectType, string json)
            {
                if (s_JsonHelper == null)
                {
                    throw new GameFrameworkException("JSON helper is invalid.");
                }

                if (objectType == null)
                {

View on GitHub (pinned to d0c010b051)