Skip to content

[QUESTION] fastjson升级到2后,方法签名变化问题 #446

@FutureZM

Description

@FutureZM

JSON.toJSONString方法签名变化问题

在fastjson1.x中可以通过 SerializeConfig,NameFilter,SerializerFeature 实现对序列化后的数据进行定制,如下列通过SerializeConfig配置驼峰体转下划线, 然后通过NameFilter对 序列化后的key进行修改,通过SerializerFeature指定某些缺省值的处理方式,请问在fastjson2中该如何处理呢?

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class Student{
        private String nameV1;
        private Integer ageB1;
        private String emptyC1;
    }
    
    // 样例代码
    Student test = new Student("z", 12, null);
    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
    final NameFilter nameFilter = (o12, s, o1) -> {
        String res = s.substring(0, 1).toUpperCase();
        if (s.length() > 1) {
            res += s.substring(1);
        }
        return res;
    };
    String res = JSON.toJSONString(test, config, nameFilter, SerializerFeature.WriteNullStringAsEmpty);
    System.out.println(res);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions