package cn.superdesk.uniorder.common.json.serializer;

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.math.RoundingMode;
import java.util.Objects;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/26 下午7:20
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/26 下午7:20
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class BigDecimalScale4ConvertSerializer extends JsonSerializer<BigDecimal> {

	@Override
	public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

		if (Objects.isNull(value)) {
//			gen.writeNull();
			gen.writeNumber(BigDecimal.ZERO);
		} else {
			// 这里取floor
			gen.writeNumber(value.setScale(4, RoundingMode.FLOOR));
		}
	}

}
