什么@JsonTypeInfo.ID为property选择=" type.id"用于反序列化,JsonTypeInfo.Id.CUSTOM?

时间:2014-09-04 17:41:23

标签: java json polymorphism jackson

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

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

我试图找到让@JsonTypeInfo因为在type对象中包含type参数而对我不感到生气的方法。在字段type是字符串而不是对象本身之前,我已经完成了这项工作,但是为了以后的处理,我需要它作为对象。我知道以下内容并不起作用,而且我猜测这是一种使用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 { 
    ...

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

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

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

2 个答案:

答案 0 :(得分:4)

我知道自原始问题已经过了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。 希望杰克逊有一天会允许这种开箱即用,所以我们不需要这些解决方法。

答案 1 :(得分:3)

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

{
    "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. Jackson Tree Model Example
  2. Convert Java Object to JsonNode in Jackson