简体   繁体   中英

Deserializing JSON object

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?

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}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM