首页 > 其他分享 >序列化成Json时,多个对象互相引用导致死循环 Text.Json.JsonException: A possible object cycle was detected.

序列化成Json时,多个对象互相引用导致死循环 Text.Json.JsonException: A possible object cycle was detected.

时间:2022-09-29 19:00:30浏览次数:58  
标签:JsonException Text object 死循环 Delivery Json Order cycle

错误: 当两个类中的属性互相引用时,导致对象实例序列化成Json时死循环,错误如下:

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 32.
Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
Path: $.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Delivery.Order.Id.

 解决方法:

在 Program.cs中添加 AddJsonOptions 选项配置,此配置Json不但不会死循环而且还能完整表达实例之间的互相引用关系。

builder.Services.AddControllers()
    .AddJsonOptions(option => option.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve);

增加AddJsonOptions 后的结果:

{
  "$id": "1",
  "id": 1,
  "companyName": "蜗牛快递",
  "number": "SN356565565",
  "order": {
    "$id": "2",
    "id": 1,
    "name": "USB 充电器",
    "address": "湖南省长沙市岳阳临湘大市场",
    "delivery": {
      "$ref": "1"
    }
  },
  "orderId": 1
}

  

标签:JsonException,Text,object,死循环,Delivery,Json,Order,cycle
From: https://www.cnblogs.com/friend/p/16742643.html

相关文章

  • 跨域 jsonp/CROS
    jsonp是jsonwithpadding的缩写,它不属于Ajax请求,但它可以模拟Ajax请求。封装jsonp 文件functionjsonp(options){//动态创建script标签varscri......
  • 返回Json对象不忽略null值SerializerFeature.WriteMapNullValue)
    Fastjson的SerializerFeature序列化属性QuoteFieldNames———-输出key时是否使用双引号,默认为trueWriteMapNullValue——–是否输出值为null的字段,默认为falseWriteN......
  • LeetCode 2296. Design a Text Editor
    原题链接在这里:https://leetcode.com/problems/design-a-text-editor/题目:Designatexteditorwithacursorthatcandothefollowing:Add texttowherethecu......
  • hive中使用hive原生的json报错的问题
    默认我们使用的hive自带的json包格式,创建表时格式为:CREATEEXTERNALTABLE`ods.ods_test`(...)ROWFORMATSERDE'org.apache.hive.hcatalog.data.JsonSerDe'STO......
  • jQuery的$.getJSON()方法在浏览器上不能请求到本地文件
    前言今天运行很久之前写的一个echarts页面,当时是练习写demo之前写了关于地图动态的一个图表,突然显示不成功,并且报错发现了是jQuery的$.getJSON()这个方法在浏览器上不能......
  • JSON APIs and Ajax
    JSONAPIsandAjaxonclick希望代码仅在页面完成加载后执行。将名为DOMContentLoaded的JavaScript事件附加到document中。以下是实现的代码:document.addEventList......
  • 项目使用AD域控,但HttpContext.Current.User.Identity.Name=""如何解决
    项目需求:用户直接用登陆电脑的AD账号登陆系统(防止多个账号公用),权限通过获取用户所在的group来配置(group在anywhere上统一管理)。获取当前用户group的接口:///<summary>/......
  • WPF 修复 ContextMenu 在开启 PerMonitorV2 后所用 DPI 错误
    本文告诉大家如何修复WPF的ContextMenu在开启PerMonitorV2之后,在双屏不同的DPI的设备上,在副屏弹出的ContextMenu使用了主屏的DPI导致缩放错误的问题关于什么......
  • ApplicationDbContext配置
    一、appsetting.json声明连接字符串"ConnectionStrings":{"DefaultConnection":"Server=DESKTOP-DABHN6U\\MSSQLSERVER2014;uid=sa;pwd=Lz38275292;database=SP......
  • Pythontext_3
    1实例一:输出每日一帖2importdatetime#导入日期时间表3#定义一个列表4mot=["今天星期一:\n坚持下去不是因为我很坚强,而是......