2

I am having trouble deserializing a JSON object. The object contains a collection which is being deserialized as a Map, which is the default. I need it to deserialize as a Set. My code is as follows:

TaskDetail.java

@ManyToMany
private Set<RoleDetail> roleDetails = new HashSet<RoleDetail>();

public String toJson() {
    return new JSONSerializer().exclude("*.class").include("roleDetails").serialize(this);
}

RoleDetail.java

@ElementCollection
@Enumerated(EnumType.STRING)
private Set<RoleFunction> roleFunctions = new HashSet<RoleFunction>();

public String toJson() {
    return new JSONSerializer().exclude("*.class").include("roleFunctions").serialize(this);
}

From the front-end, I submit the data from my form which is received by my controller in the following format:

{"name":"Clean Shelves","description":"Clean all shelves in the store","roleDetails":{"description":"A person that counts stock","id":1,"name":"Stock Counter","version":0}}

I need the roleDetails object to be deserialized as a HashSet. How can I use the JsonDeserializer to do this? I assume it is something like this in RoleDetail.java:

public static RoleDetail fromJsonToRoleDetail(String json) {
    return new JSONDeserializer<RoleDetail>().use(null, RoleDetail.class).use("roleDetail.values", HashSet.class).deserialize(json);
}

Or do I also have to code something similare in TaskDetail.java?

Neriyan
  • 155
  • 2
  • 2
  • 14

1 Answers1

1

Your roleDetails should be an array of objects, not an object.

You should have something like that :

{"name":"Clean Shelves","description":"Clean all shelves in the store","roleDetails":[{"description":"A person that counts stock","id":1,"name":"Stock Counter","version":0}]}
eugen
  • 5,856
  • 2
  • 29
  • 26