java - jackson Pascal Case 和@JsonProperty on getter

标签 java json jackson

我创建了一个映射器

new ObjectMApper()
        .setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE)
        .setSerializationInclusion(Include.NON_NULL)

并且序列化在字段上完美运行(没有 getter 和 setter)。字段 currentStatus 被序列化为 "currentStatus"(首字母大写)。但我还有一个 getter(没有字段和 setter),它必须是驼峰命名法。所以我这样做:

@JsonProperty("abcDef")
public String getZxy() {...

但它被序列化为 "AbcDef" 而不是 "abcDef"。看起来命名策略仍然会触发并更改首字母。我使用 jackson-databind 2.3.2;

如何将这个 getter 映射为首字母小写?


编辑: 丑陋的代码,但显示了问题。这个测试应该通过但它失败了

import static org.assertj.core.api.Assertions.assertThat;
import org.testng.annotations.Test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class JsonFailureTest {

    @Test
    public void should_serialize_first_letter_lowercase() throws Exception {

        String json = new ObjectMapper()
                            .setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE)
                            .writeValueAsString(

        new Object(){

            @JsonProperty("fooBar")
            public String whatever() {return "";}

        });

        assertThat(json).contains("fooBar");
    }
}

最佳答案

这是使用自定义“注释感知”策略的解决方法:

import com.fasterxml.jackson.annotation.JsonInclude;
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.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

public class Foo {
    public static void main(final String[] args) throws JsonProcessingException {
        final SomeObject someObject = new SomeObject();
        someObject.setZxy("foobar");

        final ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy() {
            @Override
            public String nameForGetterMethod(final MapperConfig<?> config, final AnnotatedMethod method, final String defaultName) {
                final JsonProperty annotation = method.getAnnotation(JsonProperty.class);
                if (annotation != null) {
                    return annotation.value();
                }
                return super.nameForGetterMethod(config, method, defaultName);
            }
        });
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        System.out.println(mapper.writeValueAsString(someObject));
    }

    private static class SomeObject {

        private String zxy;

        @JsonProperty("abcDef")
        public String getZxy() {
            return this.zxy;
        }

        public void setZxy(final String zxy) {
            this.zxy = zxy;
        }
    }
}

输出:

{"abcDef":"foobar"}

关于java - jackson Pascal Case 和@JsonProperty on getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22667964/

相关文章:

java - 如何强制释放软引用

java - 将 org.json.JSONArray 插入 jackson ObjectNode

python - python加载json时,如何将str转成unicode,才能打印汉字?

json - 无法初始化代理 - 无 session

java - 使用 DateTimeFormatterBuilder 解析非 UTC 日期

java - Jackson - 将接口(interface)反序列化为枚举

java - 在 @Configuration 类中的 Spring 中设置注释驱动的事务

java - 如何配置 Intellij toString() 模板以与 Eclipse 保持一致

java - 子类化 UISelectMany 以创建自定义 JSF 组件

php - 无法将 swift 应用程序连接到 MySQL