0

I need to add @JsonIgnore annotated fields while serializing an object by Jackson ObjectMapper. I know you may offer me to remove the @JsonIgnore annotation from my class, but I need they are ignorable in some part of my application. And in another part of my application I need to have those @JsonIgnore annotated fields in my json string.

gabby
  • 629
  • 2
  • 11
  • 34

4 Answers4

4

You can define a SimpleBeanPropertyFilter and FilterProvider.

First annotate your class with custom filter like this:

@JsonFilter("firstFilter")
public class MyDtoWithFilter {

    private String name;

    private String anotherName;
    private SecondDtoWithFilter dtoWith;

    // get set ....
}
 @JsonFilter("secondFilter")
public class SecondDtoWithFilter{
    private long id;
    private String secondName;
}

and this is how you will dynamically serialise your object.

    ObjectMapper mapper = new ObjectMapper();

    // Field that not to be serialised. 
    SimpleBeanPropertyFilter firstFilter = SimpleBeanPropertyFilter.serializeAllExcept("anotherName");
     SimpleBeanPropertyFilter secondFilter = SimpleBeanPropertyFilter.serializeAllExcept("secondName");

    FilterProvider filters = new SimpleFilterProvider().addFilter("firstFilter", firstFilter).addFilter("secondFilter", secondFilter);

    MyDtoWithFilter dtoObject = new MyDtoWithFilter();
    String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
gabby
  • 629
  • 2
  • 11
  • 34
PyThon
  • 1,007
  • 9
  • 22
  • Thank you for the answer. How can I add sub field in serializeAllExcept? I mean I have a class and one attribute of this class is another class. And this class also has some fields which should serialized or not – gabby Feb 27 '16 at 17:38
  • then in that case you must add the full path to your property. updating answer code above for this. Happy Coding !! :) – PyThon Feb 27 '16 at 17:54
  • Unfortunately, it did not work. I still can see innerProperty2 inb my json string. but attribute1 is not in json as expected. – gabby Feb 27 '16 at 18:10
  • @gabby please edit my answer as well with your working code of nested properties. It will help others. Thanks – PyThon Feb 27 '16 at 19:34
0

I would suggest removing and re-adding them programmatically via reflection when your specific mapping is happening.

Henning Luther
  • 2,067
  • 15
  • 22
0

That suggests to me you have two different models with some common elements. I would reexamine your model.

Gavin
  • 1,725
  • 21
  • 34
0
public class MainProgram {
    @JsonFilter("nameRemoveFilter")
    public static class User{

        private String name;

        private String age;

        private String password;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }



    }

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        FilterProvider filters = new SimpleFilterProvider().addFilter("nameRemoveFilter",
                SimpleBeanPropertyFilter.filterOutAllExcept("name","age"));
            // and then serialize using that filter provider:
            User user = new User();
            try {
                String json = mapper.writer(filters).writeValueAsString(user);
                System.out.println(json);
            } catch (JsonProcessingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }


    Works for Latest version of Jackson after 2.0
Sarath Krrish
  • 221
  • 1
  • 5