使用 StdDeserializer Jackson 2.5 处理多态

Handle Polymorphic with StdDeserializer Jackson 2.5

我有三个 class 继承自超级 class (SensorData)

@JsonDeserialize(using = SensorDataDeserializer.class)
public abstract class SensorData {

}

public class HumiditySensorData extends SensorData {
}

public class LuminositySensorData extends SensorData {
}

public class TemperatureSensorData extends SensorData {
}

我想根据参数将 json 输入转换为其中一个 classes。我正在尝试使用 Jackson StdDeserializer 并创建一个自定义反序列化器

@Component
public class SensorDataDeserializer extends StdDeserializer<SensorData> {

    private static final long serialVersionUID = 3625068688939160875L;

    @Autowired
    private SensorManager sensorManager;

    private static final String discriminator = "name";

    public SensorDataDeserializer() {
        super(SensorData.class);
        SpringBeanProvider.getInstance().autowireBean(this);
    }

    @Override
    public SensorData deserialize(JsonParser parser,
            DeserializationContext context) throws IOException,
            JsonProcessingException {
        ObjectMapper mapper = (ObjectMapper) parser.getCodec();
        ObjectNode root = (ObjectNode) mapper.readTree(parser);
        ObjectNode sensor = (ObjectNode) root.get("data");
        String type = root.get(discriminator).asText();
        Class<? extends SensorData> clazz = this.sensorManager
                .getCachedSensorsMap().get(type).sensorDataClass();
        if (clazz == null) {
            // TODO should throw exception
            return null;
        }
        return mapper.readValue(sensor.traverse(), clazz);
    }
}

我的问题是,当我确定正确的类型来映射具体 class 时,映射器再次调用自定义 StdDeserializer。所以我需要一个方法 当我有正确的类型时打破循环。堆栈跟踪是下一个

java.lang.NullPointerException
at com.hp.psiot.mapping.SensorDataDeserializer.deserialize(SensorDataDeserializer.java:38)
at com.hp.psiot.mapping.SensorDataDeserializer.deserialize(SensorDataDeserializer.java:1)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3532)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1868)
at com.hp.psiot.mapping.SensorDataDeserializer.deserialize(SensorDataDeserializer.java:47)
at com.hp.psiot.mapping.SensorDataDeserializer.deserialize(SensorDataDeserializer.java:1)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3560)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2660)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:200)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters    (AbstractMessageConverterMethodArgumentResolver.java:138)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:184)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:105)

输入示例

{
    "name":"temperature",
    "data": {
        "value":20
    }
}

我只包含堆栈跟踪以表明映射器正在再次调用反序列化器。 nullPointerException 的原因是,当第二次调用 ObjectMapper 时,输入是

"value":20

因此,抛出异常是因为我们没有确定类型的信息并且它不检查输入是否正确

如果可能,我想避免使用 JsonSubTypes 和 JsonTypeInfo。

提前致谢!


部分解决方案

在我的例子中,SensorData 包裹在其他 class (ServiceData)

class ServiceData {
    @JsonDeserialize(using = SensorDataDeserializer.class)
    List<SensorData> sensors;

}

因此,我摆脱了 SensorData class 中的 JsonDeserializer 并将其放在避免循环的字段中。该解决方案不是最好的,但就我而言,它对我有帮助。但是如果 class 没有包裹在另一个中,我们仍然会遇到同样的问题。

请注意,如果您有一个集合并使用 JsonDeserialize 注释该字段,则您必须处理所有集合。这是修改 就我而言

 @Component
 public class SensorDataDeserializer extends StdDeserializer<List<SensorData>> {

      private static final long serialVersionUID = 3625068688939160875L;

      @Autowired
      private SensorManager sensorManager;

      private static final String discriminator = "name";

      public SensorDataDeserializer() {
           super(SensorData.class);
            SpringBeanProvider.getInstance().autowireBean(this);
      }

      @Override
      public List<SensorData> deserialize(JsonParser parser,
                DeserializationContext context) throws IOException,
                JsonProcessingException {
           try {
                ObjectMapper mapper = (ObjectMapper) parser.getCodec();
                ArrayNode root = (ArrayNode) mapper.readTree(parser);
                int size = root.size();
                List<SensorData> sensors = new ArrayList<SensorData>();
                for (int i = 0; i < size; ++i) {
                     ObjectNode sensorHead = (ObjectNode) root.get(i);
                     ObjectNode sensorData = (ObjectNode) sensorHead.get("data");
                     String tag = sensorHead.get(discriminator).asText();
                     Class<? extends SensorData> clazz = this.sensorManager
                               .getCachedSensorsMap().get(tag).sensorDataClass();
                     if (clazz == null) {
                          throw new InvalidJson("unbound sensor");
                     }
                     SensorData parsed = mapper.readValue(sensorData.traverse(),
                               clazz);
                     if (parsed == null) {
                          throw new InvalidJson("unbound sensor");
                     }
                     sensors.add(parsed);
                }
                return sensors;
           } catch (Throwable e) {
                throw new InvalidJson("invalid data");
           }

      }
 }

希望对大家有所帮助:)

为什么不直接使用 @JsonTypeInfo?多态处理是它的特定用例。

在这种情况下,您可能希望使用如下内容:

@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="name")
@JsonSubTypes({ HumiditySensorData.class, ... }) // or register via mapper
public abstract class SensorData { ... }

@JsonTypeName("temperature")
public class TemperaratureSensorData extends SensorData {
   public TemperaratureSensorData(@JsonProperty("data") JsonNode data) {
     // extract pieces out
  }
}

将处理从 'name' 到子类型的解析,将 'data' 的内容绑定为 JsonNode(或者,如果您愿意,可以使用 MapObject 或任何匹配的类型)。