我有以下类要将JSON字符串反序列化为使用Jackson。
PushNotificationMessage.java
public class PushNotificationMessage {
@JsonProperty("device_info")
private DeviceInfo deviceInfo;
private String content;
//getters & setters
}
DeviceInfo.java
public class DeviceInfo {
@JsonProperty(value = "device_type")
private String deviceType;
//getters & setters
}
IOSDeviceInfo.java
public class IOSDeviceInfo extends DeviceInfo {
@JsonProperty(value = "device_id")
private String deviceId;
private String arn;
@JsonProperty(value = "user_data")
private String userData;
//getters & setters
}
WebDeviceInfo.java
public class WebDeviceInfo extends DeviceInfo {
private String endpoint;
private String key;
private String auth;
//getters & setters
}
我有以下要反序列化的JSON内容:
{
"device_info": {
"endpoint": "https://android.googleapis.com/gcm/send/blah",
"key": "blahkey",
"auth": "blahauth",
"device_type": "web"
},
"content": "Notification content"
}
我只是使用ObjectMapper
来尝试执行反序列化。
final ObjectMapper objectMapper = new ObjectMapper();
final PushNotificationMessage message = objectMapper.readValue(jsonString, PushNotifictionMessage.class);
当我这样做时,我得到:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段"端点" (com.blah.DeviceInfo类),未标记为可忽略(一个已知属性:" device_type"])
如何让杰克逊认识到需要将其映射到WebDeviceInfo
实例,而不是尝试将其映射到DeviceInfo
超类,而这些超类没有endpoint
场?
我尝试在不同的课程中使用@JsonTypeInfo
和@JsonSubTypes
注释,但我找不到如何使用它们的好例子。
编辑:我在@JsonDeserialize(using = DeviceInfoDeserializer.class)
课程中添加了DeviceInfo
注释,并创建了以下DeviceInfoDeserializer
。
public class DeviceInfoDeserializer extends JsonDeserializer<DeviceInfo> {
private static final String DEVICE_TYPE = "device_type";
private static final String WEB = "web";
private static final String IOS = "ios";
@Override
public DeviceInfo deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final ObjectMapper objectMapper = (ObjectMapper) jsonParser.getCodec();
final ObjectNode root = objectMapper.readTree(jsonParser);
if (root.has(DEVICE_TYPE)) {
final JsonNode jsonNode = root.get(DEVICE_TYPE);
if (jsonNode.asText().equalsIgnoreCase(WEB)) {
return objectMapper.readValue(root.toString(), WebDeviceInfo.class);
} else if (jsonNode.asText().equalsIgnoreCase(IOS)) {
return objectMapper.readValue(root.toString(), IOSDeviceInfo.class);
}
}
throw deserializationContext.mappingException("Failed to de-serialize device info, as device_type was not \"web\" or \"ios\"");
}
}
现在,在尝试反序列化PushNotificationMessage
JSON时出现了不同的错误:
java.lang.StackOverflowError: null
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:210)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:69)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3770)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2207)
at com.blah.serialization.DeviceInfoDeserializer.deserialize(DeviceInfoDeserializer.java:25)
at com.blah.serialization.DeviceInfoDeserializer.deserialize(DeviceInfoDeserializer.java:16)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
... (above trace repeated many times)
编辑:只需将@JsonDeserialize(as = WebDeviceInfo.class)
和@JsonDeserialize(as = IOSDeviceInfo.class)
添加到我的子类,现在它可以按预期工作。非常感谢@Luciano van der Veekens。
答案 0 :(得分:1)
Jackson并不知道多态,它只是尝试创建具体的DeviceInfo类的实例。
但是,您可以实现一个自定义反序列化器,它以编程方式解析设备信息JSON,并知道何时实例化其中一个子类,因为endpoint
等某些字段的唯一性。
@JsonDeserialize(using = DeviceInfoDeserializer.class)
public class DeviceInfo {
}
可在此处找到一个示例:http://sunilkumarpblog.blogspot.nl/2015/12/javajson-polymorphic-serialization-de.html