Skip to content

Commit

Permalink
'尝试修复配置Integer的ToStringSerializer不生效问题'
Browse files Browse the repository at this point in the history
  • Loading branch information
cychen1981 committed Dec 10, 2024
1 parent 77d51b1 commit 9ed8a2d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 9ed8a2d

Please sign in to comment.