定制JSON序列化的利器示例

金额序列化:

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.rb.jrzl2.crm.infrastructure.constant.json.deserializer.BigDecimalDeserializer;
import com.rb.jrzl2.crm.infrastructure.constant.json.serializer.BigDecimalSerializer;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.math.RoundingMode;

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = BigDecimalSerializer.class)
@JsonDeserialize(using = BigDecimalDeserializer.class)
@Target(ElementType.FIELD)
public @interface BigDecimalFormat {
    /**
     * 功能描述:
     * 参考DecimalFormat
     */
    String value() default "";

    boolean showPlusAndMinus() default false;

    RoundingMode roundingMode() default RoundingMode.HALF_UP;
}

 

序列化:

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;
import com.rb.jrzl2.crm.infrastructure.constant.json.annotation.BigDecimalFormat;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;


public class BigDecimalSerializer extends JsonSerializer<BigDecimal> implements ContextualSerializer {
    private String pattern;
    private RoundingMode roundingMode = RoundingMode.HALF_UP;
    private boolean showPlusAndMinus;

    @Override
    public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        DecimalFormat decimalFormat = new DecimalFormat(pattern);
        decimalFormat.setRoundingMode(roundingMode);
        if (showPlusAndMinus) {
            decimalFormat.setNegativePrefix("-");
            decimalFormat.setPositivePrefix("+");
        }
        if (value == null) {
            jgen.writeString("");
        } else {
            jgen.writeString(decimalFormat.format(value));
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        if (property == null) {
            return prov.findNullValueSerializer(property);
        }
        BigDecimalFormat annotation = property.getAnnotation(BigDecimalFormat.class);
        if (annotation == null) {
            annotation = property.getContextAnnotation(BigDecimalFormat.class);
        }
        this.pattern = annotation.value();
        this.roundingMode = annotation.roundingMode();
        this.showPlusAndMinus = annotation.showPlusAndMinus();
        return this;
    }
}

 

反序列化:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.rb.jrzl2.crm.infrastructure.constant.json.annotation.BigDecimalFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;

@Slf4j
public class BigDecimalDeserializer extends JsonDeserializer<BigDecimal> implements ContextualDeserializer {
    private String pattern;
    private RoundingMode roundingMode = RoundingMode.HALF_UP;

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        DecimalFormat decimalFormat = new DecimalFormat(pattern);
        decimalFormat.setRoundingMode(roundingMode);
        decimalFormat.setParseBigDecimal(true);
        try {
            Number parse = decimalFormat.parse(p.getValueAsString());
            return new BigDecimal(parse.toString());
        } catch (ParseException e) {
            log.error("金额解析异常", e);
        }
        String valueAsString = p.getValueAsString();
        return new BigDecimal(valueAsString);
    }

    @Override
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
        if (property == null) {
            return this;
        }
        final BigDecimalFormat annotation = property.getAnnotation(BigDecimalFormat.class);
        if (annotation == null) {
            return this;
        }
        this.pattern = annotation.value();
        this.roundingMode = annotation.roundingMode();
        return this;
    }
}

 

posted @ 2024-09-02 18:16  图图小淘气_real  阅读(2)  评论(0编辑  收藏  举报