{"id":262,"date":"2013-06-28T23:52:56","date_gmt":"2013-06-29T06:52:56","guid":{"rendered":"http:\/\/www.lorrin.org\/blog\/?p=262"},"modified":"2013-06-28T23:52:56","modified_gmt":"2013-06-29T06:52:56","slug":"custom-joda-time-dateformatter-in-jackson","status":"publish","type":"post","link":"https:\/\/www.lorrin.org\/blog\/2013\/06\/28\/custom-joda-time-dateformatter-in-jackson\/","title":{"rendered":"Custom Joda-Time DateFormatter in Jackson"},"content":{"rendered":"<p>Here is how to customize how <a href=\"https:\/\/github.com\/FasterXML\/jackson-core\">Jackson<\/a> serializes <a href=\"http:\/\/joda-time.sourceforge.net\/\">Joda-Time<\/a> dates to JSON:<\/p>\n<pre class=\"brush:java\">objectMapperFactory.registerModule(new SimpleModule() {\r\n    {\r\n        addSerializer(DateTime.class, new StdSerializer&lt;DateTime&gt;(DateTime.class) {\r\n            @Override\r\n            public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {\r\n                 jgen.writeString(ISODateTimeFormat.date().print(value));\r\n            }\r\n        });\r\n    }\r\n});<\/pre>\n<p>You can use this in combination with <a href=\"https:\/\/github.com\/FasterXML\/jackson-datatype-joda\">JodaModule<\/a>, just place it after the JodaModule is registered.<\/p>\n<p>Alternatively, if all you need is to write DateTimes in ISO 8061 format instead of as Unix epochs, you can use the following:<\/p>\n<pre class=\"brush:java\">objectMapperFactory.registerModule(new JodaModule())\r\nobjectMapperFactory.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);<\/pre>\n<p>JodaModule <a href=\"https:\/\/github.com\/FasterXML\/jackson-datatype-joda\/blob\/master\/src\/main\/java\/com\/fasterxml\/jackson\/datatype\/joda\/JodaModule.java\">registers<\/a> a custom <a href=\"https:\/\/github.com\/FasterXML\/jackson-datatype-joda\/blob\/master\/src\/main\/java\/com\/fasterxml\/jackson\/datatype\/joda\/ser\/DateTimeSerializer.java\">DateTimeSerializer<\/a> that takes the setting into account. However, unlike the standard Java Date implementation, <code class=\"&quot;brush:java\">SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS<\/code> and\u00a0<code class=\"&quot;brush:java\">getSerializationConfig().setDateFormat(myDateFormat)<\/code> are ignored, so there is no way to fine-tune the serialization.<\/p>\n<p>Ultimately a more elegant solution would be to give JodaModule some additional constructors or setters that allow passing in a DateFormatter that its various helper classes would use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to customize how Jackson serializes Joda-Time dates to JSON: objectMapperFactory.registerModule(new SimpleModule() { { addSerializer(DateTime.class, new StdSerializer&lt;DateTime&gt;(DateTime.class) { @Override public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { jgen.writeString(ISODateTimeFormat.date().print(value)); } }); } }); You can use this in combination with JodaModule, just place it after the JodaModule is registered. Alternatively, <a href='https:\/\/www.lorrin.org\/blog\/2013\/06\/28\/custom-joda-time-dateformatter-in-jackson\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[76,62,78,77],"_links":{"self":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/262"}],"collection":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":4,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}