Reputation: 7692
I've a use case where it seems more appropriate to use JSONView annotation with exclusion information, for example:
@JSONView(Views.Report1.class, include=false)
This is not (include attribute) directly supported in Jackson (1.9.2) as of now as, I am wondering is there an easy workaround in Jackson to achieve this.
Use case:
id, name, info1, info2, info3, info4
attributesid, name, info1, info2, info4
attributesprivate int id; private String name; private String info1; private String info2; //ignore if view=report2 @JsonView(ReportViews.Report2.class , include=false) private String info3; private String info4;
My use case is excluding attributes based on views (or say report-ids). With JSONView approach I need to add all views to info3 attribute except Report2 to get it excluded. Doesn't fit well.
What should be the correct approach in this scenario? Is customized JSONView to except exclusion/inclusion would be right solution if not something similar already available.
Upvotes: 6
Views: 3656
Reputation: 101
There doesn't seem to be a way to exclude fields from a specific view in that manner.
However you can structure your views very flexibly by using interfaces to compose the view from needed elements.
In the above case I would try this:
public class ReportViews {
public interface NeedsInfo3 {};
public static class Report1 implements NeedsInfo3 {};
public static class Report2 {};
}
Then in your model use the field specific view.
private int id;
private String name;
private String info1;
private String info2;
@JsonView(ReportViews.NeedsInfo3.class)
private String info3;
private String info4;
Upvotes: 2
Reputation: 116572
Jackson's JSON View functionality is based on criteria for inclusion, so you can not change existing annotations.
Upvotes: 1