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