Jackson Change JsonIgnore Dynamically

后端 未结 3 1872
执念已碎
执念已碎 2020-11-28 10:32

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 d

相关标签:
3条回答
  • 2020-11-28 10:56

    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.

    It might also help if you gave an example of practical application, i.e what and why you are trying to prevent from being deserialized.

    0 讨论(0)
  • 2020-11-28 11:10

    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 )

    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'.
    
    0 讨论(0)
  • 2020-11-28 11:12

    You should probably look at the modules feature of recent Jackson versions.

    One possible mechanism would be to use a 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 ObjectMappers manually, or using them in a JAX-RS setting, injected in Spring, or what?

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