fastjson序列化的结果出现$ref/$.object

案例:{"xiaoMing":{"courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"},"studentNo":"0001"},"xiaoHua":{"courses":{"$ref":"$.xiaoMing.courses"},"studentNo":"0002"}}

原因:一个对象被重复引用,fastjson默认开启循环引用检查,所以序列化结果会出现($ref. $.),来标注被重复引用的对象。

package cn.com.dl.bean;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by yanshao on 2021-01-13.
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    private static final long serialVersionUID = 465467703322405014L;
    private String studentNo;
    private Map<String,String> courses;
}

异常案例:

直接com.alibaba.fastjson.JSON#toJSONString(java.lang.Object)来序列化

    @Test
    public void test(){
        Map<String,Student> map = new HashMap<>();
        Map<String,String> courses = new HashMap<>();
        courses.put("English","English");
        courses.put("Chinese","English");
        courses.put("Mathematics","Mathematics");
        Student xiaoMing = Student.builder()
                .studentNo("0001")
                .courses(courses)
                .build();
        map.put("xiaoMing",xiaoMing);
        Student xiaoHua = Student.builder()
                .studentNo("0002")
                .courses(courses)
                .build();
        map.put("xiaoHua",xiaoHua);
        System.out.println(JSONObject.toJSONString(map));
    }

序列化结果:

{"xiaoMing":{"courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"},"studentNo":"0001"},"xiaoHua":{"courses":{"$ref":"$.xiaoMing.courses"},"studentNo":"0002"}}

解决方案1:关闭fastjson的循环引用检查

    @Test
    public void test(){
        Map<String,Student> map = new HashMap<>();
        Map<String,String> courses = new HashMap<>();
        courses.put("English","English");
        courses.put("Chinese","English");
        courses.put("Mathematics","Mathematics");
        Student xiaoMing = Student.builder()
                .studentNo("0001")
                .courses(courses)
                .build();
        map.put("xiaoMing",xiaoMing);
        Student xiaoHua = Student.builder()
                .studentNo("0002")
                .courses(courses)
                .build();
        map.put("xiaoHua",xiaoHua);
        System.out.println( JSONObject.toJSONString(map,SerializerFeature.DisableCircularReferenceDetect));
    }

序列化结果:

{"xiaoMing":{"courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"},"studentNo":"0001"},"xiaoHua":{"courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"},"studentNo":"0002"}}

解决方案2:使用com.google.gson.Gson

    @Test
    public void test(){
        Map<String,Student> map = new HashMap<>();
        Map<String,String> courses = new HashMap<>();
        courses.put("English","English");
        courses.put("Chinese","English");
        courses.put("Mathematics","Mathematics");
        Student xiaoMing = Student.builder()
                .studentNo("0001")
                .courses(courses)
                .build();
        map.put("xiaoMing",xiaoMing);
        Student xiaoHua = Student.builder()
                .studentNo("0002")
                .courses(courses)
                .build();
        map.put("xiaoHua",xiaoHua);
        Gson gson = new Gson();
        System.out.println(gson.toJson(map));
//        System.out.println("fastjson" + JSONObject.toJSONString(map,SerializerFeature.DisableCircularReferenceDetect));
    }

序列化结果:

{"xiaoMing":{"studentNo":"0001","courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"}},"xiaoHua":{"studentNo":"0002","courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"}}}

解决方案3:深拷贝或者新创建对象

    @Test
    public void test(){
        Map<String,Student> map = new HashMap<>();
        Map<String,String> courses = new HashMap<>();
        courses.put("English","English");
        courses.put("Chinese","English");
        courses.put("Mathematics","Mathematics");
        Student xiaoMing = Student.builder()
                .studentNo("0001")
                .courses(courses)
                .build();
        map.put("xiaoMing",xiaoMing);
        Student xiaoHua = Student.builder()
                .studentNo("0002")
                .courses(new HashMap<>(courses))
                .build();
        map.put("xiaoHua",xiaoHua);
        System.out.println(JSONObject.toJSONString(map));
//        Gson gson = new Gson();
//        System.out.println(gson.toJson(map));
//        System.out.println("fastjson" + JSONObject.toJSONString(map,SerializerFeature.DisableCircularReferenceDetect));
    }

注意:  对于循环引用,fastjson和gson处理方式不同,fastjson默认开启循环引用检查,而使用gson序列化时,会堆内存溢出;

fastjson:

fastjson{"map,":{"$ref":"@"},"xiaoMing":{"courses":{"English":"English","Chinese":"English","Mathematics":"Mathematics"},"studentNo":"0001"},"xiaoHua":{"courses":{"$ref":"$.xiaoMing.courses"},"studentNo":"0002"}}

gson:

java.lang.StackOverflowError
	at java.lang.StringBuffer.append(StringBuffer.java:270)
	at java.io.StringWriter.write(StringWriter.java:112)
	at com.google.gson.stream.JsonWriter.string(JsonWriter.java:590)

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
这个错误通常是由于 Maven 无法从 Maven 中央仓库(central)下载所需的依赖项引起的。该错误消息指出,Maven 在之前的尝试中无法从 https://repo.maven.apache.org/maven2 下载 com.alibaba:fastjson:jar:1.2.83 这个依赖项,并且该错误已被缓存到本地仓库中。根据错误消息中提到的内容,可能有几个原因导致这个问题: 1. 网络连接问题:首先,请确保你的网络连接正常,并且你可以访问 https://repo.maven.apache.org/maven2。有时网络连接不稳定或防火墙设置可能会导致下载失败。 2. 本地仓库缓存问题:Maven 会将下载失败的依赖项缓存在本地仓库中,以避免重复下载。但如果缓存的依赖项出现问题,你可能需要清理本地仓库并重新尝试下载。你可以尝试删除 Maven 本地仓库中与 com.alibaba:fastjson 相关的文件,并重新构建你的项目。 3. Maven 中央仓库问题:偶尔,Maven 中央仓库可能会遇到问题或暂时不可用。你可以尝试等待一段时间后再次构建项目,或者尝试更改 Maven 配置以使用其他镜像仓库。 为了解决这个问题,你可以尝试以下几个步骤: 1. 检查网络连接,并确保你可以访问 https://repo.maven.apache.org/maven2。 2. 清除本地 Maven 仓库中与 com.alibaba:fastjson 相关的缓存文件。你可以在 Maven 设置中找到本地仓库的位置,并删除相应的文件。 3. 在 Maven 配置文件(pom.xml)中,将 Maven 中央仓库的 URL 更改为其他可用的镜像仓库。你可以在 Maven 官方网站找到可用的镜像仓库列表,并将其添加到 pom.xml 文件中。 4. 执行 Maven 构建命令时,添加 -U 参数以强制更新依赖项。 如果上述步骤都无法解决问题,那可能是由于 Maven 中央仓库本身的问题。此时,你可以尝试等待一段时间,直到问题得到修复,或者尝试使用其他版本的 fastjson 依赖项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕少༒江湖

给我一份鼓励!谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值