EllanJiang/GameFramework · error · GameFrameworkException
Can not convert to JSON with exception
Error message
Can not convert to JSON with exception '{0}'. What it means
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.
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
Example fix
// before
Utility.Json.ToJson(gameObject); // circular/Unity refs -> throws
// after
var dto = new SaveData { level = 1, items = 3 }; // plain data class
string json = Utility.Json.ToJson(dto); Defensive patterns
Strategy: try-catch
Validate before calling
if (obj == null) throw new ArgumentException("obj is null"); Try / catch
try { string json = Utility.Json.ToJson(obj); }
catch (GameFrameworkException e) { Log.Error("JSON serialize failed: {0}", e.InnerException ?? e); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Serialized packet failure.
- Buffer is invalid.
- Start index is invalid.
- JSON helper is invalid.
- Can not convert to object with exception
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/32d61b1db0e576d0.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Utility/Utility.Json.cs:53
public static string ToJson(object obj)
{
if (s_JsonHelper == null)
{
throw new GameFrameworkException("JSON helper is invalid.");
}
try
{
return s_JsonHelper.ToJson(obj);
}
catch (Exception exception)
{
if (exception is GameFrameworkException)
{
throw;
}
throw new GameFrameworkException(Text.Format("Can not convert to JSON with exception '{0}'.", exception), exception);
}
}
/// <summary>
/// 将 JSON 字符串反序列化为对象。
/// </summary>
/// <typeparam name="T">对象类型。</typeparam>
/// <param name="json">要反序列化的 JSON 字符串。</param>
/// <returns>反序列化后的对象。</returns>
public static T ToObject<T>(string json)
{
if (s_JsonHelper == null)
{
throw new GameFrameworkException("JSON helper is invalid.");
}
try
{View on GitHub (pinned to d0c010b051)