com.mogudiandian.util.json.jackson.BigDecimal2BitsSerializer Maven / Gradle / Ivy
package com.mogudiandian.util.json.jackson;
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;
/**
* BigDecimal使用jackson序列化保留两位小数
* @author Joshua Sun
* @since 1.0.0
*/
public class BigDecimal2BitsSerializer extends JsonSerializer {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
} else {
gen.writeNumber(value.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros());
}
}
}