1

我正在尝试使用杰克逊对象映射器解析多态对象(它来自 Strava api)。该对象如下所示:

[
  {
    "type": "latlng",
    "data": [
      [ 38.603734, -122.864112 ],
      [ 38.608798, -122.867714 ],

      ... omitted for brevity ...

      [ 38.611205, -122.870848 ],
      [ 38.603579, -122.863891 ]
    ],
    "series_type": "distance",
    "original_size": 512,
    "resolution": "low"
  },
  {
    "type": "distance",
    "data": [
      0.0,
      1305.8,

      ... omitted for brevity ...

      128136.6,
      129444.1
    ],
    "series_type": "distance",
    "original_size": 512,
    "resolution": "low"
  }
]

因此,根据类型,字段数据中有不同的对象。在大多数情况下,它是一个浮点数组。在“latlng”的情况下,有一个 float[] 数组,所以它是一个 float[][](我认为)。我使用反序列化器创建了一个表示此数据结构的对象。它看起来像这样:

public class StravaStream {


    @JsonProperty("type")
    private String type;

    public String getType() {
        return type;
    }


    public static class StravaStream1D extends StravaStream {
        @JsonProperty("data")
        private float[] data;

        public StravaStream1D() {
        }

        public float[] getData() {
            return data;
        }


    }

    public static class StravaStream2D extends StravaStream {

        @JsonProperty("data")
        private float[][] data;

        public StravaStream2D() {
        }

        public float[][] getData() {
            return data;
        }

    }

    public StravaStream() {
    }


    public static class StravaStreamDeserializer extends StdDeserializer<StravaStream> {
        public StravaStreamDeserializer() {
            super(StravaStream.class);
        }

        @Override
        public StravaStream deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            Class<? extends StravaStream> variantStravaStream;
            ObjectMapper mapper = (ObjectMapper) jp.getCodec();
            ObjectNode root = (ObjectNode) mapper.readTree(jp);
            JsonNode type = root.get("type");
            System.out.println("type is "+type);
            if (type.textValue().equals("latlng")) {
                variantStravaStream = StravaStream2D.class;
            } else {
                variantStravaStream = StravaStream1D.class;
            }
                System.out.println("variant is "+variantStravaStream.getSimpleName());

            return mapper.readValue(jp, variantStravaStream);
        }
    }
}

当我只要求一维数据时,比如距离对象左右,效果很好。但是当我尝试解析“latlng”float[][] 时,杰克逊失败了。我确信该类型已被识别,请参阅附加的 system.out,它打印出使用了 StravaStream2D.class 变体。我得到的错误消息(以及额外的系统输出)是:

01-26 09:05:49.605 27165-27165/nl.jfvh.stravatest I/System.out: type is "latlng"
01-26 09:05:49.605 27165-27165/nl.jfvh.stravatest I/System.out: variant is StravaStream2D
01-26 09:05:49.620 27165-27165/nl.jfvh.stravatest W/System.err: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of float[] out of VALUE_NUMBER_FLOAT token
01-26 09:05:49.620 27165-27165/nl.jfvh.stravatest W/System.err:     at [Source: java.io.StringReader@e8550ec; line: 1, column: 40164] (through reference chain: java.util.ArrayList[0]->nl.jfvh.stravatest.client.model.StravaStream2D["data"]->[Ljava.lang.Object[][0])

我的数据模型错了吗?多态对象的解析对我来说很新,我遵循了一些教程,但问题似乎在于 float[][] 上的数据的简单映射......

4

1 回答 1

0

由于(根据您的说法)您是解析多态对象的新手,我建议使用注释而不是自定义反序列化器。它很简单:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(name = "latlng", value = StravaStream.StravaStream2D.class),
        @JsonSubTypes.Type(name = "distance", value = StravaStream.StravaStream1D.class)
})
public class StravaStream {

阅读JsonTypeInfoJsonSubTypes的文档

您正在尝试使用的自定义反序列化器的一个问题是,当您return mapper.readValue(jp, variantStravaStream);最后这样做时,它实际上会重新进入反序列化器,因为class StravaStream2D extends StravaStream杰克逊将再次使用自定义反序列化器。由于无限递归,我在尝试此操作时遇到堆栈溢出错误。

于 2018-01-29T12:54:31.580 回答