package com.dgtly.wxportal.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

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

/**
 * @Author: csz
 * @Date: 2020/8/19 18:16
 */
public class JsonBigDecimalSerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        DecimalFormat decimalFormat = new DecimalFormat("#,###");
        if (bigDecimal == null) {
            jsonGenerator.writeString("0");
        } else {
            jsonGenerator.writeString(decimalFormat.format(bigDecimal));
        }
    }
}
