我定义了一个vehicle
Java类,如下所示:
public final class Vehicle {
private Integer id;
private String description;
private Location start;
private Location end;
private List<Integer> capacity;
private List<Integer> skills;
private TimeWindow timeWindow;
private List<Break> breaks;
public Vehicle(Integer id, String description, Location start,
Location end, List<Integer> capacity,
List<Integer> skills, TimeWindow timeWindow,
List<Break> breaks) {
this.id = id;
this.description = description;
this.start = start;
this.end = end;
this.capacity = capacity;
this.skills = skills;
this.timeWindow = timeWindow;
this.breaks = breaks;
}
TimeWindow
的定义如下:
public final class Location {
private final Double latitude;
private final Double longitude;
public Location(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
现在,我得到的JSON没有为位置定义latitude
和longitude
(start
和end
);该信息仅被编码为数组,参见例如:
// vehicle.json
{
"id" : 0,
"description" : "vehicle 0",
"start" : [
12.304373066846503,
51.62270653765847
],
"end" : [
12.304373066846503,
51.62270653765847
],
"capacity" : [
9
],
"skills" : [
],
"time_window" : [
1644188400,
1644274800
],
"breaks" : [
]
}
在这种情况下,我如何仅为Location
编写自定义反序列化器(TimeWindow
也有同样的问题)?如果可能,我不想为整个Vehicle
类编写自定义反序列化器。
我试过这个:
第一个
在我看来,我把整个Vehicle
传递到了我的deserialize
方法中,而不仅仅是Location
部分。我在这里做错了什么吗?使用Jackson,这种方法可行吗?
1条答案
按热度按时间33qvvth11#
您可以使用
@JsonCreator
标记和Double[]
为参数start
和end
添加构造函数到Vehicle
类。您还需要为每个参数添加@JsonProperty
标记。这里有一个例子,为了简单起见,我没有包括参数
time_window
和breaks
。使用问题中的
json
进行测试,但不使用参数time_window
和breaks
:输出量: