如何在自定义反序列化器中使用一些Jackson Deserializer? [英] How to use some Jackson Deserializer in own custom Deserializer?
问题描述
我正在努力解决问题中提到的问题。
I am struggling with mentioned in the question issue.
我需要创建一些自定义反序列化器,它或多或少地从标准反序列化器转换类型(原因是 ZonedDateTime
正在为我的输入工作,但我不想将类型更改为 ZonedDateTime
,但保持 LocalDateTime
)。
I need to create some custom deserializer which is more or less type conversion from the standard deserializer (reason is that ZonedDateTime
is working for my input, but I don't want to change the type to ZonedDateTime
, but keep LocalDateTime
).
我想在我的反序列化器中做的事情是:
Bascially what I want to do in my deserializer is to:
- 使用
反序列化ZonedDateTime
反序列化器(我发现,实际上,它是自定义InstantDeserializer
) - 使用
.toLocalDateTime
并将其返回。
- Deserialize using
ZonedDateTime
deserializer (which I found, in reality, is customInstantDeserializer
) - Use
.toLocalDateTime
and return it.
我该如何使用它?
试图找到它,但我不能。
How can I use it? Was trying to find it but I can't.
推荐答案
如果您的输入代表 ZonedDateTime
并且您想将其转换为 LocalDateTime
,您可以执行以下操作。
If your input represents a ZonedDateTime
and you want to convert it to a LocalDateTime
, you can do the following.
我创建了一个带有 LocalDateTime
字段的示例类:
I've created a sample class with a LocalDateTime
field:
public class ZoneToLocalTest {
@JsonDeserialize(using = CustomZonedToLocalDeserializer.class)
private LocalDateTime date;
// getter and setter
}
还创建了反序列化器类:
And also created the deserializer class:
public class CustomZonedToLocalDeserializer extends LocalDateTimeDeserializer {
public CustomZonedToLocalDeserializer() {
super(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
}
我已经测试了输入 2017-07-05T14:10:45.432 + 01:00 [欧洲/伦敦]
,结果是 LocalDateTime
,其值为 2017-07-05T14:10:45.432
。
I've tested with the input 2017-07-05T14:10:45.432+01:00[Europe/London]
and the result was a LocalDateTime
with the value 2017-07-05T14:10:45.432
.
如果输入格式不同,则需要在 CustomZonedToLocalDeserializer
类中使用此格式(而不是使用 DateTimeFormatter.ISO_ZONED_DATE_TIME
,您将使用 DateTimeFormatter.ofPattern(图案)
)。
If the input is in a different format, then you need to use this format in the CustomZonedToLocalDeserializer
class (instead of using DateTimeFormatter.ISO_ZONED_DATE_TIME
, you'd use DateTimeFormatter.ofPattern(pattern)
).
这篇关于如何在自定义反序列化器中使用一些Jackson Deserializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!