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 d50929b94..dba7a8e0b 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java @@ -1312,6 +1312,13 @@ protected ObjectWriter getInitWriter(ObjectWriterProvider provider, Class fieldC return objectWriter; } } + } else if (fieldClass == int.class || fieldClass == Integer.class) { + if ((provider.userDefineMask & ObjectWriterProvider.TYPE_INT32_MASK) != 0) { + ObjectWriter objectWriter = provider.cache.get(Integer.class); + if (objectWriter != ObjectWriterImplInt32.INSTANCE) { + return objectWriter; + } + } } else if (fieldClass == long.class || fieldClass == Long.class) { if ((provider.userDefineMask & ObjectWriterProvider.TYPE_INT64_MASK) != 0) { ObjectWriter objectWriter = provider.cache.get(Long.class); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3157Test.java b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3157Test.java new file mode 100644 index 000000000..292772d0e --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3157Test.java @@ -0,0 +1,54 @@ +package com.alibaba.fastjson2.issues_3100; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializeConfig; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson.serializer.ToStringSerializer; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +/** + * Issue3157测试类 + * @author changyi.ccy + */ +public class Issue3157Test { + /** + * 测试: Issue3157 + */ + @Test + public void testIssue3157() { + SerializeConfig stringSerializeConfig = new SerializeConfig(); + stringSerializeConfig.put(Integer.class, ToStringSerializer.instance); + stringSerializeConfig.put(Long.class, ToStringSerializer.instance); + People people = new People("Test", 10, 10000000L); + String jsonText = "{\"age\":\"10\",\"hairNums\":\"10000000\",\"name\":\"Test\"}"; + Assert.assertEquals("JSON文本不一致", jsonText, JSON.toJSONString(people, stringSerializeConfig, new SerializerFeature[0])); + } + + /** + * People类 + */ + public static class People { + private String name; + private Integer age; + private Long hairNums; + + public People(String name, Integer age, Long hairNums) { + this.name = name; + this.age = age; + this.hairNums = hairNums; + } + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } + + public Long getHairNums() { + return hairNums; + } + } +}