Json字段自定义序列化和反序列化
前言
Github:https://github.com/HealerJean
1、序列化
public class DecimalSerialize extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String value = null;
if(bigDecimal != null) {
BigDecimal b = bigDecimal.setScale(0);
value = b.toString();
}
jsonGenerator.writeString(value);
}
}
2、反序列化
public class DecimalSerialize extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String value = null;
if(bigDecimal != null) {
BigDecimal b = bigDecimal.setScale(0);
value = b.toString();
}
jsonGenerator.writeString(value);
}
}
3、使用
@JsonSerialize(using = UserBelongSerialize.class)
@JsonDeserialize(using = DecimalSerialize.class)