jackson 1.9.x使用自定义注释进行反序列化

时间:2013-04-03 10:28:13

标签: java json jackson

我有一个注释来指示json属性名称

public @interface ApiField {

        /**json property name*/
        public String value() default "";

    }

这样的课程

public class User{
   @ApiField("last_name") 
   String lastName;
   public String getLastName(){...}

   public void setLastName(String lastName){...}
}

json字符串是

{"last_name":"bob"}
  

如何使用此注释来指示属性名称而不是   @JsonProperty?

1 个答案:

答案 0 :(得分:4)

您可以使用AnnotationIntrospector来实现此目标

ApiAnnotationIntrospector

import java.lang.annotation.Annotation;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.Versioned;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.KeyDeserializer;
import org.codehaus.jackson.map.annotate.JsonSerialize.Typing;
import org.codehaus.jackson.map.introspect.Annotated;
import org.codehaus.jackson.map.introspect.AnnotatedClass;
import org.codehaus.jackson.map.introspect.AnnotatedConstructor;
import org.codehaus.jackson.map.introspect.AnnotatedField;
import org.codehaus.jackson.map.introspect.AnnotatedMethod;
import org.codehaus.jackson.map.introspect.AnnotatedParameter;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.util.VersionUtil;

public class ApiAnnotationIntrospector extends AnnotationIntrospector implements
        Versioned {

    @Override
    public Version version() {
        return VersionUtil.versionFor(getClass());
    }

    @Override
    public boolean isHandled(Annotation ann) {
        Class<?> cls = ann.annotationType();
        if (ApiField.class == cls) {
            return true;
        }
        return false;
    }

    @Override
    public String findRootName(AnnotatedClass ac) {
        return null;
    }

    @Override
    public String[] findPropertiesToIgnore(AnnotatedClass ac) {
        return null;
    }

    @Override
    public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) {
        return null;
    }

    @Override
    public boolean isIgnorableMethod(AnnotatedMethod m) {
        return false;
    }

    @Override
    public boolean isIgnorableConstructor(AnnotatedConstructor c) {
        return false;
    }

    @Override
    public boolean isIgnorableField(AnnotatedField f) {
        return false;
    }

    @Override
    public Object findSerializer(Annotated am) {
        return null;
    }

    @Override
    public Class<?> findSerializationType(Annotated a) {
        return null;
    }

    @Override
    public Typing findSerializationTyping(Annotated a) {
        return null;
    }

    @Override
    public Class<?>[] findSerializationViews(Annotated a) {
        return null;
    }

    @Override
    public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
        return null;
    }

    @Override
    public Boolean findSerializationSortAlphabetically(AnnotatedClass ac) {
        return null;
    }

    @Override
    public String findGettablePropertyName(AnnotatedMethod am) {
        return null;
    }

    @Override
    public boolean hasAsValueAnnotation(AnnotatedMethod am) {
        return false;
    }

    @Override
    public String findEnumValue(Enum<?> value) {
        return null;
    }

    @Override
    public String findSerializablePropertyName(AnnotatedField af) {
        return getPropertyName(af);
    }

    @Override
    public Object findDeserializer(Annotated am) {
        return null;
    }

    @Override
    public Class<? extends KeyDeserializer> findKeyDeserializer(Annotated am) {
        return null;
    }

    @Override
    public Class<? extends JsonDeserializer<?>> findContentDeserializer(
            Annotated am) {
        return null;
    }

    @Override
    public Class<?> findDeserializationType(Annotated am, JavaType baseType,
            String propName) {
        return null;
    }

    @Override
    public Class<?> findDeserializationKeyType(Annotated am,
            JavaType baseKeyType, String propName) {
        return null;
    }

    @Override
    public Class<?> findDeserializationContentType(Annotated am,
            JavaType baseContentType, String propName) {
        return null;
    }

    @Override
    public String findSettablePropertyName(AnnotatedMethod am) {
        return null;
    }

    @Override
    public String findDeserializablePropertyName(AnnotatedField af) {
        return getPropertyName(af);
    }

    @Override
    public String findPropertyNameForParam(AnnotatedParameter param) {
        return null;
    }

    private String getPropertyName(AnnotatedField af) {
        ApiField annotation = af.getAnnotation(ApiField.class);
        if (annotation != null && annotation.value() != "") {
            return annotation.value();
        }
        return null;
    }

}

然后按如下所示更改ObjectMapper

ObjectMapper mapper = new ObjectMapper();

AnnotationIntrospector primary = new ApiAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,
        secondary);

mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
mapper.getSerializationConfig().setAnnotationIntrospector(pair);

我的测试

public static class Type {
    private String code;

    private String description;

    @ApiField("raw-value")
    private String value;

    public Type() {
    }

    public Type(String code, String description, String value) {
        super();
        this.code = code;
        this.description = description;
        this.value = value;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

和测试用例

Type t = new Type("c", "d", "e");
StringWriter sw = new StringWriter();
mapper.writeValue(sw, t);
System.out.println(sw.toString());

输出

{"code":"c","description":"d","raw-value":"e"}