Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:52:40+00:00 2026-06-12T12:52:40+00:00

Given the following class hierarchy, I would like Foo to be serialized differently depending

  • 0

Given the following class hierarchy, I would like Foo to be serialized differently depending on the context it is used in my class hierarchy.

public class Foo {
    public String bar;
    public String biz;
}

public class FooContainer {
    public Foo fooA;
    public Foo fooB;
}

I would like for the biz attribute to not show up in fooB when I serialize FooContainer. So the output would look something like the following.

{
  "fooA": {"bar": "asdf", "biz": "fdsa"},
  "fooB": {"bar": "qwer"}
}

I was going to use something JsonView, but that has to be applied at the mapper layer for all instances of a class, and this is context dependent.


On the Jackson user mailing list, Tatu gave the simplest solution (works in 2.0), which I will probably end up using for now. Awarding the bounty to jlabedo because the answer is an awesome example of how to extend Jackson using custom annotations.

public class FooContainer {
    public Foo fooA;

    @JsonIgnoreProperties({ "biz" })
    public Foo fooB;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:52:41+00:00Added an answer on June 12, 2026 at 12:52 pm

    You could use a combination of a custom serializer with a custom property filter using JsonViews. Here is some code working with Jackson 2.0

    Define a custom annotation:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface FilterUsingView {
        Class<?>[] value();
    }
    

    Define some Views:

    // Define your views here
    public static class Views {
        public class Public {};
        public class Internal extends Public{};
    }
    

    Then you can write your entities like this. Note that you may define your own annotation instead of using @JsonView :

    public class Foo {
        @JsonView(Views.Public.class)
        public String bar;
        @JsonView(Views.Internal.class)
        public String biz;
    }
    
    public class FooContainer {
        public Foo fooA;
        @FilterUsingView(Views.Public.class)
        public Foo fooB;
    }
    

    Then, here is where the code begins 🙂
    First your custom filter:

    public static class CustomFilter extends SimpleBeanPropertyFilter {
    
        private Class<?>[] _nextViews;
    
        public void setNextViews(Class<?>[] clazz){
            _nextViews = clazz;
        }
    
        @Override
        public void serializeAsField(Object bean, JsonGenerator jgen,
                SerializerProvider prov, BeanPropertyWriter writer)
                throws Exception {
    
            Class<?>[] propViews = writer.getViews();
    
            if(propViews != null && _nextViews != null){
                for(Class<?> propView : propViews){
                    System.out.println(propView.getName());
                    for(Class<?> currentView : _nextViews){
                        if(!propView.isAssignableFrom(currentView)){
                            // Do the filtering!
                            return;
                        }
                    }
                }
            }
            // The property is not filtered
            writer.serializeAsField(bean, jgen, prov);
        }
    }
    

    Then a custom AnnotationIntrospector that will do two things:

    1. Enable your custom filter for any bean… Unless another filter is defined on your class (so you cannot use both of them, if you see what I mean)
    2. Enable your CustomSerializer if he found a @FilterUsingView annotation.

    Here is the code

    public class CustomAnnotationIntrospector extends AnnotationIntrospector {
        @Override
        public Version version() {
            return DatabindVersion.instance.version();
        }
    
        @Override
        public Object findFilterId(AnnotatedClass ac) {
          // CustomFilter is used for EVERY Bean, unless another filter is defined
          Object id = super.findFilterId(ac);
          if (id == null) {
            id = "CustomFilter";
          }
          return id;
        }
    
        @Override
        public Object findSerializer(Annotated am) {
            FilterUsingView annotation = am.getAnnotation(FilterUsingView.class);
            if(annotation == null){
                return null;
            }
            return new CustomSerializer(annotation.value());
        }
    }
    

    Here is your custom serializer. The only thing it does is passing your annotation’s value to your custom filter, then it let the default serializer do the job.

    public class CustomSerializer extends JsonSerializer<Object> {
    
        private Class<?>[] _activeViews;
    
        public CustomSerializer(Class<?>[] view){
            _activeViews = view;
        }
    
        @Override
        public void serialize(Object value, JsonGenerator jgen,
                SerializerProvider provider) throws IOException,
                JsonProcessingException {
    
            BeanPropertyFilter filter = provider.getConfig().getFilterProvider().findFilter("CustomFilter");
            if(filter instanceof CustomFilter){
                CustomFilter customFilter = (CustomFilter) filter;
    
                // Tell the filter that we will filter our next property
                customFilter.setNextViews(_activeViews);
    
                provider.defaultSerializeValue(value, jgen);
    
                // Property has been filtered and written, do not filter anymore
                customFilter.setNextViews(null);
            }else{
                // You did not define a CustomFilter ? Well this serializer is useless...
                provider.defaultSerializeValue(value, jgen);
            }
        }
    }
    

    Finally ! Let’s put this all together :

    public class CustomModule extends SimpleModule {
    
        public CustomModule() {
            super("custom-module", new Version(0, 1, 0, "", "", ""));
        }
    
        @Override
        public void setupModule(SetupContext context) {
            super.setupModule(context);
            AnnotationIntrospector ai = new CustomAnnotationIntrospector();
            context.appendAnnotationIntrospector(ai);
        }
    
    }
    
    
    
    @Test
    public void customField() throws Exception {
        FooContainer object = new FooContainer();
        object.fooA = new Foo();
        object.fooA.bar = "asdf";
        object.fooA.biz = "fdsa";
        object.fooB = new Foo();
        object.fooB.bar = "qwer";
        object.fooB.biz = "test";
    
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new CustomModule());
    
        FilterProvider fp = new SimpleFilterProvider().addFilter("CustomFilter", new CustomFilter());
        StringWriter writer = new StringWriter();
    
        mapper.writer(fp).writeValue(writer, object);
    
        String expected = "{\"fooA\":{\"bar\":\"asdf\",\"biz\":\"fdsa\"},\"fooB\":{\"bar\":\"qwer\"}}";
    
        Assert.assertEquals(expected, writer.toString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following class, used to build a tree hierarchy: public class simpletest {
Given the following class structure, will Bar serialize/deserialize as expected? public class Foo {
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following object: public class Product { string Name {get;} int Quantity {get;}
Given the following assemblage of classes (contrived): public class School { [PrimaryKey] public string
Given the following class: public class MyClass { private string _param; public MyClass ()
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
Given the following class public class Website { @NotNull String owner: @ValidUrl String url;
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following multiton: public class Multiton { private static final Multiton[] instances =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.