gpt4 book ai didi

java - 使用 StdDeserializer Jackson 2.5 处理多态

转载 作者:行者123 更新时间:2023-11-30 08:15:56 24 4
gpt4 key购买 nike

我有三个类,它们继承自父类(super 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 输入转换为此类之一。我正在尝试使用 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);
}
}

我的问题是,当我确定映射具体类的正确类型时,映射器再次调用自定义 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 被包装在其他类 (ServiceData) 中

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

}

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

请注意,如果您有一个 Collection 并且使用 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
}
}

它将处理从“名称”到子类型的解析,将“数据”的内容绑定(bind)为JsonNode(或者,如果您愿意,可以使用Map对象或任何匹配的类型)。

关于java - 使用 StdDeserializer Jackson 2.5 处理多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com