杰克逊动态过滤反序列化过程中的属性

时间:2012-09-06 17:34:46

标签: java json filtering jackson deserialization

我有一个REST WS来更新一个接收JSON字符串作为输入的bean对象。

ABean entity = svc.findEntity(...);
objectMapper.readerForUpdating(entity).readValue(json);
[...]
svc.save(entity);

ABean是一个包含其他对象的复杂类型,例如:

class ABean {
    public BBean b;
    public CBean c;

    public String d;
}

svc.save(...)将保存bean和嵌入对象

出于安全原因,我想过滤掉一些可以通过JSON字符串更新的属性,但我想动态地执行此操作,以便对于每个WS(或用户角色),我可以决定要阻止哪些属性更新(所以我不能简单地使用杰克逊观点)

总结一下,有什么办法可以在JSON反序列化期间动态过滤掉属性吗?

4 个答案:

答案 0 :(得分:7)

另一种方法是使用BeanDeserializerModifier:

private static class BeanDeserializerModifierForIgnorables extends BeanDeserializerModifier {

        private java.lang.Class<?> type;
        private List<String> ignorables;

        public BeanDeserializerModifierForIgnorables(java.lang.Class clazz, String... properties) {
            ignorables = new ArrayList<>();
            for(String property : properties) {
                ignorables.add(property);
            }
            this.type = clazz;
        }

        @Override
        public BeanDeserializerBuilder updateBuilder(
                DeserializationConfig config, BeanDescription beanDesc,
                BeanDeserializerBuilder builder) {
            if(!type.equals(beanDesc.getBeanClass())) {
                return builder;
            }

            for(String ignorable : ignorables) {
                builder.addIgnorable(ignorable);                
            }

            return builder;
        }

        @Override
        public List<BeanPropertyDefinition> updateProperties(
                DeserializationConfig config, BeanDescription beanDesc,
                List<BeanPropertyDefinition> propDefs) {
            if(!type.equals(beanDesc.getBeanClass())) {
                return propDefs;
            }

            List<BeanPropertyDefinition> newPropDefs = new ArrayList<>();
            for(BeanPropertyDefinition propDef : propDefs) {
                if(!ignorables.contains(propDef.getName())) {
                    newPropDefs.add(propDef);
                }
            }
            return newPropDefs;
        }
    }

您可以使用以下命令将修改器注册到ObjectMapper:

BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables(YourType.class, "name");
DeserializerFactory dFactory = BeanDeserializerFactory.instance.withDeserializerModifier(modifier);
ObjectMapper mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(dFactory));

然后忽略定义的属性。如果使用@JsonAnySetter注释,则可以忽略updateBuilder方法。

问候, 马丁

答案 1 :(得分:1)

我从您的描述中假设您不能简单地使用@JsonIgnore注释来阻止为每个特定类序列化字段。

使用Jakson mix-ins查看:mix-ins允许您使用必要的注释替换类定义 - 用于数据绑定。在序列化期间,您可以选择特定的混合类定义,以站在中为要序列化的实际类。定义一组混合来处理每个案例,然后选择在序列化特定bean时哪个是合适的。

答案 2 :(得分:1)

您可以使用@JsonIgnoreType忽略序列化中的java类/接口/枚举。此外,您可以使用@JsonIgnoreProperties忽略特定属性的属性列表和@JsonIgnore

答案 3 :(得分:0)

我提出的最强大,快速和简单的解决方案是过滤从反序列化中获得的JsonNode树,然后将过滤后的结果传递给readerForUpdating。这样的事情:

public class JacksonHelper {
    public JsonNode filterBeanTree(JsonNode o, List<String> includedProperties,
            List<String> excludedProperties, int maxDepth) {
        JsonNode tree = o.deepCopy();
        this.filterBeanTreeRecursive(tree, includedProperties, excludedProperties, maxDepth, null);
        return tree;
    }

    private void filterBeanTreeRecursive(JsonNode tree, List<String> includedProperties,
            List<String> excludedProperties, int maxDepth, String key) {
        Iterator<Entry<String, JsonNode>> fieldsIter = tree.fields();
        while (fieldsIter.hasNext()) {
            Entry<String, JsonNode> field = fieldsIter.next();
            String fullName = key == null ? field.getKey() : key + "." + field.getKey();

            boolean depthOk = field.getValue().isContainerNode() && maxDepth >= 0;
            boolean isIncluded = includedProperties != null
                    && !includedProperties.contains(fullName);
            boolean isExcluded = excludedProperties != null
                    && excludedProperties.contains(fullName);
            if ((!depthOk && !isIncluded) || isExcluded) {
                fieldsIter.remove();
                continue;
            }

            this.filterBeanTreeRecursive(field.getValue(), includedProperties, excludedProperties,
                    maxDepth - 1, fullName);
        }
    }
} 
相关问题