Fasterxml Jackson会自动将非布尔值转换为布尔值

时间:2016-10-25 16:09:02

标签: java json serialization jackson fasterxml

我有一个pojo类,其中一个标志isControl是布尔类型。

当此属性获得除true or false以外的非布尔值时,fastxml jackson会自动将输入值转换为true。经过几个小时的调试后,我发现这是在setter方法setIsControl中发生的。

如果此属性的输入值是非布尔值,我想传递自定义消息。我编写了自己的注释来验证此属性的输入值,如果它不是布尔值,则返回自定义消息,但jackson在检查我的自定义验证器之前绑定该值。

使用jackson版本>>> 2.6.3。任何帮助将不胜感激。

Control.java

    @JsonProperty(required = true)
    @NotNull(message = "isControl cannot be null")
    private Boolean isControl;

    public Boolean getIsControl() {
            return isControl;
    }


    @CheckBoolean(fieldName = "isControl")
    public void setIsControl(Boolean isControl) {
            this.isControl = isControl;
    }

public class BooleanValidator implements ConstraintValidator<CheckBoolean,  Boolean> {

    private String fieldName;

    @Override
    public void initialize(CheckBoolean constraintAnnotation) {
        this.fieldName = constraintAnnotation.fieldName();
    }

    @Override
    public boolean isValid(Boolean value, ConstraintValidatorContext context) {         
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(
                String.format("The control flag %s should be either true or false", fieldName))
                .addConstraintViolation();

        if (value != null) {
            boolean isBoolean;
            if (value instanceof Boolean) {                 
                isBoolean = ((Boolean)value).booleanValue();
                System.out.println("var isBoolean: " +isBoolean);
                return true;
            } else if (value instanceof Boolean && Boolean.FALSE.equals(value)) {
                isBoolean = ((Boolean)value).booleanValue();                    
                return true;
            } else {
                return false;
            }           
        }
return false;
}
}

例外:

2 个答案:

答案 0 :(得分:1)

我认为这是因为setter将其设置为boolean。以下作品。在这种情况下,我将boolean作为反序列化类中的对象。

public class Json {
    private String key1;
    private Object booleanKey;
    public String getKey1() {
        return key1;
    }
    public void setKey1(String key1) {
        this.key1 = key1;
    }
    public Object getBooleanKey() {
        return booleanKey;
    }
    public void setBooleanKey(Object booleanKey) {
        this.booleanKey = booleanKey;
    }
}

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        String jsonString = "{\"key1\" : \"value1\", \"booleanKey\" : true}";

        ObjectMapper mapper = new ObjectMapper();
        Json obj = mapper.readValue(jsonString,Json.class);
        if (obj.getBooleanKey() instanceof Boolean) {
            System.out.println("booleanKey is boolean");
        } else {
            System.out.println("booleanKey is some other beast");
        }


        jsonString = "{\"key1\" : \"value1\", \"booleanKey\" : \"test\"}";
        obj = mapper.readValue(jsonString, Json.class);
        if (obj.getBooleanKey() instanceof Boolean) {
            System.out.println("booleanKey is boolean");
        } else {
            System.out.println("booleanKey is some other beast");
        }
    }

答案 1 :(得分:1)

有两种方法可以做到这一点,假设您将布尔字段作为对象类型映射为HARDI应答 -

<强> 1。自定义setter方法 -

    public class DTO {
    String key1;
    Object booleanKey;

    public Object getBooleanKey() {
        return booleanKey;
    }

    public void setBooleanKey(Object booleanKey) {
        if (booleanKey instanceof Boolean) {
            this.booleanKey = booleanKey;
        } else {
            // custom code here
        }

    }

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }
    }

<强> 2。编写自定义反序列化程序 -

class BooleanKeyDeserializer extends JsonDeserializer<Object> {

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Object object = p.readValueAs(Object.class);
    if (!(object instanceof Boolean)) {
        // custom code here
    }
    return object;
}
}

注释要为其执行自定义反序列化的字段 -

class DTO {
String key1;
@JsonDeserialize(using = BooleanKeyDeserializer.class)
Object booleanKey;
//setters getters
}
相关问题