反序列化 json 使用原始值构造函数在杰克逊中没有自定义 JsonDeserializer

Deserialize json using raw value constructor without custom JsonDeserializer in jackson

我有一个 class,我想通过使用一种接受完整 json 对象作为字符串的方法调用构造函数来反序列化:

public class MyDataObject {

    @IwantObjectMapperToCallThisWhenDeserializing
    public MyDataObject(String json) {
      // custom logic
    }
}

是否可以仅使用简单的注释来实现,而不影响我自己的 JsonDeserializer

我没有找到直接的方法,但这里有一个解决方法:

public class MyDataObject {

    @JsonCreator(mode = Mode.DELEGATING)
    // passed object is a Map, but as long as the type is serializable below, it can by any type
    public MyDataObject(Object map) {
        String json;
        try {
            json = new ObjectMapper().writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Error deserializing MyDataObject", e);
        }
        // custom logic
    }
}