首页 > 其他分享 >fastjson中$ref的坑

fastjson中$ref的坑

时间:2023-05-12 16:14:23浏览次数:40  
标签:fastjson index name propB json test ref

相信不少人都遇到过,用fastjson进行序列化时会遇到属性出现$ref的情况,本质是fastjson在处理对象引用时默认不展开,需要自己指定序列化参数。写个DEMO演示一下:

 1 JSONObject json = new JSONObject();
 2 JSONObject prop = new JSONObject();
 3 prop.put("name", "test");
 4 prop.put("index", "1");
 5 json.put("propA", prop);        //引用同一个对象
 6 json.put("propB", prop);        //引用同一个对象
 7 
 8 //用不同方式序列化
 9 System.out.println(JSON.toJSONString(json));
10 System.out.println(json.toJSONString());
11 System.out.println(json.toString(SerializerFeature.DisableCircularReferenceDetect));
12 System.out.println(JSON.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect));

输出结果如下:

{"propB":{"name":"test","index":"1"},"propA":{"$ref":"$.propB"}}
{"propB":{"name":"test","index":"1"},"propA":{"$ref":"$.propB"}}
{"propB":{"name":"test","index":"1"},"propA":{"name":"test","index":"1"}}
{"propB":{"name":"test","index":"1"},"propA":{"name":"test","index":"1"}}

可以看到序列化时需要加 SerializerFeature.DisableCircularReferenceDetect 才能得到正确的结果。

需要说明的是这个问题仅出现在fastjson 1.x版中,升级到fastjson 2.x后就没这个问题了,但是2.x部分API与之前不同,升级有一定的成本。

 

标签:fastjson,index,name,propB,json,test,ref
From: https://www.cnblogs.com/BoyTNT/p/17394470.html

相关文章

  • react Ref 什么时候用,怎么用
    何时使用ref通常,当你的组件需要“跳出”React并与外部API通信时,你会用到ref——通常是不会影响组件外观的浏览器API。以下是这些罕见情况中的几个:存储timeoutID存储和操作DOM元素存储不需要被用来计算JSX的其他对象。如果你的组件需要存储一些值,但不影......
  • ref 的用法
    document.getElementById("p").innerHTML相当于 this.$refs.pp.innerHTML this.ref获取指定的元素//使用ref函数来声明对象类型的数据源:具备响应式letemp=ref({salary:7000,name:'Jack'})//通过ref函数声明的响应式数据,需要使用.value来获取数据的......
  • 微软Bing正面对阵谷歌!竞标争夺Firefox默认搜索引擎
    在今早的谷歌I/O大会上,谷歌带来了全新的PaLM2人工智能语言模型,并将用该模型升级Bard对话机器人以及谷歌搜索。但面对来势汹汹的谷歌,微软似乎并不打算退却。根据TheInformation的消息,微软Bing部门的负责人希望与Mozilla达成合作,让Bing取代谷歌成为Firefox浏览器的默认搜索引擎......
  • nginx 10061: No connection could be made because the target machine actively ref
    nginx10061:Noconnectioncouldbemadebecausethetargetmachineactivelyrefusedit nginx重载配置一直有些不生效,查看后,发现nginx有4个,全部关闭掉,再重开nginx,正常了nginx.exe-squit,可以把正常的nginx退出掉,其他的nginx,任务管理器强制关闭掉startnginx开启nginx,o......
  • java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组
    1.java代码中fastjson生成字符串和解析字符串的方法List<TemplateFull>templateFulls=newArrayList<TemplateFull>();JSONArrayjsonArr=newJSONArray();jsonArr.addAll(templateFulls);StringjsonStr=jsonArr.toJSONString();System.out.pr......
  • location.href和location.replace的区别
    情景比如支付过程中或者使用商品的优惠券,而使用这张优惠券需要取请求一个第三方的地址,中间会有一次跳转。若使用window.location.href=“url”,按流程操作是没问题的,但是如果用户点击返回,则无法跳回原本的提交订单的页面,会一直进行重复请求,造成程序出错。所以,必须替换成wind......
  • vue3 Type 'never[]' is missing the following properties from type 'Ref ': value,
      加个value就可以了基本在vue3里面类似的错都可以用这种方法......
  • How to ensure all the deposits to exchange accounts are reflected properly?
    Accounts,includingexchangeaccounts,canreceivefundsintwoways:an“external”,or“top-level”transfer(eg.iff1XXXsendsamessagetof1ZZZthattransfers1FIL),and“internal”transfers”thatresultfromasubinvocation.Anexampleof“inter......
  • 总结:C++引用(Reference)
    声明:资料整理自网络资源,未能全部注明引用来源,如有侵权请联系。一、基本概念引用(Reference)是C++相对于C语言的又一个扩充。引用变量是一个别名,即某个已存在变量的另一个名字。声明方法:类型标识符&引用名=目标变量名;inta;//定义变量aint&b=a;//定义引用b,a和b表......
  • mysql中删除时报错Cannot truncate a table referenced in a foreign key constraint
    在Mysql使用Truncate截断表时,提示Cannottruncateatablereferencedinaforeignkeyconstraint(monitoritem,CONSTRAINTmonitortaskpollutant_monitortask_fk)。这是因为存在外键约束导致的无法删除,我们可以先关闭外键约束,删除后再启动外键约束。1、检查外键约束SELE......