2

Consider a case that I have 2 instance of Object Mapper. I want one must exclude fields that are annotated with some custom annotation from serialization While other mapper includes(ignores annotation)

Like class has 3 fields a,b,c and c is annotated with some annotation (say @IgnoreField) (Their will n number of class, each will have their Fields that are not meant to be serialized)

Now 1st object mapper o1 must serialize only a and b. While 2nd object mapper o2 can serialize a,b and c.

This can happen with any class having different fields some of which may be annotated.

3 Answers3

0

Try configure SimpleBeanPropertyFilter for different condition.

@JsonFilter("someBeanFilter")
public class SomeBean {
}

SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter("someBeanFilter",SimpleBeanPropertyFilter.serializeAllExcept("aFild")); 

ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(filterProvider); 
Eugen
  • 877
  • 6
  • 16
0

You can always implement a custom JsonSerializer and register it with your ObjectMapper.

class Bean {
    @Ignore
    String a;
    String b;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Ignore {

}

class BeanWithIgnoredFieldsSerializer extends JsonSerializer<Bean> {

    @Override
    public void serialize(final Bean value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        try {
            for (final Field f : Bean.class.getFields()) {
                if (f.isAnnotationPresent(Ignore.class)) {
                    gen.writeStringField(f.getName(), (String) f.get(value));
                }
            }
        } catch (final Exception e) {
            //
        }
        gen.writeEndObject();
    }

}

class BeanModule extends SimpleModule {

    BeanModule() {
        addSerializer(Bean.class, new BeanWithIgnoredFieldsSerializer());
    }
}

void configure(final ObjectMapper om) {
    om.registerModule(new BeanModule());
}

Note I have not tested this code, but that is the general idea how you add custom serializers to the OM. Adjust the code within the serialize method however you want.

sfiss
  • 2,119
  • 13
  • 19
-1

A distinct non-answer:

This is most likely a terrible idea.

You write code to communicate your intention. When you use that annotation, then you are telling "everybody" that these fields should be ignored.

A human reader looking at your code might spend half a day asking himself later "it says @IgnoreField for a and c , so why the heck are a, and c showing up serialized data?"

In other words, whatever problem you are trying to solve here, the answer is most likely not by hacking your way into ignoring annotations sometimes.

The next best "reasonable" solution might be: to rely on different custom annotations, like @IgnoreAlways and something like @OnlyIncludeForXyz. In other words: clearly express what might happen. Instead of using declarative programming, to then "lie" about what you declared.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I got your point but it was just an example actual annotation was @FieldDescriptor with some properties. – Rajat Jindal Sep 24 '19 at 10:27
  • @RajatJindal well, then you should have said so. That is the point why people suggest that you should always provide a [mcve] instead of only describing your problem in vague terms. – GhostCat Sep 24 '19 at 10:35