简体   繁体   English

杰克逊动态改变JsonIgnore

[英]Jackson Change JsonIgnore Dynamically

I have a class and there are variables inside it as well. 我有一个类,里面也有变量。 Sometimes I want to ignore some fields and sometimes not when deserializing (maybe at serializing too). 有时我想忽略一些字段,有时候不反序列化(也许在序列化时)。 How can I do it at Jackson? 我怎么能在杰克逊这样做?

For serialization, " filtering properties " blog entry should help. 对于序列化,“ 过滤属性 ”博客条目应该有所帮助。 Deserialization side has less support, since it is more common to want to filter out stuff that is written. 反序列化方面的支持较少,因为想要过滤掉所写的东西更为常见。

One possible approach is to sub-class JacksonAnnotationIntrospector , override method(s) that introspect ignorability of methods (and/or fields) to use whatever logic you want. 一种可能的方法是对JacksonAnnotationIntrospector进行子类化,重写方法(内省)方法(和/或字段)的无知性,以使用您想要的任何逻辑。

It might also help if you gave an example of practical application, ie what and why you are trying to prevent from being deserialized. 如果你举一个实际应用的例子,也就是说你试图阻止被反序列化的原因和原因也可能有所帮助。

You might want to use JsonViews ( took it originally from http://wiki.fasterxml.com/JacksonJsonViews - broken now - web archive link: https://web.archive.org/web/20170831135842/http://wiki.fasterxml.com/JacksonJsonViews ) 您可能想要使用JsonViews(最初来自http://wiki.fasterxml.com/JacksonJsonViews - 现在已经破解 - 网站存档链接: https ://web.archive.org/web/20170831135842/http://wiki 。 fasterxml.com/JacksonJsonViews

Quoting it: 引用它:

First, defining views means declaring classes; 首先,定义视图意味着声明类; you can reuse existing ones, or just create bogus classes -- they are just view identifiers with relationship information (child inherits view membership from parents): 您可以重用现有的类,或者只创建虚假类 - 它们只是具有关系信息的视图标识符(子级继承父级的视图成员资格):

 // View definitions:
  class Views {
            static class Public { }
            static class ExtendedPublic extends PublicView { }
            static class Internal extends ExtendedPublicView { }
  }

  public class Bean {
            // Name is public
            @JsonView(Views.Public.class) String name;
            // Address semi-public
            @JsonView(Views.ExtendPublic.class) Address address;
            // SSN only for internal usage
            @JsonView(Views.Internal.class) SocialSecNumber ssn;
  }

With such view definitions, serialization would be done like so: 使用这样的视图定义,序列化将如下所示:

 // short-cut:
  objectMapper.writeValueUsingView(out, beanInstance, ViewsPublic.class);

  // or fully exploded:
  objectMapper.getSerializationConfig().setSerializationView(Views.Public.class);
  // (note: can also pre-construct config object with 'mapper.copySerializationConfig'; reuse)
  objectMapper.writeValue(out, beanInstance); // will use active view set via Config

  // or, starting with 1.5, more convenient (ObjectWriter is reusable too)
  objectMapper.viewWriter(ViewsPublic.class).writeValue(out, beanInstance);
and result would only contain 'name', not 'address' or 'ssn'.

You should probably look at the modules feature of recent Jackson versions. 您应该查看最近Jackson版本的模块功能

One possible mechanism would be to use a BeanDeserializerModifier . 一种可能的机制是使用BeanDeserializerModifier

I've been looking for a useful online tutorial or example, but nothing immediately appears. 我一直在寻找一个有用的在线教程或示例,但没有立即出现。 It might be possible to work something up if more is known of your context. 如果了解您的上下文,可能会有所改进。 Are you managing your ObjectMapper s manually, or using them in a JAX-RS setting, injected in Spring, or what? 您是在手动管理ObjectMapper ,还是在JAX-RS设置中使用它们,在Spring中注入,或者是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM