简体   繁体   English

将实体模型序列化为 JSON 时检测到可能的对象循环

[英]A possible object cycle was detected when serializing entity-model to JSON

I have a webapi project that written using C# and built on the top of ASP.NET Core/5 framework.我有一个使用C#编写并构建在 ASP.NET Core/5 框架之上的 webapi 项目。

The API return JSON data. API 返回 JSON 数据。

Here is an example of one endpoint这是一个端点的示例

[HttpGet]
public async Task<ActionResult<TModel[]>> QueryAsync(
   [FromQuery(Name = "filter")] string filter = null,
   [FromQuery(Name = "expand")] string expand = null)
{
    IQueryable<TModel> query = GetQuery(filter, expand);

    var models = await query.ToArrayAsync();

    return Ok(models);
}

In the above request, the expand will tell the server which navigation properties to expand.在上面的请求中, expand会告诉服务器要扩展哪些导航属性。 When the user request to expand a property, I get the following error当用户请求扩展属性时,我收到以下错误

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 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles

Following the instruction from the error, I added the following to the Startup class按照错误中的说明,我在 Startup 类中添加了以下内容

services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    // added this
    options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});

The above change did fix the problem!上述更改确实解决了问题! However, it introduced another problem.然而,它引入了另一个问题。 The new problem is that it changed the structure of the output.新的问题是它改变了输出的结构。

The output went from something like this输出来自这样的东西

{
  // properties
 
  "relations": [
     {
         // relation 1
         "parent": null
     },
     {
         // relation 2
         "parent": null
     }
   ]
}

to something like this像这样的事情

{
  "$id": "1",
  // properties
 
  "relations": {
     "$id": "2",
     "$values": [
         {
             // relation 1 properties
             "$id": "3",
             "parent": {
                 "$ref": 1
             }
         },
         {
            // relation 2 properties
            "$id": "3",
            "parent": {
                 "$ref": 1
             }
         }
     ]
   }
}

Is there a way to not change the structure of the output and instead ignore the circular reference?有没有办法不改变输出的结构而是忽略循环引用?

There is a similar question here, where one of the answers mentions the same issue you're having: .Net Core 3.0 possible object cycle was detected which is not supported这里有一个类似的问题,其中一个答案提到了您遇到的相同问题: .Net Core 3.0 possible object cycle was detected that is not supported

Some of the things mentioned there:那里提到的一些事情:

If you're on .NET 6 you can use如果您使用的是 .NET 6,则可以使用

JsonSerializerOptions options = new()
{
    ReferenceHandler = ReferenceHandler.IgnoreCycles,
    WriteIndented = true
};

alternatively, depending on what you need to achieve, you might want to just ignore looping properties altogether.或者,根据您需要实现的目标,您可能只想完全忽略循环属性。

The proper solution though would be to find out where exactly your references are looping and exclude the property(s) in question from serialization, or supply them in a format that doesn't loop (such as an id or similar)正确的解决方案是找出引用的确切位置循环并从序列化中排除有问题的属性,或者以不循环的格式(例如 id 或类似格式)提供它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 System.Text.Json.JsonException:在实体框架中检测到可能的 object 循环? - How to resolve System.Text.Json.JsonException: A possible object cycle was detected in Entity Framework? 可能检测到 Object 周期 - Possible Object Cycle Detected 实体到 json 错误 - 序列化类型对象时检测到循环引用 - Entity to json error - A circular reference was detected while serializing an object of type 反序列化 Json .NET CORE 5 - JsonException 时出错:检测到可能的 object 循环不支持 - Error when deserialize Json .NET CORE 5 - JsonException: A possible object cycle was detected which is not supported System.Text.Json.JsonException:检测到可能的 object 循环 - System.Text.Json.JsonException: A possible object cycle was detected 检测到不支持的可能的对象循环 - A possible object cycle was detected which is not supported C# 循环引用。 System.Text.Json.JsonException:检测到可能的对象循环 .NET 5 - C# Circular reference. System.Text.Json.JsonException: A possible object cycle was detected .NET 5 Azure CosmosDB + NetCore 3.1:System.Text.Json.JsonException:检测到可能的 object 循环不支持 - Azure CosmosDB + NetCore 3.1 : System.Text.Json.JsonException: A possible object cycle was detected which is not supported System.Text.Json.JsonException:&#39;检测到不支持的可能对象循环......&#39; - System.Text.Json.JsonException: 'A possible object cycle was detected which is not supported....' 序列化 JSON 对象时结果为“/” - "/" in result when serializing a JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM