Jackson - 序列化日期对象
Jackson - serializing date objects
我正在使用 Jackson 将 POJO 序列化为 JSON,但我在设置日期格式方面遇到困难。我在杰克逊的网站上看到了这个维基:http://wiki.fasterxml.com/JacksonFAQDateHandling
通过这篇文章,我做了以下事情:
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
这会以 ISO-8601 格式打印 Date 对象,这正是我想要的。除了“+0000”。根据 ISO-8601 格式,+hhmm 是与 UTC 的时间偏移。杰克逊有没有办法禁用时间偏移?还是我只需要指定自定义日期格式?
我认为你最好的选择是在序列化时只传递一个新的日期格式。例如:
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"));
基于 JavaDocs on github 可以深入了解日期格式的确定方式,无需指定您自己的格式,我认为您只能使用数字时间戳或 Jackson 定义的内置 ISO 格式。参考文档:
/**
* Feature that determines whether Date (and date/time) values
* (and Date-based things like {@link java.util.Calendar}s) are to be
* serialized as numeric timestamps (true; the default),
* or as something else (usually textual representation).
* If textual representation is used, the actual format is
* one returned by a call to
* {@link com.fasterxml.jackson.databind.SerializationConfig#getDateFormat}:
* the default setting being {@link com.fasterxml.jackson.databind.util.StdDateFormat},
* which corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
* (see {@link java.text.DateFormat} for details of format Strings).
*<p>
* Note: whether this feature affects handling of other date-related
* types depend on handlers of those types, although ideally they
* should use this feature
*<p>
* Note: whether {@link java.util.Map} keys are serialized as Strings
* or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS}.
*<p>
* Feature is enabled by default, so that date/time are by default
* serialized as timestamps.
*/
WRITE_DATES_AS_TIMESTAMPS(true),
我正在使用 Jackson 将 POJO 序列化为 JSON,但我在设置日期格式方面遇到困难。我在杰克逊的网站上看到了这个维基:http://wiki.fasterxml.com/JacksonFAQDateHandling
通过这篇文章,我做了以下事情:
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
这会以 ISO-8601 格式打印 Date 对象,这正是我想要的。除了“+0000”。根据 ISO-8601 格式,+hhmm 是与 UTC 的时间偏移。杰克逊有没有办法禁用时间偏移?还是我只需要指定自定义日期格式?
我认为你最好的选择是在序列化时只传递一个新的日期格式。例如:
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"));
基于 JavaDocs on github 可以深入了解日期格式的确定方式,无需指定您自己的格式,我认为您只能使用数字时间戳或 Jackson 定义的内置 ISO 格式。参考文档:
/**
* Feature that determines whether Date (and date/time) values
* (and Date-based things like {@link java.util.Calendar}s) are to be
* serialized as numeric timestamps (true; the default),
* or as something else (usually textual representation).
* If textual representation is used, the actual format is
* one returned by a call to
* {@link com.fasterxml.jackson.databind.SerializationConfig#getDateFormat}:
* the default setting being {@link com.fasterxml.jackson.databind.util.StdDateFormat},
* which corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
* (see {@link java.text.DateFormat} for details of format Strings).
*<p>
* Note: whether this feature affects handling of other date-related
* types depend on handlers of those types, although ideally they
* should use this feature
*<p>
* Note: whether {@link java.util.Map} keys are serialized as Strings
* or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS}.
*<p>
* Feature is enabled by default, so that date/time are by default
* serialized as timestamps.
*/
WRITE_DATES_AS_TIMESTAMPS(true),