使用 Jackson 的 ConvertValue 从自定义类型转换为 Map 时删除一些键

发布于 2025-01-11 23:48:19 字数 1584 浏览 0 评论 0原文

我有一个相当简单的 Java 对象,我想使用 Jackson 的 convertValue(fromValue, toValueTypeRef) 将其转换为 Map — 这是相当简单的。

同时,有一些字段/成员(例如:createdOnmodifiedOn)我想从生成的Map中删除,但是我无法在所有(反)序列化例程中删除它们。我想知道是否有办法告诉杰克逊这样做,而不是“手动”删除它们。我通常看到 @JsonView 用于此类目的,但我认为它不适用于 convertValue — 或者我仍然无法正确配置它。

这就是我进行转换的方式:

@NoArgsConstructor
public final class CustomTypeMapper {
  private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<>() { };

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); // ...with some customizations

  public Map<String, Object> convertFrom(final CustomType input) {
    // OBJECT_MAPPER.writerWithView(Views.Public.class);
    return OBJECT_MAPPER.convertValue(input, MAP_TYPE_REFERENCE);
  }
}

CustomType 大致如下所示:

@Getter
@Builder
@Jacksonized
public final class CustomType {
  @JsonProperty("id")
  private String id;

  @JsonProperty("user")
  private User user;

  ...

  @Builder.Default
  @JsonProperty("created_on") // TODO: Exclude this field/member some times
  @JsonView(Views.Internal.class)
  private final Instant createdOn = Instant.now();

  @Builder.Default
  @JsonView(Views.Internal.class)
  @JsonProperty("modified_on") // TODO: Exclude this field/member some times
  private Instant modifiedOn = Instant.now();
}

I have a rather simple Java object that I would like to convert it into a Map<String, Object> using Jackson's convertValue(fromValue, toValueTypeRef) — this is rather trivial.

At the same time, there are some fields/members (e.g.: createdOn and modifiedOn) I would like to remove from the resultant Map, but I can't remove them in all (de)serialization routines. I was wondering if there is a way to tell Jackson to do that, rather than removing them "by hand". I've usually seen @JsonView used for such purposes, but I think it doesn't work with convertValue — or I'm still unable to configure it correctly.

This is how I'm doing the conversion:

@NoArgsConstructor
public final class CustomTypeMapper {
  private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<>() { };

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); // ...with some customizations

  public Map<String, Object> convertFrom(final CustomType input) {
    // OBJECT_MAPPER.writerWithView(Views.Public.class);
    return OBJECT_MAPPER.convertValue(input, MAP_TYPE_REFERENCE);
  }
}

CustomType looks roughly like this:

@Getter
@Builder
@Jacksonized
public final class CustomType {
  @JsonProperty("id")
  private String id;

  @JsonProperty("user")
  private User user;

  ...

  @Builder.Default
  @JsonProperty("created_on") // TODO: Exclude this field/member some times
  @JsonView(Views.Internal.class)
  private final Instant createdOn = Instant.now();

  @Builder.Default
  @JsonView(Views.Internal.class)
  @JsonProperty("modified_on") // TODO: Exclude this field/member some times
  private Instant modifiedOn = Instant.now();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文