使用objectmapper解析多态对象

时间:2018-01-26 08:10:15

标签: java jackson objectmapper strava

我正在尝试使用jackson对象映射器解析多态对象(它来自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);
        }
    }
}

当我只询问一维数据时,如距离物体左右,它可以正常工作。但是当我试图解析&#34; latlng&#34; float [] [],杰克逊失败了。我确信该类型已被识别,请参阅additonal 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 [] []上的数据的简单映射......

1 个答案:

答案 0 :(得分: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因此Jackson将再次使用自定义反序列化程序。由于无限递归,我在尝试此时遇到了堆栈溢出错误。