Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

fastjson有没有类似于jackson的类级别的JsonNaming注解,实现下划线驼峰(json property)和大小写驼峰(java jean property)转换? #1555

Closed
lovepoem opened this issue Oct 31, 2017 · 6 comments
Milestone

Comments

@lovepoem
Copy link
Contributor

lovepoem commented Oct 31, 2017

fastjson有没有类似于jackson的JsonNaming注解?
jackson用法:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Test{
    private String studentName;
}

可以在类级别实现下划线驼峰(json property)和大小写驼峰转换(java jean property)?
eg: java property:studentName <---> json property: student_name
PropertyNamingStrategy.SnakeCase目前看只有全局config可以设定,并没有类级别的注解。

@lovepoem lovepoem changed the title fastjson有没有类似于jackson的JsonNaming注解? fastjson有没有类似于jackson的JsonNaming注解,实现下划线驼峰(json property)和大小写驼峰(java jean property)转换? Oct 31, 2017
@lovepoem lovepoem changed the title fastjson有没有类似于jackson的JsonNaming注解,实现下划线驼峰(json property)和大小写驼峰(java jean property)转换? fastjson有没有类似于jackson的类级别的JsonNaming注解,实现下划线驼峰(json property)和大小写驼峰(java jean property)转换? Oct 31, 2017
@wenshao
Copy link
Member

wenshao commented Oct 31, 2017

@lovepoem
Copy link
Contributor Author

lovepoem commented Oct 31, 2017

目前看不满足,我想要一个注解,实现字段名转换的定制方便。

类名设置了转换策略:
1、如果属性名定制了要转换的名字,按照属性名为准
2、其他未定制名字的属性,将按照类上的注解转换策略为准。
贴一下jackson的使用方式测试代码:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Student student = new Student();
        student.setStudentName("hahah");
        student.setStudentAddress("123");
        String json = mapper.writeValueAsString(student);
        System.out.println(json);
    }

    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
    static class Student {
        private String studentName;
        @JsonProperty("studentAddress")
        private String studentAddress;

        public String getStudentName() {
            return studentName;
        }

        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }

        public String getStudentAddress() {
            return studentAddress;
        }

        public void setStudentAddress(String studentAddress) {
            this.studentAddress = studentAddress;
        }
    }
}

执行结果为:

{"student_name":"hahah","studentAddress":"123"}

反之也成立

@wenshao
Copy link
Member

wenshao commented Nov 3, 2017

明白你的意思了,谢谢你这么好的建议,将会在周末发布新版本支持

@wenshao wenshao added this to the 1.2.40 milestone Nov 3, 2017
@lovepoem
Copy link
Contributor Author

lovepoem commented Nov 3, 2017

太棒啦

@wenshao
Copy link
Member

wenshao commented Nov 4, 2017

@wenshao
Copy link
Member

wenshao commented Nov 4, 2017

https://github.com/alibaba/fastjson/releases/tag/1.2.40
问题已经修复,请用新版本

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants