Java DatatypeFeatures-class And Method Code Example

Here is an example of using the DatatypeFeatures class from the com.fasterxml.jackson.databind package in Java to configure custom serialization and deserialization settings for specific data types:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.util.DatatypeHelper;

public class Example {

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        SimpleModule module = new SimpleModule();
        module.addSerializer(Integer.class, new ToStringSerializer());

        DatatypeFeatures datatypeFeatures = new DatatypeFeatures();
        datatypeFeatures.add(Integer.class, DatatypeHelper.Feature.ACCEPT_FLOAT_AS_INT);
        datatypeFeatures.add(Integer.class, DatatypeHelper.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        datatypeFeatures.add(Integer.class, DatatypeHelper.Feature.WRITE_NUMBERS_AS_STRINGS);
        datatypeFeatures.add(Integer.class, DatatypeHelper.Feature.USE_BIG_INTEGER_FOR_INTS);

        ExampleObject obj = new ExampleObject(1, "example");
        String json = mapper.writeValueAsString(obj);

        json = "{\"id\":1.5,\"name\":\"example\"}";
        ExampleObject deserializedObj = mapper.readValue(json, ExampleObject.class);

class ExampleObject {

    private Integer id;
    private String name;

    public ExampleObject(Integer id, String name) { = id; = name;

    public Integer getId() {
        return id;

    public String getName() {
        return name;

    public String toString() {
        return "ExampleObject{" +
                "id=" + id +
                ", name='" + name + ''' +

In this example, a new ObjectMapper is created, and the DatatypeFeatures class is used to configure custom serialization and deserialization settings for the Integer data type. These settings include accepting float values as integers during deserialization, writing BigDecimal values as plain numbers, writing numbers as strings, and using BigInteger for integers. Finally, an ExampleObject is serialized and deserialized, and the output is printed to the console.