2

给定

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实例。

4

2 回答 2

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 的更简单方法。

于 2018-03-12T20:35:19.113 回答
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);
于 2018-03-13T15:34:37.073 回答