dotnet serialization_error ai_generated true

System.Text.Json.JsonException: 检测到可能的对象循环。这可能是由于循环引用或对象深度超过最大允许深度 64。

System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64.

ID: dotnet/aspnetcore-json-cycle

其他格式: JSON · Markdown 中文 · English
90%修复率
86%置信度
1证据数
2023-07-12首次发现

版本兼容性

版本状态引入弃用备注
dotnet 6.0 active
dotnet 8.0 active
System.Text.Json 6.0 active
System.Text.Json 8.0 active

根因分析

JsonSerializer 在对象图中遇到循环引用(例如导航属性相互引用)或嵌套深度超过默认限制。

English

JsonSerializer encounters a circular reference in the object graph (e.g., navigation properties referencing each other) or the nesting depth exceeds the default limit.

generic

官方文档

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/preserve-references

解决方案

  1. Configure System.Text.Json to handle reference loops using ReferenceHandler.Preserve. Example in Program.cs.
  2. Use DTOs or view models that flatten the object graph, avoiding circular references.
  3. Set ReferenceHandler.IgnoreCycles to ignore navigation properties that cause cycles.

无效尝试

常见但无效的做法:

  1. 90% 失败

    Does not break the cycle; serializer will still fail with stack overflow or infinite loop.

  2. 70% 失败

    May hide needed data; can break client-side functionality if important relationships are omitted.

  3. 80% 失败

    Newtonsoft.Json also has a default reference loop handling that throws; needs explicit configuration.