Jackson renames primitive boolean field by removing 'is'

后端 未结 10 769
渐次进展
渐次进展 2020-11-27 12:07

This might be a duplicate. But I cannot find a solution to my Problem.

I have a class

public class MyResponse implements Serializable {

    private          


        
相关标签:
10条回答
  • 2020-11-27 12:37

    Building upon Utkarsh's answer..

    Getter names minus get/is is used as the JSON name.

    public class Example{
        private String radcliffe; 
    
        public getHarryPotter(){
            return radcliffe; 
        }
    }
    

    is stored as { "harryPotter" : "whateverYouGaveHere" }


    For Deserialization, Jackson checks against both the setter and the field name. For the Json String { "word1" : "example" }, both the below are valid.

    public class Example{
        private String word1; 
    
        public setword2( String pqr){
            this.word1 = pqr; 
        }
    }
    
    public class Example2{
        private String word2; 
    
        public setWord1(String pqr){
            this.word2 = pqr ; 
        }
    }
    

    A more interesting question is which order Jackson considers for deserialization. If i try to deserialize { "word1" : "myName" } with

    public class Example3{
        private String word1;
        private String word2; 
    
        public setWord1( String parameter){
            this.word2 = parameter ; 
        }
    }
    

    I did not test the above case, but it would be interesting to see the values of word1 & word2 ...

    Note: I used drastically different names to emphasize which fields are required to be same.

    0 讨论(0)
  • 2020-11-27 12:40

    You can change primitive boolean to java.lang.Boolean (+ use @JsonPropery)

    @JsonProperty("isA")
    private Boolean isA = false;
    
    public Boolean getA() {
        return this.isA;
    }
    
    public void setA(Boolean a) {
        this.isA = a;
    }
    

    Worked excellent for me.

    0 讨论(0)
  • 2020-11-27 12:41

    I recently ran into this issue and this is what I found. Jackson will inspect any class that you pass to it for getters and setters, and use those methods for serialization and deserialization. What follows "get", "is" and "set" in those methods will be used as the key for the JSON field ("isValid" for getIsValid and setIsValid).

    public class JacksonExample {   
    
        private boolean isValid = false;
    
        public boolean getIsValid() {
            return isValid;
        }
    
        public void setIsValid(boolean isValid) {
            this.isValid = isValid;
        }
    } 
    

    Similarly "isSuccess" will become "success", unless renamed to "isIsSuccess" or "getIsSuccess"

    Read more here: http://www.citrine.io/blog/2015/5/20/jackson-json-processor

    0 讨论(0)
  • 2020-11-27 12:42

    there is another method for this problem.

    just define a new sub-class extends PropertyNamingStrategy and pass it to ObjectMapper instance.

    here is a code snippet may be help more:

    mapper.setPropertyNamingStrategy(new PropertyNamingStrategy() {
            @Override
            public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
                String input = defaultName;
                if(method.getName().startsWith("is")){
                    input = method.getName();
                }
    
                //copy from LowerCaseWithUnderscoresStrategy
                if (input == null) return input; // garbage in, garbage out
                int length = input.length();
                StringBuilder result = new StringBuilder(length * 2);
                int resultLength = 0;
                boolean wasPrevTranslated = false;
                for (int i = 0; i < length; i++)
                {
                    char c = input.charAt(i);
                    if (i > 0 || c != '_') // skip first starting underscore
                    {
                        if (Character.isUpperCase(c))
                        {
                            if (!wasPrevTranslated && resultLength > 0 && result.charAt(resultLength - 1) != '_')
                            {
                                result.append('_');
                                resultLength++;
                            }
                            c = Character.toLowerCase(c);
                            wasPrevTranslated = true;
                        }
                        else
                        {
                            wasPrevTranslated = false;
                        }
                        result.append(c);
                        resultLength++;
                    }
                }
                return resultLength > 0 ? result.toString() : input;
            }
        });
    
    0 讨论(0)
  • 2020-11-27 12:43

    Using both annotations below, forces the output JSON to include is_xxx:

    @get:JsonProperty("is_something")
    @param:JsonProperty("is_something")
    
    0 讨论(0)
  • 2020-11-27 12:54

    When you are using Kotlin and data classes:

    data class Dto(
        @get:JsonProperty("isSuccess") val isSuccess: Boolean
    )
    

    You might need to add @param:JsonProperty("isSuccess") if you are going to deserialize JSON as well.

    0 讨论(0)
提交回复
热议问题