仅用于嵌套类的自定义jackson反序列化程序

cclgggtu  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(99)

我定义了一个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没有为位置定义latitudelongitudestartend);该信息仅被编码为数组,参见例如:

// 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,这种方法可行吗?

33qvvth1

33qvvth11#

您可以使用@JsonCreator标记和Double[]为参数startend添加构造函数到Vehicle类。您还需要为每个参数添加@JsonProperty标记。
这里有一个例子,为了简单起见,我没有包括参数time_windowbreaks

@JsonCreator
public Vehicle(@JsonProperty("id") Integer id, 
        @JsonProperty("description") String description,
        @JsonProperty("start") Double[] start, 
        @JsonProperty("end") Double[] end,
        @JsonProperty("capacity") List<Integer> capacity, 
        @JsonProperty("skills") List<Integer> skills) {
    this(id, description, new Location(start[0], start[1]), 
            new Location(end[0], end[1]), capacity, skills);
}

使用问题中的json进行测试,但不使用参数time_windowbreaks

String result = "{\"id\":0,\"description\":\"vehicle 0\",\"start\":[12.304373066846503,51.62270653765847],\"end\":[12.304373066846503,51.62270653765847],\"capacity\":[9],\"skills\":[]}";

ObjectMapper mapper = new ObjectMapper();

Vehicle vehicle = mapper.readValue(result, Vehicle.class);

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(vehicle);

System.out.println(json);

输出量:

{
  "id" : 0,
  "description" : "vehicle 0",
  "start" : {
    "latitude" : 12.304373066846503,
    "longitude" : 51.62270653765847
  },
  "end" : {
    "latitude" : 12.304373066846503,
    "longitude" : 51.62270653765847
  },
  "capacity" : [ 9 ],
  "skills" : [ ]
}

相关问题