如果我有这样的类结构:
public abstract class Parent {
private Long id;
...
}
public class SubClassA extends Parent {
private String stringA;
private Integer intA;
...
}
public class SubClassB extends Parent {
private String stringB;
private Integer intB;
...
}
是否有另一种方法来反序化不同的@JsonTypeInfo
?在我的父类上使用此批注:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "objectType")
我宁愿不必强迫我的API客户端包含"objectType": "SubClassA"
来反序列化Parent
子类。
杰克逊是否提供了一种注释子类并通过唯一属性将其与其他子类区分开来的方式,而不是使用@JsonTypeInfo
?在上面的例子中,这将是“如果一个JSON对象将"stringA": ...
反序列化为SubClassA
,如果它有"stringB": ...
将其反序列化为SubClassB
”。
这感觉像是应该使用@JsonTypeInfo
和@JsonSubTypes
,但是我已经选择了文档,并且没有任何可以提供的属性看起来与您所描述的相匹配。
您可以编写一个自定义反序列化器,它以非标准方式使用@JsonSubTypes
'“name”和“value”属性来完成您想要的任务。反序列化器和@JsonSubTypes
将在您的基类上提供,反序列化器将使用“name”值来检查属性是否存在,如果存在,则将JSON反序列化为“value”属性中提供的类。你的课程看起来像这样:
@JsonDeserialize(using = PropertyPresentDeserializer.class)
@JsonSubTypes({
@Type(name = "stringA", value = SubClassA.class),
@Type(name = "stringB", value = SubClassB.class)
})
public abstract class Parent {
private Long id;
...
}
public class SubClassA extends Parent {
private String stringA;
private Integer intA;
...
}
public class SubClassB extends Parent {
private String stringB;
private Integer intB;
...
}
这是我提出的一个解决方案,它扩展了Erik Gillespie的一些。它完全符合您的要求并且对我有用。
使用Jackson 2.9
@JsonDeserialize(using = CustomDeserializer.class)
public abstract class BaseClass {
private String commonProp;
}
// Important to override the base class' usage of CustomDeserializer which produces an infinite loop
@JsonDeserialize(using = JsonDeserializer.None.class)
public class ClassA extends BaseClass {
private String classAProp;
}
@JsonDeserialize(using = JsonDeserializer.None.class)
public class ClassB extends BaseClass {
private String classBProp;
}
public class CustomDeserializer extends StdDeserializer<BaseClass> {
protected CustomDeserializer() {
super(BaseClass.class);
}
@Override
public BaseClass deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
TreeNode node = p.readValueAsTree();
// Select the concrete class based on the existence of a property
if (node.get("classAProp") != null) {
return p.getCodec().treeToValue(node, ClassA.class);
}
return p.getCodec().treeToValue(node, ClassB.class);
}
}
// Example usage
String json = ...
ObjectMapper mapper = ...
BaseClass instance = mapper.readValue(json, BaseClass.class);
如果你想变得更加漂亮,你可以扩展CustomDeserializer
以包含一个Map<String, Class<?>>
,它映射一个属性名称,当存在时,映射到特定的类。这种方法在这个article中提出。
顺便说一句,这里有一个github问题:https://github.com/FasterXML/jackson-databind/issues/1627
没有。这个功能已经被要求了 - 它可以被称为“类型推断”或“隐含类型” - 但没有人提出一个可行的一般性建议,说明它应该如何工作。很容易想到支持特定案例的特定解决方案的方法,但找出一般解决方案更加困难。
我的应用程序要求我保留旧结构,所以我找到了一种方法来支持多态而不改变数据。这是我做的:
@Override public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode jsonNode = p.readValueAsTree();
Iterator<Map.Entry<String, JsonNode>> ite = jsonNode.fields();
boolean isSubclass = false;
while (ite.hasNext()) {
Map.Entry<String, JsonNode> entry = ite.next();
// **Check if it contains field name unique to subclass**
if (entry.getKey().equalsIgnoreCase("Field-Unique-to-Subclass")) {
isSubclass = true;
break;
}
}
if (isSubclass) {
return mapper.treeToValue(jsonNode, SubClass.class);
} else {
// process other classes
}
}
正如其他人所指出的那样,对how it should work so it hasn't been implemented没有达成共识。
如果您有类Foo,Bar和他们的父FooBar解决方案,当您有JSON时,它们似乎非常明显:
{
"foo":<value>
}
要么
{
"bar":<value>
}
但是当你得到时会发生什么事情没有共同的答案
{
"foo":<value>,
"bar":<value>
}
乍一看,最后一个例子似乎是400 Bad Request的一个明显案例,但在实践中有许多不同的方法:
我目前的解决方案适用于大多数情况并尝试尽可能多地利用现有的Jackson基础架构(每个层次结构只需要1个解串器):
public class PresentPropertyPolymorphicDeserializer<T> extends StdDeserializer<T> {
private final Map<String, Class<?>> propertyNameToType;
public PresentPropertyPolymorphicDeserializer(Class<T> vc) {
super(vc);
this.propertyNameToType = Arrays.stream(vc.getAnnotation(JsonSubTypes.class).value())
.collect(Collectors.toMap(Type::name, Type::value,
(a, b) -> a, LinkedHashMap::new)); // LinkedHashMap to support precedence case by definition order
}
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectMapper objectMapper = (ObjectMapper) p.getCodec();
ObjectNode object = objectMapper.readTree(p);
for (String propertyName : propertyNameToType.keySet()) {
if (object.has(propertyName)) {
return deserialize(objectMapper, propertyName, object);
}
}
throw new IllegalArgumentException("could not infer to which class to deserialize " + object);
}
@SuppressWarnings("unchecked")
private T deserialize(ObjectMapper objectMapper,
String propertyName,
ObjectNode object) throws IOException {
return (T) objectMapper.treeToValue(object, propertyNameToType.get(propertyName));
}
}
用法示例:
@JsonSubTypes({
@JsonSubTypes.Type(value = Foo.class, name = "foo"),
@JsonSubTypes.Type(value = Bar.class, name = "bar"),
})
interface FooBar {
}
@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Foo implements FooBar {
private final String foo;
}
@AllArgsConstructor(onConstructor_ = @JsonCreator)
@Value
static class Bar implements FooBar {
private final String bar;
}
杰克逊配置
SimpleModule module = new SimpleModule();
module.addDeserializer(FooBar.class, new PresentPropertyPolymorphicDeserializer<>(FooBar.class));
objectMapper.registerModule(module);
或者如果您使用的是Spring Boot:
@JsonComponent
public class FooBarDeserializer extends PresentPropertyPolymorphicDeserializer<FooBar> {
public FooBarDeserializer() {
super(FooBar.class);
}
}
测试:
@Test
void shouldDeserializeFoo() throws IOException {
// given
var json = "{\"foo\":\"foo\"}";
// when
var actual = objectMapper.readValue(json, FooBar.class);
// then
then(actual).isEqualTo(new Foo("foo"));
}
@Test
void shouldDeserializeBar() throws IOException {
// given
var json = "{\"bar\":\"bar\"}";
// when
var actual = objectMapper.readValue(json, FooBar.class);
// then
then(actual).isEqualTo(new Bar("bar"));
}
@Test
void shouldDeserializeUsingAnnotationDefinitionPrecedenceOrder() throws IOException {
// given
var json = "{\"bar\":\"\", \"foo\": \"foo\"}";
// when
var actual = objectMapper.readValue(json, FooBar.class);
// then
then(actual).isEqualTo(new Foo("foo"));
}