alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.79k stars 495 forks source link

[BUG] fastJson2.x序列化,SerializeConfig.put(Integer.class, ToStringSerializer.instance)未生效 #3157

Open gonghuan93cn opened 1 week ago

gonghuan93cn commented 1 week ago

问题描述

使用 SerializeConfig.put(Integer.class, ToStringSerializer.instance)进行序列化,将integer类型转成string类型,未生效;

环境信息

重现步骤

public class App { public static class People { private Integer age;

    private Long hairNums;

    private String name;

    public People(Integer age, String name, Long hairNums) {
        this.age = age;
        this.name = name;
        this.hairNums = hairNums;
    }

    public Integer getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public Long getHairNums() {
        return hairNums;
    }
}

public static void main( String[] args )
{
    SerializeConfig stringSerializeConfig = new SerializeConfig();
    stringSerializeConfig.put(Integer.class, ToStringSerializer.instance);
    stringSerializeConfig.put(Long.class, ToStringSerializer.instance);

    People one = new People(10, "aaa", 10000000L);
    System.out.println(JSON.toJSONString(one, stringSerializeConfig, new SerializerFeature[0]));
}

}

期待的正确结果

{"age":"10","hairNums":"10000000","name":"aaa"}

相关日志输出

{"age":10,"hairNums":"10000000","name":"aaa"}