自定义 Jackson 反序列化 - 获取 com.fasterxml.jackson.databind.exc.MismatchedInputException:绑定数据时意外的输入结束

Custom Jackson deserialization - getting com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected end-of-input when binding data

我正在尝试为类型感知的对象(下面代码中的 TypeAwareObject)编写自定义反序列化程序。我收到以下错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException:将数据绑定到 TestMain$Value

时出现意外的输入结束

仅供参考,我正在使用 lombok 来减少样板文件(用于自动生成 getter、setter 和构造函数)

public class TestMain {

    public static void main(String[] args) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();

        final String str = mapper
                .writeValueAsString(new TypeAwareObject(Value.class, new Value("test")));
        System.out.println(str);

        TypeAwareObject obj = mapper.readValue(str, TypeAwareObject.class);
        System.out.println(obj);
    }

    @lombok.Data
    public static class Value implements Serializable{
        private static final long serialVersionUID = 1L;
        private final String v;
    }

    @JsonDeserialize(using = TypeAwareObjectDeserializer.class)
    @lombok.Data
    @lombok.RequiredArgsConstructor
    public static class TypeAwareObject implements Serializable {
        private static final long serialVersionUID = 1L;
        private final Class<? extends Serializable> type;
        private final Serializable value;
    }

    public static class TypeAwareObjectDeserializer extends StdDeserializer<TypeAwareObject> {
        private static final long serialVersionUID = 1L;

        public TypeAwareObjectDeserializer() {
            super(TypeAwareObject.class);
        }

        @Override
        public TypeAwareObject deserialize(JsonParser jsonParser, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {

            final ObjectCodec objectCodec = jsonParser.getCodec();

            final JsonNode jsonNode = objectCodec.readTree(jsonParser);
            final String typeClassName = jsonNode.get("type").asText();
            try {

                @SuppressWarnings("unchecked")
                final Class<? extends Serializable> type = (Class<? extends Serializable>) Class.forName(typeClassName);

                final JsonNode valueNode = jsonNode.get("value");   
                final Serializable value = ctxt.readValue(valueNode.traverse(), type);

                return new TypeAwareObject(type, value);

            } catch (ClassNotFoundException typeNotFound) {             
                throw new JsonParseException(jsonParser, //
                        "Error in deserializing TypeAwareObject", //
                        typeNotFound);
            }
        }
    }

}

我在下面收到以下错误。我确定我遗漏了一些小东西,但不能完全把手指放在上面..我做错了什么? :( 请帮忙!!!

{"type":"TestMain$Value","value":{"v":"test"}}
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected end-of-input when binding data into `TestMain$Value`
 at [Source: (String)"{"type":"TestMain$Value","value":{"v":"test"}}"; line: 1, column: 46]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:198)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
    at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:760)
    at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:747)
    at TestMain$TypeAwareObjectDeserializer.deserialize(TestMain.java:67)
    at TestMain$TypeAwareObjectDeserializer.deserialize(TestMain.java:1)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at TestMain.main(TestMain.java:28)

所以我不是 JsonParsing 方面的专家,但我发现 readValue 引用的 currentToken 被初始化为 null。

一些测试引导我创建解析器并告诉它立即转到下一个令牌(可能有可能通过加载第一个令牌来初始化解析器,但我不知道)。

JsonParser parser = valueNode2.traverse();
parser.nextToken();
final Serializable value = ctxt.readValue(parser, type);