对于属性选择什么 @JsonTypeInfo.ID = “type.id” 进行反序列化,JsonTypeInfo.Id.CUSTOM?

2022-09-02 04:04:51

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

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

我试图找到一种方法,不因为我在对象中包含type参数而生气。我以前在字段是字符串而不是对象本身时已经实现了这一点,但是为了以后的处理,我需要它作为一个对象。我知道以下内容不起作用,我猜有一种方法可以使用JsonTypeInfo.Id.CUSTOM,但是在互联网上查看了所有内容之后,还没有出现JSON的完整示例。另外,如果使用objectMapper设置可以做到这一点,那么我全都听见了。@JsonTypeInfotypetype

/** 
 * 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 { 
    ...

以上基本上是我想做的,但当然我得到一个例外:

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

对于这样一个简单的问题,只是在JSON中深入一层,谁会想到它会有这么大的麻烦?


答案 1

我知道自最初的问题以来已经过去了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 {

NestedTypeResolver:

/**
 * 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);
    }
}

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


答案 2

我不确定你是否可以通过指定内部属性来做到这一点:。在我看来,您应该将JSON更改为更简单的版本。如果您无法强制 JSON 供应商更改 JSON 架构,则必须手动执行此操作。假设您的 JSON 如下所示:type.id

{
    "activityDisplayModel": {
        "name": "lunch with friends",
        "type": {
            "id": "MEAL",
            "description": "Meal"
        },
        "complete": false
    }
}

下面的 POJO 类适合上面的 JSON:

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

    protected String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

class MealDisplayModel extends ActivityDisplayModel {

    private boolean complete;

    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    @Override
    public String toString() {
        return "MealDisplayModel [complete=" + complete + ", toString()=" + super.toString() + "]";
    }
}

@JsonIgnoreProperties("complete")
class EntertainmentDisplayModel extends ActivityDisplayModel {

    @Override
    public String toString() {
        return "EntertainmentDisplayModel [toString()=" + super.toString() + "]";
    }
}

class Root {

    private ActivityDisplayModel activityDisplayModel;

    public ActivityDisplayModel getActivityDisplayModel() {
        return activityDisplayModel;
    }

    public void setActivityDisplayModel(ActivityDisplayModel activityDisplayModel) {
        this.activityDisplayModel = activityDisplayModel;
    }

    @Override
    public String toString() {
        return activityDisplayModel.toString();
    }
}

下面的脚本显示了如何解析 JSON 之上的内容:

ObjectMapper mapper = new ObjectMapper();
// Updated JSON in memory
ObjectNode rootNode = (ObjectNode)mapper.readTree(json);
ObjectNode activityDisplayModelNode = (ObjectNode)rootNode.path("activityDisplayModel");
JsonNode typeNode = activityDisplayModelNode.path("type");
activityDisplayModelNode.set("type", typeNode.path("id"));

System.out.println("Result: " + mapper.convertValue(rootNode, Root.class));

以上脚本打印:

Result: MealDisplayModel [complete=false, toString()=lunch with friends]

另请参阅:

  1. 杰克逊树模型示例
  2. 将 Java 对象转换为 Jackson 中的 JsonNode