2

I have the following classes -

Employee

public class Employee {

    private String firstName ;
    private String lastName ;
    private String emailAddress ; 
    private String ssn ;
}

Payroll

public class Payroll {

    // different payroll related fields

    private Employee emp ;

}

HR

public class HR {

    // different HR related fields

    private Employee emp ;

}

Now when I serialize my Payroll class, I don't want to serialize my ssn field from Employee class.

Where as when I serialize my HR class, I don't want to serialize my emailAddress field from Employee class.

How I can dynamically exclude fields from serializing by using Jackson JSON API?

jagamot
  • 5,348
  • 18
  • 59
  • 96

3 Answers3

6

How I can dynamically exclude fields from serializing by using Jackson JSON API?

This seems like a prime candidate for applying JacksonJsonViews.

public class Employee {

    private String firstName;
    private String lastName;
    @JsonView(Views.Payroll.class) private String emailAddress; 
    @JsonView(Views.HR.class) private String ssn;
}

public class Payroll {
    // snip

    @JsonView(Views.Payroll.class)
    private Employee emp;
}

public class HR {
    // snip

    @JsonView(Views.HR.class)
    private Employee emp;
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • If there is yet an other class, say Department which has Employee as an object and when serialized want to serialize everything from Employee, how does that work ? – jagamot Apr 02 '13 at 20:07
  • The linked documentation covers this. You could just disable view processing entirely in that case. – Matt Ball Apr 02 '13 at 20:14
  • Is there a way to choose which views are used based on some value determined at runtime and have it change? If not, then this doesn't answer the "dynamically" part of the question. – user695022 Sep 19 '16 at 21:41
1

You can achieve this with the help of a JsonFilter.

  1. Annotate the class you want to filter as thus:
@JsonFilter("employeeFilter")
public class Employee {

    private String firstName ;
    private String lastName ;
    private String emailAddress ; 
    private String ssn ;
}
  1. Create a service called FilterBeanService(Or anything really)
@Service
public class FilterBeanService {

    // fields is an array of field names you wish not to sned in your response
    // beanFilterName is value you give when you annotage your bean class
    // dataSet is the data you want to filter
    public static MappingJacksonValue filterBean(String[] fields, String beanFilterName, Object dataSet ) {
        SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAllExcept(fields);
        FilterProvider filterProvider = new SimpleFilterProvider().addFilter(beanFilterName, filter);
        MappingJacksonValue jacksonValue = new MappingJacksonValue(dataSet);
        jacksonValue.setFilters(filterProvider);
        return jacksonValue;
    }
}
  1. In your controller, you can then filter fileds you do not want
@GetMapping("/whatever")
public ResponseEntity<MappingJacksonValue> getSomething(){
   List<Employee> employees = eventRepo.findAll();
   String[] fields = {"ssn"};
   MappingJacksonValue jacksonValue = FilterBeanService.filterBean(fields, "employeeFilter", employees);
   return jacksonValue;
}
0

I answered below to dynamically deserialise with customer reader. Can do similar thing with writer to ignore when serialising.

Jackson Change JsonIgnore Dynamically

Satthy
  • 109
  • 1
  • 3
  • 10