Open
Description
I have a MenuModel that extends ResourceSupport. The MenuModel inturn contains a Set. The SubMenuModel also extends ResourceSupport.
When I try to marshall / unmarshall the MenuModel, only the first element of the SubMenuModel is attached to the MenuModel and rest of the elements are lost.
Please find below the sample code for recreating the issue,
@JsonInclude(value = Include.NON_NULL)
public class MenuModel extends ResourceSupport implements Serializable{
private static final long serialVersionUID = 1698404814795439732L;
private String name;
private boolean active;
private Set<SubMenuModel> submenus;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<SubMenuModel> getSubmenus() {
return submenus;
}
public void setSubmenus(Set<SubMenuModel> submenus) {
this.submenus = submenus;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
@JsonInclude(value=Include.NON_NULL)
public class SubMenuModel extends ResourceSupport implements Serializable {
private static final long serialVersionUID = -4096365611186035173L;
private String name;
private boolean active;
@JsonIgnore
private MenuModel menu;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MenuModel getMenu() {
return menu;
}
public void setMenu(MenuModel menu) {
this.menu = menu;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
I am trying to convert the following json to an Object & vice versa and I could find that the second element is lost,
sample json:
{"name":"Electronics","active":true,"submenus":[{"name":"Mobiles","active":true}, {"name":"Tablets","active":true}]}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
MenuModel menuModel = objectMapper.readValue(new File("E:\\test.json"), MenuModel.class);
String value = objectMapper.writeValueAsString(menuModel);
System.out.println(value);
}
resulting json:
{"name":"Electronics","active":true,"submenus":[{"name":"Mobiles","active":true,"links":[]}],"links":[]}
All the elements are in the SubMenuModel is retained when I remove ResourceSupport.