java - 什么@JsonTypeInfo.ID选择属性= "type.id"反序列化,JsonTypeInfo.Id.CUSTOM?

标签 java json polymorphism jackson

所以我的 JSON 看起来像这样:

{
    "ActivityDisplayModel" : {
        "name" : "lunch with friends",
        "startTime" : "12:00:00",
        "type" : {
            "id" : "MEAL",
            "description" : "Meal"
        },
        "complete" : false
    }
}

我正在尝试找到让 @JsonTypeInfo 不再因为在 type 对象中包含类型参数而生我的气。当字段 type 是一个 String 而不是一个对象本身时,我已经开始工作了,但是为了以后的处理,我需要它作为一个对象。我知道以下内容不起作用,我猜有一种方法可以使用 JsonTypeInfo.Id.CUSTOM ,但是在互联网上翻遍之后,没有出现 JSON 的完整示例。另外,如果 objectMapper 设置可以做到这一点,我会洗耳恭听。

/** 
 * My ActivityDisplayModel Abstract Class
 */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type.id")
@JsonSubTypes({
@JsonSubTypes.Type(value = MealDisplayModel.class, name = "MEAL"),
@JsonSubTypes.Type(value = EntertainmentDisplayModel.class, name = "ENTERTAINMENT")
})
public abstract class ActivityDisplayModel { 
    ...

以上基本上是我想做的,但当然我得到一个异常(exception):

Could not read JSON: Could not resolve type id '{' into a subtype of  [simple type, class ... .ActivityDisplayModel]

这么简单的问题,只看JSON更深一层,谁能想到会这么麻烦?

最佳答案

我知道从最初的问题到现在已经 3 年了,但是点嵌套属性仍然不受支持,也许这会对某人有所帮助。我最终创建了一个 NestedTypeResolver 类,因此我们可以按预期使用点语法。只需将 @JsonTypeResolver(NestedTypeResolver.class) 添加到任何具有嵌套鉴别器的类中,发布者的原始尝试就会起作用:

/** 
 * My ActivityDisplayModel Abstract Class
 */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type.id")
  @JsonSubTypes({
  @JsonSubTypes.Type(value = MealDisplayModel.class, name = "MEAL"),
  @JsonSubTypes.Type(value = EntertainmentDisplayModel.class, name = "ENTERTAINMENT")
})
@JsonTypeResolver(NestedTypeResolver.class)
public abstract class ActivityDisplayModel {

嵌套类型解析器:

/**
 * Allows using nested "dot" dyntax for type discriminators. To use, annotate class with @JsonTypeResolver(NestedTypeResolver.class)
 */
public class NestedTypeResolver extends StdTypeResolverBuilder {
    @Override
    public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, JavaType baseType,
            Collection<NamedType> subtypes) {
            //Copied this code from parent class, StdTypeResolverBuilder with same method name
            TypeIdResolver idRes = idResolver(config, baseType, subtypes, false, true);
            return new NestedTypeDeserializer(baseType, idRes, _typeProperty, _typeIdVisible,
                null, _includeAs);
    }
}

所有繁重的工作都在这里完成,NestedTypeDeserializer:

/**
 * Heavy work to support {@link NestedTypeResolver}
 */
public class NestedTypeDeserializer extends AsPropertyTypeDeserializer {

    private static final Logger LOGGER = LoggerFactory.getLogger(NestedTypeDeserializer.class);

    public NestedTypeDeserializer(JavaType bt,
            TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible,
            JavaType defaultImpl) {
        super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
    }

    public NestedTypeDeserializer(JavaType bt, TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible,
            JavaType defaultImpl, JsonTypeInfo.As inclusion) {
        super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, inclusion);
    }

    public NestedTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) {
        super(src, property);
    }

    @Override
    public TypeDeserializer forProperty(BeanProperty prop) {
        return (prop == _property) ? this : new NestedTypeDeserializer(this, prop);
    }

    @Override
    public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode originalNode = p.readValueAsTree();
        JsonNode node = originalNode;
        //_typePropertyName is the dot separated value of "property" in @JsonTypeInfo
        LOGGER.debug("Searching for type discriminator [{}]...", _typePropertyName);
        for (String property : _typePropertyName.split("\\.")) { //traverse down any nested properties
            JsonNode nestedProp = node.get(property);
            if (nestedProp == null) {
                ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
                        "missing property '" + _typePropertyName + "' that is to contain type id  (for class "
                                + baseTypeName() + ")");
                return null;
            }
            node = nestedProp;
        }
        LOGGER.debug("Found [{}] with value [{}]", _typePropertyName, node.asText());
        JsonDeserializer<Object> deser = _findDeserializer(ctxt, "" + node.asText());
        //Since JsonParser is a forward-only operation and finding the "type" discriminator advanced the pointer, we need to reset it
        //Got clues from https://www.dilipkumarg.com/dynamic-polymorphic-type-handling-jackson/
        JsonParser jsonParser = new TreeTraversingParser(originalNode, p.getCodec());
        if (jsonParser.getCurrentToken() == null) {
            jsonParser.nextToken();
        }
        return deser.deserialize(jsonParser, ctxt);
    }
}

免责声明:我们已经在 J​​ackson 2.8.10 上使用了一个月,没有遇到任何问题,但我们必须深入研究 Jackson 源代码杂草才能完成它,所以 YMMV。 希望 Jackson 有一天会允许这种开箱即用的方式,这样我们就不需要这些解决方法了。

关于java - 什么@JsonTypeInfo.ID选择属性= "type.id"反序列化,JsonTypeInfo.Id.CUSTOM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671806/

相关文章:

java - 尝试将 spring boot war 部署到 tomcat8 时 Unresolved 循环引用?

javascript - 按两个不同的值过滤 JSON

c# - 这个设计使用动态好吗?

Java - 使用paintComponent和多态性绘制形状

java - 带有 Java 的 Swig C++ 在多态回调函数上丢失类型

java - NetBeans 不会更改我的背景颜色

java - 按日期从数据库中删除行

php - 如何从 AJAX 以 JSON 形式返回数组并读取结果

json - 将所有列存储在一个 JSON 列中的缺点

java - Websphere REST 上传 - 不将上传的文件加载到内存中