From e37073905aa3e2afdd7f2aca96fb1c36810eafd0 Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Thu, 26 Dec 2024 07:50:40 +0800 Subject: [PATCH 1/3] fix remove the field writer which only differ in first character that one is upper case the other is lower case, for issue 3220 --- .../com/alibaba/fastjson2/writer/ObjectWriterCreator.java | 8 ++++++++ .../alibaba/fastjson2/writer/ObjectWriterCreatorASM.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java index dba7a8e0b..8ef458a4e 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java @@ -479,6 +479,14 @@ boolean record = BeanUtils.isRecord(objectClass); if (origin != null && origin.compareTo(fieldWriter) > 0) { fieldWriterMap.put(fieldName, fieldWriter); } + + // the sameFieldName means only differ in first character that one is upper case the other is lower case + if (origin == null) { + String sameFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + if (fieldWriterMap.containsKey(sameFieldName)) { + fieldWriterMap.remove(sameFieldName); + } + } }); fieldWriters = new ArrayList<>(fieldWriterMap.values()); diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java index 85ef8693c..cea3051cf 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java @@ -345,6 +345,14 @@ boolean record = BeanUtils.isRecord(objectClass); if (origin != null && origin.compareTo(fieldWriter) > 0) { fieldWriterMap.put(fieldName, fieldWriter); } + + // the sameFieldName means only differ in first character that one is upper case the other is lower case + if (origin == null) { + String sameFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + if (fieldWriterMap.containsKey(sameFieldName)) { + fieldWriterMap.remove(sameFieldName); + } + } }); } } else { From fdb4e06672a0ce27b5db09a9eaca9f7fb30ec745 Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Fri, 27 Dec 2024 07:18:08 +0800 Subject: [PATCH 2/3] fix build error --- .../alibaba/fastjson2/writer/ObjectWriterCreator.java | 10 ++++++++-- .../fastjson2/writer/ObjectWriterCreatorASM.java | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java index 8ef458a4e..36328296d 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java @@ -482,8 +482,14 @@ boolean record = BeanUtils.isRecord(objectClass); // the sameFieldName means only differ in first character that one is upper case the other is lower case if (origin == null) { - String sameFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); - if (fieldWriterMap.containsKey(sameFieldName)) { + String sameFieldName = null; + char firstChar = fieldName.charAt(0); + if (firstChar >= 'A' && firstChar <= 'Z') { + sameFieldName = (firstChar + 32) + fieldName.substring(1); + } else if (firstChar >= 'a' && firstChar <= 'z') { + sameFieldName = (firstChar - 32) + fieldName.substring(1); + } + if (sameFieldName != null && fieldWriterMap.containsKey(sameFieldName)) { fieldWriterMap.remove(sameFieldName); } } diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java index cea3051cf..92d22a4d1 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java @@ -348,8 +348,14 @@ boolean record = BeanUtils.isRecord(objectClass); // the sameFieldName means only differ in first character that one is upper case the other is lower case if (origin == null) { - String sameFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); - if (fieldWriterMap.containsKey(sameFieldName)) { + String sameFieldName = null; + char firstChar = fieldName.charAt(0); + if (firstChar >= 'A' && firstChar <= 'Z') { + sameFieldName = (char) (firstChar + 32) + fieldName.substring(1); + } else if (firstChar >= 'a' && firstChar <= 'z') { + sameFieldName = (char) (firstChar - 32) + fieldName.substring(1); + } + if (sameFieldName != null && fieldWriterMap.containsKey(sameFieldName)) { fieldWriterMap.remove(sameFieldName); } } From 1e5b940fc06b1dee8b2c30b2203d5c3ad0eceece Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Fri, 27 Dec 2024 23:06:52 +0800 Subject: [PATCH 3/3] add test --- .../fastjson2/writer/ObjectWriterCreator.java | 4 +-- .../fastjson2/issues_3200/Issue3220.java | 27 +++++++++++++++++++ pom.xml | 3 ++- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java index 36328296d..713f753f7 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java @@ -485,9 +485,9 @@ boolean record = BeanUtils.isRecord(objectClass); String sameFieldName = null; char firstChar = fieldName.charAt(0); if (firstChar >= 'A' && firstChar <= 'Z') { - sameFieldName = (firstChar + 32) + fieldName.substring(1); + sameFieldName = (char) (firstChar + 32) + fieldName.substring(1); } else if (firstChar >= 'a' && firstChar <= 'z') { - sameFieldName = (firstChar - 32) + fieldName.substring(1); + sameFieldName = (char) (firstChar - 32) + fieldName.substring(1); } if (sameFieldName != null && fieldWriterMap.containsKey(sameFieldName)) { fieldWriterMap.remove(sameFieldName); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java new file mode 100644 index 000000000..c88b9b2e4 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java @@ -0,0 +1,27 @@ +package com.alibaba.fastjson2.issues_3200; + +import com.alibaba.fastjson2.JSON; +import lombok.Data; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue3220 { + @Test + public void test() { + Param param = new Param(); + param.setUserName("张三"); + param.setCode("001"); + param.setSort("1"); + param.setStatus("正常"); + assertEquals("{\"code\":\"001\",\"sort\":\"1\",\"status\":\"正常\",\"userName\":\"张三\"}", JSON.toJSONString(param)); + } + + @Data + private static class Param { + public String UserName; + String Code; + String Sort; + String Status; + } +} diff --git a/pom.xml b/pom.xml index 01a9b2030..d65b6c103 100644 --- a/pom.xml +++ b/pom.xml @@ -663,7 +663,8 @@ com/alibaba/fastjson2/issues_1000/Issue1395.java, com/alibaba/fastjson2/issues_2100/Issue2164.java, com/alibaba/fastjson2/issues_2100/Issue2181.java, - com/alibaba/fastjson/v2issues/Issue1432.java + com/alibaba/fastjson/v2issues/Issue1432.java, + com/alibaba/fastjson2/issues_3200/Issue3220.java