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

org.openl.rules.serialization.FloatSerializer Maven / Gradle / Ivy

There is a newer version: 5.27.9
Show newest version
package org.openl.rules.serialization;

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

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.NumberSerializers;

/**
 * Float type serializer for preventing output of the float point numbers in the scientific notation.
 *
 * @author Yury Molchan
 */
class FloatSerializer extends NumberSerializers.FloatSerializer {
    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        Float number = (Float) value;
        if (Float.isFinite(number)) {
            // Serialize Float without scientific notation
            gen.writeNumber(new BigDecimal(value.toString()).toPlainString());
        } else {
            gen.writeNumber(number);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy