Hi I have few UI objects that embedded into each other. Example #1:
class CarUI {
public UserUI user;
public UserUI agent;
....
public UserUI getUser() {
return user;
}
public UserUI getAgent() {
return agent;
}
}
class UserUI {
@JsonView({Views.Public.class,Views.Private.class})
public String name;
@JsonView({Views.Private.class})
public String phone;
...
}
As you can see if I serialize UserUI as Public I will expose only name but for private I will expose all fields.
Here is my question: when I serialize object CarUI I want to expose only name for "user" and both name+phone for "agent". That essentially means that in the getter for the getUser() I want to change JsonView to Views.Public.class and for getAgent() I want to change it to Views.Private.class
Is there way to overwrite JsonView in getters to make all hierarchy for the returned object now serialize using this new JsonView?
Edited As pointed below you can solve my Example#1 with JsonView inheritance. What about more generic. Here is Example#2:
class CarUI {
public UserUI user;
public UserUI agent;
....
public UserUI getUser() {
return user;
}
public UserUI getAgent() {
return agent;
}
}
class UserUI {
@JsonView({Views.Public.class,Views.Private.class})
public String name;
@JsonView({Views.Private.class})
public String phone;
@JsonView({Views.Public.class})
public String avatarUrl;
...
}
In this example Id like to display name+phone for Agent and name+avatarUrl for User.