package cn.com.ty.lift.ud.common;

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

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

/**
 * 序列化格式化
 * 
 * @author SKing
 * 
 *         属性上面加：@JsonSerialize(using = MySerializer.class)
 *
 */
public class MySerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value != null) {
            /** 在这里定制我们需要格式化的逻辑 */
            // 留六位小数，四舍五入
            BigDecimal number = value.setScale(2, RoundingMode.HALF_UP);
            gen.writeNumber(number);
        } else {
            gen.writeNumber(value);
        }
    }

}
