使用 Jackson 的 ConvertValue 从自定义类型转换为 Map 时删除一些键
我有一个相当简单的 Java 对象,我想使用 Jackson 的 convertValue(fromValue, toValueTypeRef)
将其转换为 Map
— 这是相当简单的。
同时,有一些字段/成员(例如:createdOn
和modifiedOn
)我想从生成的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论