| .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); |
| } |
| } |
复制
}
| |
| 
@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字符串,在反序列化时可以识别出来,不会反序列化失败,结果如下图, |
| |
| |
| 
@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。 |
| |
| |
| |
复制
| |
| |
| |
| |
| @Override |
| @Deprecated |
| public String findEnumValue(Enum<?> value) |
| { |
| |
| |
| |
| try { |
| |
| 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) { |
| |
| } catch (NoSuchFieldException e) { |
| |
| } |
| 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(); |
| |
| return str.isEmpty() ? null : str; |
| } |
复制
| |
| |
| |
| # |
| |
| |
| 另外,序列化和反序列化中会有些常见配置,比如常见的如下, |
| |
| |
| # |
| |
| |
| 在反序列化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\":\"男\"}"; |
复制