如何使用 Jackson 反序列化混合类型的匿名数组

How to deserialise anonymous array of mixed types with Jackson

在我的 Java 程序中,我试图解析从 Strava.com 的 API 中获得的数据。我从那里收到的 JSON 有效负载之一如下所示:

[
  {"type": "altitude","data": [519.1,519.3,519.3,519.4,519.5],"series_type": "distance","original_size": 5,"resolution": "high"},
  {"type": "latlng","data": [[46.01234,6.01234],[46.11234,6.11234],[46.21234,6.21234],[46.31234,6.31234],[46.41234,6.41234]],"series_type": "distance","original_size": 5,"resolution": "high"},
  {"type": "velocity_smooth","data": [0.0,0.0,0.0,5.5,5.2],"series_type": "distance","original_size": 5,"resolution": "high"},
  {"type": "distance","data": [0.0,8.6,11.8,16.6,20.8],"series_type": "distance","original_size": 5,"resolution": "high"},
  {"type": "time","data": [0,1,2,3,4],"series_type": "distance","original_size": 5,"resolution": "high"}
]

基本上,其中四个条目(高度、velocity_smooth、距离和时间)具有相同的结构(它们的 data 字段是一个双精度数组(或可以解析为双精度的整数)),但第二个条目 (latlng) 的 data 字段结构略有不同(它是双精度数组的数组)。

如果所有内容都已命名,我熟悉 Jackson 库在 JSONPOJO 之间进行转换,但不知道如何对上述数据结构建模对其进行反序列化。

假设上面的数据看起来如下:

{
  "altitude": {"data": [519.1,519.3,519.3,519.4,519.5],"series_type": "distance","original_size": 5,"resolution": "high"},
  "latlng":  {"data": [[46.01234,6.01234],[46.11234,6.11234],[46.21234,6.21234],[46.31234,6.31234],[46.41234,6.41234]],"series_type": "distance","original_size": 5,"resolution": "high"},
  "velocity_smooth": {"data": [0.0,0.0,0.0,5.5,5.2],"series_type": "distance","original_size": 5,"resolution": "high"},
  "distance":  {"data": [0.0,8.6,11.8,16.6,20.8],"series_type": "distance","original_size": 5,"resolution": "high"},
  "time": {"data": [0,1,2,3,4],"series_type": "distance","original_size": 5,"resolution": "high"}
}

那么我可以定义下面三个classes

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Value;
import java.util.List;

@Getter
@NoArgsConstructor
public class Holder {
  DoubleData altitude;
  CoordinateData latlng;
  @JsonProperty("velocity_smooth") DoubleData velocitySmooth;
  DoubleData distance;
  DoubleData time;
}

@Getter
@NoArgsConstructor
public class DoubleData {
  List<Double> data;
  @JsonProperty("series_type")   String seriesType;
  @JsonProperty("original_size") Integer originalSize;
  String resolution;
}

@Getter
@NoArgsConstructor
public class CoordinateData {
  List<List<Double>> data;
  @JsonProperty("series_type")   String seriesType;
  @JsonProperty("original_size") Integer originalSize;
  String resolution;
}

然后使用

objectMapper.readValue(jsonString, Holder.class);

读取该对象。但是,由于从 Strava 接收到的数据是一个数组而不是一个对象,所以我失败了。我读过 Baeldung's article on how to unmarshal to collections/arrays 但假设 array/collection 中的所有 class 都是相同的。

我想定义一个接口,该接口将由可以在数组中找到的两个 classes 扩展,然后使用该机制:

public interface Data {
}

@Getter
@NoArgsConstructor
public class DoubleData implements Data {
  String type;
  List<Double> data;
  @JsonProperty("series_type")   String seriesType;
  @JsonProperty("original_size") Integer originalSize;
  String resolution;
}

@Getter
@NoArgsConstructor
public class CoordinateData implements Data {
  String type;
  List<List<Double>> data;
  @JsonProperty("series_type")   String seriesType;
  @JsonProperty("original_size") Integer originalSize;
  String resolution;
}

Data[] array = objectMapper.readValue(jsonString, Data[].class);

但这不起作用,因为我需要找到一些方法让它找出何时使用 DoubleData class 以及何时使用 CoordinateData class.

我敢肯定,我不是第一个尝试在 Java 中使用 Strava 数据的人。这能做到吗?

如果可能的话,你一定要用他们的客户端。 Strava API v3 展示了如何将此 API 与他们的模型一起使用的许多示例。

如果您想实现自己的模型,您应该考虑继承和 com.fasterxml.jackson.annotation.JsonTypeInfocom.fasterxml.jackson.annotation.JsonSubTypes 注释。此外,类型为 latlngJSON Object 包含以 array 形式在 JSON 中表示的对象列表。我们可以使用 com.fasterxml.jackson.annotation.JsonFormat 注释来处理这个问题。所有一起给出:

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.StringJoiner;

public class StravaApp {
    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.readValue(jsonFile, new TypeReference<List<Data>>() {}).forEach(System.out::println);
    }
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        visible = true,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(name = "altitude", value = DoubleData.class),
        @JsonSubTypes.Type(name = "latlng", value = CoordinateData.class),
        @JsonSubTypes.Type(name = "velocity_smooth", value = DoubleData.class),
        @JsonSubTypes.Type(name = "distance", value = DoubleData.class),
        @JsonSubTypes.Type(name = "time", value = DoubleData.class)
})
abstract class Data<T> {

    private String type;

    @JsonProperty("series_type")
    private String seriesType;

    @JsonProperty("original_size")
    private Integer originalSize;

    private String resolution;

    private List<T> data;

    // getters, setters, toString
}


class DoubleData extends Data<Double> {

}

class CoordinateData extends Data<Coordinates> {

}

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
class Coordinates {
    private double lat;
    private double lng;

        // getters, setters, toString
}

以上代码打印:

Data[type='altitude', seriesType='distance', originalSize=5, resolution='high', data=[519.1, 519.3, 519.3, 519.4, 519.5]]
Data[type='latlng', seriesType='distance', originalSize=5, resolution='high', data=[Coordinates[lat=46.01234, lng=6.01234], Coordinates[lat=46.11234, lng=6.11234], Coordinates[lat=46.21234, lng=6.21234], Coordinates[lat=46.31234, lng=6.31234], Coordinates[lat=46.41234, lng=6.41234]]]
Data[type='velocity_smooth', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 0.0, 0.0, 5.5, 5.2]]
Data[type='distance', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 8.6, 11.8, 16.6, 20.8]]
Data[type='time', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 1.0, 2.0, 3.0, 4.0]]

您还应该看看 Google Dev Group 并参考此解决方案。