如何使用Jackson验证重复的属性? [英] How to use Jackson to validate duplicated properties?

查看:468
本文介绍了如何使用Jackson验证重复的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson JSON库将一些JSON对象转换为POJO类。问题是,当我使用具有重复属性的JSON对象时:

I'm using Jackson JSON library to convert some JSON objects to POJO classes. The problem is, when I use JSON Objects with duplicated properties like:

{
  "name":"xiaopang",
  "email":"xiaopang1@123.com",
  "email":"xiaopang2@123.com"
}

杰克逊报告最后一封电子邮件对email:xiaopang2@123.com然后解析对象。

Jackson report the last email pair "email":"xiaopang2@123.com" and then parse the object.

我从 JSON语法是否允许对象中的重复键?当反序列化具有重复属性的JSON对象时发生的情况取决于库实现,抛出错误或使用最后一个重复键。

I've learned from Does JSON syntax allow duplicate keys in an object? that what happens when deserializing a JSON object with duplicate properties depends on the library implementation, either throwing an error or using the last one for duplicate key.

尽管跟踪所有属性的开销,有没有办法告诉Jackson报告错误或异常,例如重复密钥?

Despite overheads of tracking all properties, is there any way to tell Jackson to report an error or exception such as "Duplicate key" in this case?

推荐答案

使用 JsonParser.Feature.STRICT_DUPLICATE_DETECTION

ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
MyPOJO result = mapper.readValue(json, MyPOJO.class);

结果:

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Duplicate field 'email'






您还可以尝试使用 DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY 更多信息)如果你首先将你的json字符串/输入反序列化为jackson json树然后再反序给你POJO,它将被触发。可以将它与自定义 JsonDeserializer 结合使用:


You can also try to use DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY (more info) It will be trigggered if you deserialize your json string/input to jackson json tree first and then to you POJO. Can combine it with custom JsonDeserializer like this:

private static class MyPojoDeserializer extends JsonDeserializer<MyPOJO>{

    @Override
    public MyPOJO deserialize(JsonParser p, DeserializationContext ctxt) 
                                                           throws IOException{
        JsonNode tree = p.readValueAsTree();
        return  p.getCodec().treeToValue(tree, MyPOJO.class);
    }
}

设置一次并像以前一样使用它:

Setup it once and use it same way as before:

// setup ObjectMapper 
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
SimpleModule module = new SimpleModule();
module.addDeserializer(MyPOJO.class,new MyPojoDeserializer() );
mapper.registerModule(module);

// use
mapper.readValue(json, MyPOJO.class);

结果:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Duplicate field 'email' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled






其他选项是在自定义反序列化器或POJO setter方法中自己实现所有逻辑。


Other options would be to implement all the logic yourself in custom deserializer or in you POJO setter methods.

这篇关于如何使用Jackson验证重复的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆