Jackson JsonDeserializer将字段的反序列化委托回该字段类型的默认反序列化器

时间:2018-03-12 17:08:01

标签: java jackson jackson2 jackson-databind

鉴于

public class ConstraintMatch {
    protected String constraintName;
    protected Score score;
    ...
}

我在杰克逊有以下序列化器:

public class ConstraintMatchJacksonJsonSerializer extends JsonSerializer<ConstraintMatch> {

    @Override
    public void serialize(ConstraintMatch constraintMatch, JsonGenerator generator, SerializerProvider serializers)
            throws IOException {
        generator.writeStartObject();
        generator.writeStringField("constraintName", constraintMatch.getConstraintName());
        generator.writeFieldName("score");
        // Delegate to serialization to the default Score serializer
        serializers.findValueSerializer(Score.class)
                 .serialize(constraintMatch.getScore(), generator, serializers);
        generator.writeEndObject();
    }

}

如何编写也委派给默认反序列化器的反序列化器?

public class ConstraintMatchJacksonJsonDeserializer extends JsonDeserializer<ConstraintMatch> {

    @Override
    public ConstraintMatch deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode tree = parser.readValueAsTree();

        String constraintName = tree.get("constraintName").asText();

        JsonNode scoreNode = tree.get("score");
        Score score = ...; // How do I delegate to the default deserializer?
        return new ConstraintMatch(constraintName, score);
    }

}

我查看了findContextualValueDeserializer()等,但我无法创建BeanProperty个实例。

2 个答案:

答案 0 :(得分:1)

在类似的情况下,我实际上发现有两个问题需要解决。首先,正如您所说,需要委托回正常的解串器。但我遇到的另一个问题是如何将JsonNode(下面的TreeNode)提供给下一个反序列化(JsonParser,...)。

以下是该情况的工作样本,我想做一个前瞻以找出子类。

希望这里的节点是你的scoreNode。听起来像objectClass只是Score.class。

@Override
public T deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) parser.getCodec();
    TreeNode node = parser.readValueAsTree();

    // Select the subclass to deserialize as
    Class<? extends T> objectClass = deduceClass(node);

    // This based on ObjectMapper._convert()
    // - the problem here was the JsonParser (parser) had gone past the current node
    TokenBuffer buf = new TokenBuffer(mapper, false);
    SerializationConfig config = mapper.getSerializationConfig()
            .without(SerializationFeature.WRAP_ROOT_VALUE);
    DefaultSerializerProvider serializerProvider = ((DefaultSerializerProvider) mapper
            .getSerializerProvider()).createInstance(config,
                    mapper.getSerializerFactory());
    serializerProvider.serializeValue(buf, node);
    JsonParser nestedParser = buf.asParser();
    nestedParser.nextToken();
    JsonDeserializer<Object> deserializer = ctxt
            .findRootValueDeserializer(
                    mapper.getTypeFactory().constructType(objectClass));
    @SuppressWarnings("unchecked")
    T obj = (T) deserializer.deserialize(nestedParser, ctxt);

    return obj;

}

(以防万一,这是杰克逊2.7.9)

我很高兴听到一种从节点创建JsonParser的简单方法。

答案 1 :(得分:1)

序列化:

    constraintMatch.getConstraintPackage());
    generator.writeStringField("constraintName", constraintMatch.getConstraintName());

    generator.writeFieldName("score");
    // Delegate to PolymorphicScoreJacksonJsonSerializer
    JsonSerializer<Object> scoreSerializer = serializers.findValueSerializer(Score.class);
    scoreSerializer.serialize(constraintMatch.getScore(), generator, serializers);
    generator.writeEndObject();

可以通过以下方式反序列化:

    parser.nextToken();
    if (!"constraintName".equals(parser.getCurrentName())) {
        throw new IllegalStateException(...);
    }
    parser.nextToken();
    String constraintName = parser.getValueAsString();

    parser.nextToken();
    if (!"score".equals(parser.getCurrentName())) {
        throw new IllegalStateException(...);
    }
    parser.nextToken();
    JsonDeserializer<Object> scoreDeserializer = context.findNonContextualValueDeserializer(context.constructType(Score.class));
    Score score = (Score) scoreDeserializer.deserialize(parser, context);