All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.migoo.framework.jackson.databind.BigDecimalSerializer Maven / Gradle / Ivy

The newest version!
package xyz.migoo.framework.jackson.databind;

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.util.Optional;

import static java.math.RoundingMode.HALF_DOWN;
import static xyz.migoo.framework.common.enums.NumberConstants.N_2;

/**
 * @author xiaomi
 * Created on 2022/5/27 20:59
 */
public class BigDecimalSerializer extends JsonSerializer {
    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializer) throws IOException {
        gen.writeString(Optional.ofNullable(value).orElse(BigDecimal.ZERO).setScale(N_2, HALF_DOWN).toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy