首页 前端知识 @JsonProperty注解(5)

@JsonProperty注解(5)

2025-03-02 13:03:22 前端知识 前端哥 110 993 我要收藏
            .zAxis("40.05")
            .build();
    String jsonStr = JSON.toJSONString(coordinate);
    log.info("serializes the specified object into its equivalent Json representation :" + jsonStr);
    ObjectMapper mapper = new ObjectMapper();
    try {
        String str = mapper.writeValueAsString(coordinate);
        log.info("serialize any Java value as a String : " + str);
        Object bean = mapper.readerFor(Coordinate.class).readValue(str);
        log.info("read or update instances of specified type : " + bean);
    } catch (JsonProcessingException e) {
        log.error("error message : " + e);
    }
}

}


![](https://www.qianduange.cn/upload/article/bdf5bc681b684ccab2a2e787ee9d3cfb.png)


注解一般都是通过反射拿到对映的成员变量然后再进行增强,@JsonProperty把成员变量序列化成另外一个名称,并且它在序列化和反序列化的过程中都是使用的实际名称。


## @JsonAlias


com.fasterxml.jackson.annotation中的@JsonProperty是可以在序列化和反序列化中使用,而@JsonAlias只在反序列化中起作用,指定Java属性可以接受的更多名称。文末链接也有JsonAlias的实例源码,下面就简单举一个例子,



@Slf4j
public class JsonAliasDemo {
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
private static class Coordinate {
@JsonAlias(value = “x_location”)
@JsonProperty(value = “x_axis”)
private String xAxis;
@JsonProperty(value = “y_axis”)
@JsonAlias(value = “y_location”)
private String yAxis;
@JsonProperty(value = “z_axis”)
@JsonAlias(value = “z_location”)
private String zAxis;
}

public static void main(String[] args) {
    String location = "{\"x_location\":\"113.58\",\"y_location\":\"37.86\",\"z_location\":\"40.05\"}";
    ObjectMapper mapper = new ObjectMapper();
    try {
        Object bean = mapper.readValue(location, Coordinate.class);
        log.info("read or update instances of specified type : " + bean);
    } catch (JsonProcessingException e) {
        log.error("error message : " + e);
    }
}

}


@JsonAlias里的别名的json字符串,在反序列化时可以识别出来,不会反序列化失败,结果如下图,


![](https://www.qianduange.cn/upload/article/782c3e4659a445bd8b559bac5f3e2692.png)



## @JsonProperty源码


JsonProperty的源码是一个注解类,注解类上的几个元注解就不解释了,可以参考文末链接7,该注解的主要作用就是在pojo属性上执行自定义处理器流程。



@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonProperty {
String USE_DEFAULT_NAME = “”;
int INDEX_UNKNOWN = -1;
String value() default “”;
boolean required() default false;
int index() default -1;
String defaultValue() default “”;
JsonProperty.Access access() default JsonProperty.Access.AUTO;
public static enum Access {
AUTO,
READ_ONLY,
WRITE_ONLY,
READ_WRITE;
private Access() {
}
}
}


那么下面就看一下处理器流程做了一些什么事,找到JacksonAnnotationIntrospector类,


先看在jackson 2.6版本之后找到添加了@JsonProperty注解的pojo属性做了什么事,注意这里是一个过期的旧方法,保留是为了兼容使用老版本jackson的方法(@Deprecated注解)。首先该方法通过反射拿到成员变量,然后再获取注解中的属性值(eg:@JsonProperty(value = "x\_axis")),如果找到则返回value值,否则就返回原成员变量的name。



/**
 * Since 2.6, we have supported use of {@link JsonProperty} for specifying
 * explicit serialized name
 */
@Override
@Deprecated // since 2.8
public String findEnumValue(Enum<?> value)
{
    // 11-Jun-2015, tatu: As per [databind#677], need to allow explicit naming.
    //   Unfortunately cannot quite use standard AnnotatedClass here (due to various
    //   reasons, including odd representation JVM uses); has to do for now
    try {
        // We know that values are actually static fields with matching name so:
        Field f = value.getClass().getField(value.name());
        if (f != null) {
            JsonProperty prop = f.getAnnotation(JsonProperty.class);
            if (prop != null) {
                String n = prop.value();
                if (n != null && !n.isEmpty()) {
                    return n;
                }
            }
        }
    } catch (SecurityException e) {
        // 17-Sep-2015, tatu: Anything we could/should do here?
    } catch (NoSuchFieldException e) {
        // 17-Sep-2015, tatu: should not really happen. But... can we do anything?
    }
    return value.name();
}

下面再看一下jackson2.7之后是怎么做的,首先该方法不是根据成员变量的name获取类的属性,而是直接遍历类中所有的属性,然后用哈希表expl存属性的name和注解中的value映射关系,然后一次性遍历一遍把所有的属性的真实值集合返回出来(如果没有配置@JsonProperty的value则是属性原值,如果配有@JsonProperty的value则返回value值),这么做的好处在于不用一次一次的解析真实属性值而是一起解析真实属性值。



@Override // since 2.7
public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
HashMap<String,String> expl = null;
for (Field f : ClassUtil.getDeclaredFields(enumType)) {
if (!f.isEnumConstant()) {
continue;
}
JsonProperty prop = f.getAnnotation(JsonProperty.class);
if (prop == null) {
continue;
}
String n = prop.value();
if (n.isEmpty()) {
continue;
}
if (expl == null) {
expl = new HashMap<String,String>();
}
expl.put(f.getName(), n);
}
// and then stitch them together if and as necessary
if (expl != null) {
for (int i = 0, end = enumValues.length; i < end; ++i) {
String defName = enumValues[i].name();
String explValue = expl.get(defName);
if (explValue != null) {
names[i] = explValue;
}
}
}
return names;
}


其他属性的解析也基本上如出一辙,代码如下,



@Override
public Boolean hasRequiredMarker(AnnotatedMember m)
{
    JsonProperty ann = _findAnnotation(m, JsonProperty.class);
    if (ann != null) {
        return ann.required();
    }
    return null;
}

@Override
public JsonProperty.Access findPropertyAccess(Annotated m) {
    JsonProperty ann = _findAnnotation(m, JsonProperty.class);
    if (ann != null) {
        return ann.access();
    }
    return null;
}

@Override
public Integer findPropertyIndex(Annotated ann) {
    JsonProperty prop = _findAnnotation(ann, JsonProperty.class);
    if (prop != null) {
      int ix = prop.index();
      if (ix != JsonProperty.INDEX_UNKNOWN) {
           return Integer.valueOf(ix);
      }
    }
    return null;
}

@Override
public String findPropertyDefaultValue(Annotated ann) {
    JsonProperty prop = _findAnnotation(ann, JsonProperty.class);
    if (prop == null) {
        return null;
    }
    String str = prop.defaultValue();
    // Since annotations do not allow nulls, need to assume empty means "none"
    return str.isEmpty() ? null : str;
}



## 序列化和反序列化配置


另外,序列化和反序列化中会有些常见配置,比如常见的如下,


### DeserializationFeature.FAIL\_ON\_UNKNOWN\_PROPERTIES


在反序列化json字符串成Java对象时,遇到未知属性是否抛出异常信息。



@Slf4j
public class DeserializationFeatureDemo {
/**
* 注意上面的实例对象必须要有无参构造函数,否则在反序列化时创建实例对象
* 会抛出异常com.fasterxml.jackson.databind.exc.InvalidDefinitionException
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
private static class Person {
private String name;
private Long age;
}

public static void main(String[] args) {
    String jsonStr = "{\"name\":\"张三\",\"age\":18,\"sex\":\"男\"}";
转载请注明出处或者链接地址:https://www.qianduange.cn//article/22259.html
标签
评论
发布的文章

如何修改电脑mac地址?

2025-03-03 13:03:33

C 数组:深入解析与应用

2025-03-03 13:03:28

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!