LocalDateTime - 使用LocalDateTime.parse进行反序列化 [英] LocalDateTime - deserialization with LocalDateTime.parse

查看:4261
本文介绍了LocalDateTime - 使用LocalDateTime.parse进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字段 initiationDate ,它按 ToStringSerializer 类序列化为ISO-8601格式。

I have the field initiationDate which serialises by ToStringSerializer class to ISO-8601 format.

@JsonSerialize(using = ToStringSerializer.class)
private LocalDateTime initiationDate;

当我收到以下JSON时,

When I receive the following JSON,

...
"initiationDate": "2016-05-11T17:32:20.897",
...

我想通过 LocalDateTime.parse(CharSequence text)工厂方法对其进行反序列化。我的所有尝试都以 com.fasterxml.jackson.databind.JsonMappingException 结束:

I want to deserialize it by LocalDateTime.parse(CharSequence text) factory method. All my attempts ended with com.fasterxml.jackson.databind.JsonMappingException:


无法从 String value(<$ c)实例化类型[simple type,class java.time.LocalDateTime ]的值$ C> '2016-05-11T17:32:20.897');没有单一 - 字符串构造函数/工厂方法

Can not instantiate value of type [simple type, class java.time.LocalDateTime] from String value ('2016-05-11T17:32:20.897'); no single-String constructor/factory method

我如何实现这一目标?如何指定工厂方法?

How do I achieve that? How can I specify factory method?

编辑:

通过将 jackson-datatype-jsr310模块包含在项目中并使用<已解决该问题code> @JsonDeserialize with LocalDateTimeDeserializer

The problem has been solved by including jackson-datatype-jsr310 module to the project and using @JsonDeserialize with LocalDateTimeDeserializer.

@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime initiationDate;


推荐答案

Vanilla Jackson没办法从任何JSON字符串值反序列化 LocalDateTime 对象。

Vanilla Jackson doesn't have a way to deserialize a LocalDateTime object from any JSON string value.

您有几个选项。您可以创建并注册自己的 JsonDeserializer ,它将使用 LocalDateTime#parse

You have a few options. You can create and register your own JsonDeserializer which will use LocalDateTime#parse.

class ParseDeserializer extends StdDeserializer<LocalDateTime> {
    public ParseDeserializer() {
        super(LocalDateTime.class);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return LocalDateTime.parse(p.getValueAsString()); // or overloaded with an appropriate format
    }
}
...
@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = ParseDeserializer.class)
private LocalDateTime initiationDate;

或者你可以添加杰克逊的 java.time 扩展名到您的类路径并注册相应的模块使用 ObjectMapper

Or you can add Jackson's java.time extension to your classpath and register the appropriate Module with your ObjectMapper.

objectMapper.registerModule(new JavaTimeModule());

让杰克逊为你做转换。在内部,它使用其中一种标准格式的 LocalDateTime#parse 。幸运的是,它支持的值如

and let Jackson do the conversion for you. Internally, this uses LocalDateTime#parse with one of the standard formats. Fortunately, it supports values like

2016-05-11T17:32:20.897

开箱即用。

这篇关于LocalDateTime - 使用LocalDateTime.parse进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆