FastJson重复引用 循环引用

重复引用

  • 定义
    一个对象对另外一个对象
    多个字段重复引用
    集合字段重复引用.
  public class JsonTest {

   /**
    * 学生
    */
   private static class Student {

       /**
        * 姓名
        */
       private String name;

       public Student(String name) {
           this.name = name;
       }

       /**
        * Getter method for property  name.
        *
        * @return property value of name
        */
       public String getName() {
           return name;
       }

       /**
        * Setter method for property  counterType.
        *
        * @param name value to be assigned  to property <tt>name</tt>
        */
       public void setName(String name) {
           this.name = name;
       }
   }

   private static class Teacher {

       /**
        * 姓名
        */
       private String        name;

       /**
        * 学生列表
        */
       private List<Student> studentList = Lists.newArrayList();

       /**
        * 新增学生
        * @param student
        */
       public void addStudent(Student student) {
           studentList.add(student);
       }

       /**
        * Getter method for property  name.
        *
        * @return property value of name
        */
       public String getName() {
           return name;
       }

       /**
        * Setter method for property  counterType.
        *
        * @param name value to be assigned  to property <tt>name</tt>
        */
       public void setName(String name) {
           this.name = name;
       }

       /**
        * Getter method for property  studentList.
        *
        * @return property value of studentList
        */
       public List<Student> getStudentList() {
           return studentList;
       }

       /**
        * Setter method for property  counterType.
        *
        * @param studentList value to be assigned  to property <tt>studentList</tt>
        */
       public void setStudentList(List<Student> studentList) {
           this.studentList = studentList;
       }
   }

   public static void main(String[] args) {

       Teacher teacher = new Teacher();

       Student student = new Student("张三");
       teacher.addStudent(student);
       teacher.addStudent(student);
       System.out.println(JSON.toJSONString(teacher));

   }
}

结果:
{"studentList":[{"name":"张三"},{"$ref":"$.studentList[0]"}]}

循环引用

  • 定义
    对象A引用对象B, 对象B引用对象A 出现你中有,我中有你现象.
private static class Item{

        /**
         * 商品Id
         */
        private String itemId;

        /**
         * 关联订单
         */
        private Order  order;

        /**
         * Getter method for property  itemId.
         *
         * @return property value of itemId
         */
        public String getItemId() {
            return itemId;
        }

        /**
         * Setter method for property  counterType.
         *
         * @param itemId value to be assigned  to property <tt>itemId</tt>
         */
        public void setItemId(String itemId) {
            this.itemId = itemId;
        }

        /**
         * Getter method for property  order.
         *
         * @return property value of order
         */
        public Order getOrder() {
            return order;
        }

        /**
         * Setter method for property  counterType.
         *
         * @param order value to be assigned  to property <tt>order</tt>
         */
        public void setOrder(Order order) {
            this.order = order;
        }
    }


    private static class Order{

        /**
         * 订单Id
         */
        private String orderId;

        /**
         * 关联商品
         */
        private Item   item;

        /**
         * Getter method for property  orderId.
         *
         * @return property value of orderId
         */
        public String getOrderId() {
            return orderId;
        }

        /**
         * Setter method for property  counterType.
         *
         * @param orderId value to be assigned  to property <tt>orderId</tt>
         */
        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        /**
         * Getter method for property  item.
         *
         * @return property value of item
         */
        public Item getItem() {
            return item;
        }

        /**
         * Setter method for property  counterType.
         *
         * @param item value to be assigned  to property <tt>item</tt>
         */
        public void setItem(Item item) {
            this.item = item;
        }
    }

        public static void main(String[] args) {

        Order order = new Order();

        Item item = new Item();
        item.setItemId("itemId");
        item.setOrder(order);

        order.setItem(item);
        order.setOrderId("orderId");

        System.out.println(JSON.toJSONString(item));
        System.out.println(JSON.toJSONString(order));


    }

结果:
{"itemId":"itemId","order":{"item":{"$ref":".."},"orderId":"orderId"}}
{"item":{"itemId":"itemId","order":{"$ref":".."}},"orderId":"orderId"}

解决方案

  • 避免循环引用这种情形出现.
  • 不对循环引用的字段进行序列化.
  • 编码时 使用新对象 或者 新集合 赋值,不要使用同一个对象.
  • 不要关闭FastJson 引用检测机制.如果为了避免在重复引用时显示$ref而关闭它,会有很大可能导致循环引用时发生StackOverflowError异常
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容