我想知道是否可以在不创建自定义DateTime
的情况下使用Jackson [将] Joda String
对象序列化为ISO8601 JsonSerializer<DateTime>
。
当然这是一个足够普通的功能,可以在某个地方内置到库中吗?我能找到的最接近的是SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
,但这似乎只适用于Date
个对象。
修改
我找到了this class:ISO8601DateFormat
,但是当我尝试以下代码时,我没有得到ISO8601字符串:
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new ISO8601DateFormat());
mapper.writer().writeValueAsString(DateTime.now());
看起来Jackson并没有平等对待Date和DateTime对象。
更新
我最终为ISO8601 DateTime字符串编写了自定义序列化程序。
答案 0 :(得分:2)
我自己没试过,但它看起来像you should be able to do this:
// Set the date format to the desired (in this case, ISO8601)
objectMapper.getDeserializationConfig().setDateFormat(myDateFormat);
// or, as of Jackson 1.8, use
ObjectMapper#withDateFormat(myDateFormat)
因为
从版本1.4开始,Jackson为Joda Time数据类型提供了一些支持:基本上,
DateTime
可以自动序列化/反序列化,类似于处理java.util.Date
的方式。
当前的API调用是:
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);