From df5ac5c4c6caa1f6258f6ebdb98a0bafbadeefd1 Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Thu, 19 Sep 2024 20:23:00 +0800 Subject: [PATCH 1/2] fix remove duplicated list elements when size are same, for issue #2944 --- .../FieldReaderCollectionMethodReadOnly.java | 5 ++ .../fastjson2/issues_2900/Issue2944.java | 78 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderCollectionMethodReadOnly.java b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderCollectionMethodReadOnly.java index 755a586974..140f75faa1 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderCollectionMethodReadOnly.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderCollectionMethodReadOnly.java @@ -75,6 +75,11 @@ public void accept(T object, Object value) { } Collection values = (Collection) value; + + if (values.size() == collection.size()) { + return; + } + for (Object item : values) { if (item == null) { collection.add(null); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java new file mode 100644 index 0000000000..6e01af6b78 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java @@ -0,0 +1,78 @@ +package com.alibaba.fastjson2.issues_2900; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2944 { + @Test + public void test() { + List employees = new ArrayList<>(); + employees.add(new Employee(0, "John")); + employees.add(new Employee(1, "Jane")); + employees.add(new Employee(2, "Bob")); + Department department = new Department("dev", employees); + + String payload = JSON.toJSONString(department); + JSONObject jsonObject = JSON.parseObject(payload); + Department parsed = jsonObject.toJavaObject(Department.class); + assertEquals(payload, JSON.toJSONString(parsed)); + } +} + +class Department { + private final String name; + private final List employees; + + public String getName() { + return name; + } + + public List getEmployees() { + return employees; + } + + public Department(String name, List employees) { + this.name = name; + this.employees = employees; + } + + @Override + public String toString() { + return "Department{" + + "name='" + name + '\'' + + ", employees=" + employees + + '}'; + } +} + +class Employee { + private final Integer id; + private final String name; + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public Employee(int id, String name) { + this.id = id; + this.name = name; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} From 1fa221bda1a220f686a77520479d0929b7454753 Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Thu, 19 Sep 2024 20:25:27 +0800 Subject: [PATCH 2/2] fix checkstyle --- .../fastjson2/issues_2900/Issue2944.java | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java index 6e01af6b78..d0cb3ed73b 100644 --- a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2944.java @@ -23,56 +23,56 @@ public void test() { Department parsed = jsonObject.toJavaObject(Department.class); assertEquals(payload, JSON.toJSONString(parsed)); } -} -class Department { - private final String name; - private final List employees; + class Department { + private final String name; + private final List employees; - public String getName() { - return name; - } + public String getName() { + return name; + } - public List getEmployees() { - return employees; - } + public List getEmployees() { + return employees; + } - public Department(String name, List employees) { - this.name = name; - this.employees = employees; - } + public Department(String name, List employees) { + this.name = name; + this.employees = employees; + } - @Override - public String toString() { - return "Department{" + - "name='" + name + '\'' + - ", employees=" + employees + - '}'; + @Override + public String toString() { + return "Department{" + + "name='" + name + '\'' + + ", employees=" + employees + + '}'; + } } -} -class Employee { - private final Integer id; - private final String name; + class Employee { + private final Integer id; + private final String name; - public Integer getId() { - return id; - } + public Integer getId() { + return id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public Employee(int id, String name) { - this.id = id; - this.name = name; - } + public Employee(int id, String name) { + this.id = id; + this.name = name; + } - @Override - public String toString() { - return "Employee{" + - "id=" + id + - ", name='" + name + '\'' + - '}'; + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } } }