Search code examples
javajsonjacksonjackson-databind

How to instruct Jackson ObjectMapper to not convert number field value into a String property?


I have the following JSON sample:

{
    "channel": "VTEX",
    "data": "{}",
    "refId": 143433.344,
    "description": "teste",
    "tags": ["tag1", "tag2"]
}

That should map to the following class:

public class AddConfigInput {
    public String channel;
    public String data;
    public String refId;
    public String description;
    public String[] tags;

    public AddConfigInput() {
    }
}

Using a code like bellow:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
String json = STRING_CONTAINING_THE_PREVIOUS_INFORMED_JSON;
AddConfigInput obj = mapper.readValue(json, AddConfigInput.class);
System.out.println(mapper.writeValueAsString(obj));

That produces as output:

{"channel":"VTEX","data":"{}","refId":"143433.344","description":"teste","tags":["tag1","tag2"]}

Please note that the field refId is of type String and I want to avoid this kind of automatic conversion from Numbers to String properties. Instead I want to Jackson throws an error about the type mismatch. How can I do that?


Solution

  • It seems that mapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS); works for the reverse case, that is, parsing fails when deserializing String value to numeric field.

    Providing custom deserializer for the refId field seems to resolve this issue.

    public class AddConfigInput {
        public String channel;
        public String data;
    
        //@JsonDeserialize(using = ForceStringDeserializer.class)
        public String refId;
        public String description;
        public String[] tags;
    
        public AddConfigInput() {
        }
    }
    
    public class ForceStringDeserializer extends JsonDeserializer<String> {
    
        @Override
        public String deserialize(
                JsonParser jsonParser, DeserializationContext deserializationContext) 
                throws IOException 
        {
            if (jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) {
                deserializationContext.reportWrongTokenException(
                        String.class, JsonToken.VALUE_STRING, 
                        "Attempted to parse token %s to string",
                        jsonParser.getCurrentToken());
            }
            return jsonParser.getValueAsString();
        }
    }
    

    Update
    This custom deserializer may be registered within the ObjectMapper and override default behaviour:

    public class ForcedStringParserModule extends SimpleModule {
    
        private static final long serialVersionUID = 1L;
    
        public ForcedStringParserModule() {
            this.setDeserializerModifier(new BeanDeserializerModifier() {
    
                @Override
                public JsonDeserializer<?> modifyDeserializer(
                        DeserializationConfig config, BeanDescription beanDesc,
                        JsonDeserializer<?> deserializer) 
                {
                    if (String.class.isAssignableFrom(beanDesc.getBeanClass())) {
                        return new ForceStringDeserializer();
                    }
                    return deserializer;
                }
            });
        }
    }
    

    Then this module can be registered with ObjectMapper:

    mapper.registerModule(new ForcedStringParserModule ());
    

    After modifying slightly the input JSON (using boolean for data field which must be String), the following exception is thrown:

    Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: 
    Unexpected token (VALUE_FALSE), expected VALUE_STRING: 
    Attempted to parse token VALUE_FALSE to string
     at [Source: (String)"{
        "channel": "VTEX",
        "data": false,
        "refId": "143433.344",
        "description": "teste",
        "tags": ["tag1", "tag2"]
    }"; line: 3, column: 13]