Java CoercionConfigs-class And Method Code Example

Here is an example of using the CoercionConfigs class from the com.fasterxml.jackson.databind package in the Jackson library for Java:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionConfigs;

public class Example {
    public static void main(String[] args) {
        // create a new ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // get the CoercionConfigs of the mapper
        CoercionConfigs configs = mapper.getCoercionConfigs();

        // set the configuration to enable coercion of JSON numbers to int

        // example input JSON string
        String jsonInput = "{\"value\": 3.14}";

        try {
            // parse the JSON string and get the value as int
            JsonNode root = mapper.readTree(jsonInput);
            int value = root.get("value").intValue();
            System.out.println("Value: " + value);
        } catch (IOException e) {

This example uses the CoercionConfigs class to configure the ObjectMapper to coerce JSON numbers to int. It then parses a JSON string and uses the intValue() method to get the value as int.

Note: The CoercionConfigs class is only available from Jackson 2.12 and later, if you are using an earlier version of Jackson it will not be available.