Jackson自定义序列化与反序列化注解详解
前言
某些场景下,我们使用Jackson对数据进行序列化或反序列化的时候,需要对某些数据进行特殊处理,比如,不同的场景下,对数字的精度要求不同,此时如果仅仅使用原始的@JsonDeserialize和@JsonSerialize注解,不能满足我们的需求,如果每种精度都写一个独立的处理类,无疑增加了代码的复杂度。
我们可以看到@JsonDeserialize和@JsonSerialize都支持注解标注
于是可以自己定义一个注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JsonDeserialize(using = DecimalFormatJsonDeserializer.class)
public @interface DecimalFormat{
int scale() default 2;
}
仅仅加上@JsonDeserialize注解的话,会发现Jackson在反序列时,并不会走到我们定义的DecimalFormatJsonDeserializer类中,还需要加上一个Jackson的注解继承注解@JacksonAnnotationsInside
自定义反序列化注解及实现
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonDeserialize(using = DecimalFormatJsonDeserializer.class)
public @interface DecimalFormat{
int scale() default 2;
}
编写反序列化实现
public static class DecimalFormatJsonDeserializer extends JsonDeserializer<Double> {
@Override
public Double deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
// 获取当前反序列化的对象类型
Class<?> clazz = jsonParser.getCurrentValue().getClass();
BigDecimal decimalValue = jsonParser.getDecimalValue();
Field declaredField;
try {
// 通过字段名称获取对应字段
// 需考虑继承情况
declaredField = clazz.getDeclaredField(jsonParser.getCurrentName());
} catch (NoSuchFieldException e) {
return decimalValue.doubleValue();
}
// 获取注解
DecimalFormat annotation = declaredField.getAnnotation(DecimalFormat.class);
if (annotation == null) {
return decimalValue.doubleValue();
}
int scale = annotation.scale();
if (decimalValue != null) {
return decimalValue.setScale(scale, RoundingMode.HALF_UP).doubleValue();
}
return null;
}
}
测试代码
@Data
static class TestVo {
@DecimalFormat(scale = 1)
private Double amount;
private String name;
}
public static void main(String args[]) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
TestVo testVo = objectMapper.readValue("{\"amount\":3.5576,\"name\":\"young\"}", TestVo.class);
System.out.println(testVo); // TestVo(amount=3.6, name=young)
}
自定义序列化注解及实现
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = DecimalFormatJsonSerializer.class)
public @interface DecimalSerializerFormat {
String pattern() default "0.0000";
}
编写序列化实现
public class DecimalFormatJsonSerializer extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
JsonStreamContext outputContext = jsonGenerator.getOutputContext();
// 获取当前class对象
Class<?> clazz = outputContext.getCurrentValue().getClass();
// 获取当前属性名
String fieldName = outputContext.getCurrentName();
// 获取当前属性的field
Field field = FieldUtils.getDeclaredField(clazz, fieldName, true);
// 获取当前属性的注解
DecimalSerializerFormat annotation = field.getAnnotation(DecimalSerializerFormat.class);
if (annotation == null) {
return;
}
String pattern = annotation.pattern();
DecimalFormat decimalFormat = new DecimalFormat(pattern);
jsonGenerator.writeString(decimalFormat.format(bigDecimal));
}
}
测试代码
public static void main(String[] args) throws JsonProcessingException {
Test test = new Test();
test.setDecimal(BigDecimal.ONE);
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(test);
System.out.println(s); // {"decimal":"1.0000"}
}
@Data
static class Test{
@DecimalSerializerFormat
private BigDecimal decimal;
}
到此这篇关于Jackson自定义序列化与反序列化注解详解的文章就介绍到这了,更多相关Jackson序列化与反序列化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringCloud Gateway的基本入门和注意点详解
这篇文章主要介绍了SpringCloud Gateway的基本入门和注意点,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10
Spring Security 登录时添加图形验证码实现实例
这篇文章主要为大家介绍了Spring Security 登录时添加图形验证码实现实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-11-11
Springboot上传文件的同时传递参数用对象接收的问题及解决方案
在Spring Boot中,若接口需同时接收文件和其他参数,应将文件用@RequestParam标注,其他参数封装对象并用@Validated校验,避免参数绑定冲突,本文给大家介绍Springboot上传文件的同时传递参数用对象接收,感兴趣的朋友跟随小编一起看看吧2025-09-09


最新评论