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

group.rxcloud.capa.addons.serialzer.ssjson.BigDecimalDeserializerWrapper Maven / Gradle / Ivy

package group.rxcloud.capa.addons.serialzer.ssjson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers;
import com.google.common.annotations.VisibleForTesting;
import group.rxcloud.capa.addons.serialzer.validate.BigDecimalValidator;
import group.rxcloud.capa.addons.serialzer.validate.TypeValidateResult;
import group.rxcloud.capa.addons.serialzer.validate.ValidateValueException;

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

public class BigDecimalDeserializerWrapper extends NumberDeserializers.BigDecimalDeserializer {
    private int bigNumberMaxLength;
    private int bigDecimalMaxSignificandLength;
    private int bigDecimalMaxExponentLength;
    private BigDecimalValidator bigDecimalValidator = BigDecimalValidator.INSTANCE;
    private NumberDeserializers.BigDecimalDeserializer delegate;

    public BigDecimalDeserializerWrapper(int bigNumberMaxLength, int bigDecimalMaxSignificandLength, int bigDecimalMaxExponentLength) {
        this.bigNumberMaxLength = bigNumberMaxLength;
        this.bigDecimalMaxSignificandLength = bigDecimalMaxSignificandLength;
        this.bigDecimalMaxExponentLength = bigDecimalMaxExponentLength;
        this.delegate = new NumberDeserializers.BigDecimalDeserializer();
    }

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        TypeValidateResult result = validateRawValue(p.getText());
        if (!result.isPass()) {
            if (!result.isPass()) {
                throw new ValidateValueException("validate BigDecimal failed:" + result.getErrorMessages());
            }
        }
        return delegate.deserialize(p, ctxt);
    }

    @VisibleForTesting
    TypeValidateResult validateRawValue(String rawValue)  {
        return bigDecimalValidator.validateRawValue(rawValue, bigNumberMaxLength, bigDecimalMaxSignificandLength, bigDecimalMaxExponentLength);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy