I try to deserialize json:
{
"date": "2021_05",
"uuid": "3ba8b966-993f-49e0-b349-e528843a382c",
"dataset": "dataset",
"hmm_hit": "hit",
"hmm_evalue": "6.7e-186",
"hmm_score": "610.9"
},
I have two entities:
@Entity
public class HmmResult {
@Id
@GeneratedValue
@JsonIgnore
private Integer id;
@JsonProperty("hmm_hit")
private String hmm;
@JsonProperty("hmm_evalue")
private String eValue;
@JsonProperty("hmm_score")
private Float score;
}
and
@Entity
public class Protein {
@Id
@GeneratedValue
@JsonIgnore
private Integer id;
@JsonProperty("date")
private String date;
@JsonProperty("uuid")
private String uuid;
@JsonProperty("dataset")
private String dataset;
@OneToOne
@JsonDeserialize(as = HmmResult.class)
private HmmResult hmmResult;
How to deserialize both entities at the same time with one entry of json? Here is extract of main with Jackson ObjectMapper:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Protein p = objectMapper.readValue(new File(file), Protein.class);
It parses "date", "uuid" and "dataset" fine but can not parse HmmResult object with "hmm_subfamily", "hmm_evalue" and "hmm_score" values: I get error: p.getHmmResult(): null. (HmmResult hm = objectMapper.readValue(new File(file), HmmResult.class); works fine too alone).
There is @JsonUnwrapped
annotation for this. Should work like this:
public class Protein {
@JsonProperty("date")
private String date;
@JsonProperty("uuid")
private String uuid;
@JsonProperty("dataset")
private String dataset;
@JsonUnwrapped
private HmmResult hmmResult;
}
var protein = new ObjectMapper().readValue(new File(file), Protein.class);
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.