package com.panzhihua.common.model.helper;
|
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.util.Objects;
|
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.databind.BeanProperty;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
|
/**
|
* BigDecimal序列化(默认保留二位小数和四舍五入)
|
*
|
* @author cedoo
|
* @date 2021-4-19 01:24:28
|
*/
|
public class SerializerBigDecimal extends JsonSerializer<BigDecimal> implements ContextualSerializer {
|
|
protected DecimalFormat decimalFormat;
|
|
public SerializerBigDecimal() {}
|
|
public SerializerBigDecimal(DecimalFormat decimalFormat) {
|
this.decimalFormat = decimalFormat;
|
}
|
|
@Override
|
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
if (Objects.isNull(value)) {
|
gen.writeNull();
|
} else {
|
if (null != decimalFormat) {
|
gen.writeNumber(decimalFormat.format(value));
|
} else {
|
gen.writeNumber(value.setScale(2, BigDecimal.ROUND_HALF_UP));
|
}
|
}
|
}
|
|
@Override
|
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
|
throws JsonMappingException {
|
|
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
|
if (format == null) {
|
return this;
|
}
|
|
if (format.hasPattern()) {
|
DecimalFormat decimalFormat = new DecimalFormat(format.getPattern());
|
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
|
return new SerializerBigDecimal(decimalFormat);
|
}
|
|
return this;
|
}
|
|
protected JsonFormat.Value findFormatOverrides(SerializerProvider provider, BeanProperty prop,
|
Class<?> typeForDefaults) {
|
if (prop != null) {
|
return prop.findPropertyFormat(provider.getConfig(), typeForDefaults);
|
}
|
return provider.getDefaultPropertyFormat(typeForDefaults);
|
}
|
}
|