package org.jeecg.config.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

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

public class BigDecimalToStringSerializer extends StdSerializer<BigDecimal> {
    public final static BigDecimalToStringSerializer instance = new BigDecimalToStringSerializer();
    protected BigDecimalToStringSerializer() {
        super(BigDecimal.class);
    }

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(bigDecimal == null) {
            jsonGenerator.writeString("");
        } else {
            jsonGenerator.writeString(bigDecimal.toPlainString());
        }
    }
}
