Case insensitive JSON to POJO mapping without changing the POJO

后端 未结 6 852
野性不改
野性不改 2020-11-27 21:06

Does anyone know how com.fasterxml.jackson.databind.ObjectMapper is able to map JSON properties to POJO properties case insensitive?

JSON-String:

相关标签:
6条回答
  • 2020-11-27 21:11

    An alternative solution is to specify JSON field names case-sensitive:

    public Movie(){
        //Jackson deserialize
    }
    @JsonProperty("Title")
    public String getTitle() {
        return title;
    }
    
    @JsonProperty("Year")
    public String getYear() {
        return year;
    }
    @JsonProperty("imdbID")
    public String getImdbID() {
        return imdbID;
    }
    
    0 讨论(0)
  • 2020-11-27 21:14
    package br.com.marcusvoltolim.util;
    
    
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.MapperFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.extern.log4j.Log4j;
    
    @Log4j
    public class JsonUtils {
    
        private static final ObjectMapper OBJECT_MAPPER;
    
        static {
            OBJECT_MAPPER = new ObjectMapper();
            OBJECT_MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
    
        public static <T> T fromJson(final String json, final Class<T> classe) {
            try {
                return OBJECT_MAPPER.readValue(json, classe);
            } catch (Exception e) {
                log.error(e);
                try {
                    return classe.newInstance();
                } catch (InstantiationException | IllegalAccessException ex) {
                    return null;
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 21:20

    This behaviour was introduced in Jackson 2.5.0. You can configure the mapper to be case insensitive using MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES.

    For example :

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    
    0 讨论(0)
  • I had the same problem and couldn't find a global way of solving this. However you can have 2 setters per property to achieve this:

    @JsonSetter("FIRSTNAME")
    public void setFirstNameCaps(String firstName) {
        this.firstName = firstName;
    }
    
    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    

    Not elegant but will work for both upper and lower case json fields. You can also try the solution mentioned here but this might have a performance overhead

    0 讨论(0)
  • 2020-11-27 21:26

    You can solve this problem by configuring the mapper, as described by the @Nicolas Riousset.

    In addition, since version Jackson 2.9 you can do the same using annotation @JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) over a field or class, which is a more flexible option.

    @JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
    private String firstName;
    
    0 讨论(0)
  • 2020-11-27 21:26

    I was in the same kind of situation and had to convert to a map and then copy the values over manually.

    import com.fasterxml.jackson.core.type.TypeReference;
    
    Map<String, String> map = 
        mapper.readValue(jsonAsString, new TypeReference<Map<String, String>>(){});
    
    0 讨论(0)
提交回复
热议问题