According to http://wiki.fasterxml.com/JacksonFAQDateHandling, “DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this automatic functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration.

http://wiki.fasterxml.com/JacksonFAQDateHandling次,我都会设置配置,以便将日期写入时间戳是错误的.结果就是java.util.日期类型序列化为ISO 8601格式,但不包括组织.乔达.时间DateTime类型被序列化为长对象表示形式.

My environment is this:

杰克逊2.1

我的jsonMapper bean的Spring配置是

@Bean
public ObjectMapper jsonMapper() {
    ObjectMapper objectMapper = new ObjectMapper();

    //Fully qualified path shows I am using latest enum
    ObjectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
        WRITE_DATES_AS_TIMESTAMPS , false);

    return objectMapper;
}

我的测试代码片段如下

Date d = new Date();
DateTime dt = new DateTime(d); //Joda time 
Map<String, Object> link = new LinkedHashMap<String, Object>();
link.put("date", d);
link.put("createdDateTime", dt);

The resulting snippet of JSON output is this:

{"date":"2012-12-24T21:20:47.668+0000"}

{"createdDateTime": {"year":2012,"dayOfMonth":24,"dayOfWeek":1,"era":1,"dayOfYear":359,"centuryOfEra":20,"yearOfEra":2012,"yearOfCentury":12,"weekyear":2012,"monthOfYear":12 *... remainder snipped for brevity*}}

我的期望是DateTime对象应该根据配置匹配Date对象.我做错了什么,或者我误解了什么?我是不是从Jackson文档中读了太多关于单词automatically的内容,并且产生了字符串表示法(尽管不是ISO 8601)这一事实正在产生广告中的自动功能?

推荐答案

I was able to get the answer to this from the Jackson user mailing list, and wanted to share with you since it is a newbie issue. From reading the Jackson Date FAQ, I did not realize that extra dependencies and registration are required, but that is the case. It is documented at the git hub project page here https://github.com/FasterXML/jackson-datatype-joda

Essentially, I had to add another dependency to a Jackson jar specific to the Joda data type, and then I had to register the use of that module on the object mapper. The code snippets are below.

For my Jackson Joda data type Maven dependency setup I used this:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>${jackson.version}</version>
</dependency>

为了注册Joda序列化/反序列化功能,我使用了以下方法:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
    WRITE_DATES_AS_TIMESTAMPS , false);

Json相关问答推荐

在解码的JSON哈希中搜索数组元素

Postgres Select json数组并重新映射属性名称

用于参考的Jolt变换

Pandas 对REST API的自定义响应

PowerShell女士:如何处理json对象?

将pyspark.sql.Rowtype数据转换为Json字符串,消除Azure Databricks NB中的值

展平多个数组以保持顺序

Jolt 变换以展平 json 字符串数组

如何在循环中传递列表(会话(字符串属性))以在 Gatling Scala 中创建批量 Json 请求

判断golang中解析的json响应中是否存在所需的json键(不是值)

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

如何配置spring mvc 3在json响应中不返回null对象?

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

在 JSON 编码的 HTML5 数据属性中转义/编码单引号

JSON.stringify 不会转义?

使用 ajax 将 JSON 发送到 PHP

区分字符串和列表的 Pythonic 方式是什么?

在自定义 JsonConverter 的 ReadJson 方法中处理空对象

如何从 BindingResult 获取控制器中的错误文本

无法将 System.String 转换或转换为 Class 对象