126

How do I get Jackson to serialize my Joda DateTime object according to a simple pattern (like "dd-MM-yyyy")?

I've tried:

@JsonSerialize(using=DateTimeSerializer.class)
private final DateTime date;

I've also tried:

ObjectMapper mapper = new ObjectMapper()
    .getSerializationConfig()
    .setDateFormat(df);

Thanks!

zb226
  • 8,586
  • 6
  • 44
  • 73
Haywood Jablomey
  • 1,239
  • 2
  • 9
  • 3
  • Both of above should actually also work (@JsonSerialize should imply that field is to be serialized; and date format should also ideally apply to Joda), so you might want to file a Jira bug at http://jira.codehaus.org/browse/JACKSON. – StaxMan Sep 02 '10 at 18:49
  • I realize this question is from a while back, but for future reference, objectMapper.getSerializationConfig().setDateFormat(df) is now deprecated. objectMapper.setDateFormat(df) is now suggested. – Patrick Sep 07 '12 at 14:54
  • See also https://stackoverflow.com/questions/27952472/serialize-deserialize-java-8-java-time-with-jackson-json-mapper – Raedwald Mar 01 '19 at 15:54

9 Answers9

154

This has become very easy with Jackson 2.0 and the Joda module.

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

Maven dependency:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.1.1</version>
</dependency>  

Code and documentation: https://github.com/FasterXML/jackson-datatype-joda

Binaries: http://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-joda/

quietmint
  • 13,260
  • 6
  • 47
  • 72
Kimble
  • 6,868
  • 4
  • 50
  • 73
  • 17
    Could you expand on this please? Where does this code go and how is the ObjectMapper instance used? – Paul Jul 29 '13 at 23:17
  • You should ask specific questions regarding the use of Jackson's api and Maven. Where the code goes depends on whether you use any frameworks / how you bootstrap your application. – Kimble Sep 29 '13 at 17:17
  • 4
    when I add that I get the compile error "incompatible types: JodaModule cannot be converted to Module" - the method expects a org.codehaus.jackson.map.Module but JodaModule does not have this in its heirarchy so how could this work? – Martin Charlesworth Nov 20 '13 at 23:12
  • Martin: Perhaps you have different versions of Jackson and the Jackson Joda Module on your classpath? – Kimble Jun 18 '14 at 18:49
  • 5
    Promising but currently almost useless as we cannot specify the date format to parse :( – Vincent Mimoun-Prat Jul 15 '14 at 17:36
  • @MarvinLabs my answer shows how to implement custom formatting for `DateTime` with Jackson 2 – oggmonster Sep 16 '14 at 09:18
  • 10
    You need to disable serialization as timestamps objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); – MarekM Oct 02 '14 at 11:01
  • Is it still working for the newest version ? Like `2.8.8` ? – Weedoze Jun 20 '17 at 14:11
  • Doesn't even need to add Module. It works just by adding the dependency. – coretechie Sep 07 '17 at 10:36
74

In the object you're mapping:

@JsonSerialize(using = CustomDateSerializer.class)
public DateTime getDate() { ... }

In CustomDateSerializer:

public class CustomDateSerializer extends JsonSerializer<DateTime> {

    private static DateTimeFormatter formatter = 
        DateTimeFormat.forPattern("dd-MM-yyyy");

    @Override
    public void serialize(DateTime value, JsonGenerator gen, 
                          SerializerProvider arg2)
        throws IOException, JsonProcessingException {

        gen.writeString(formatter.print(value));
    }
}
Rusty Kuntz
  • 741
  • 4
  • 2
  • 4
    How to register CustomDateSerializer in spring mvc 3? – digz6666 Mar 21 '11 at 06:26
  • 1
    I don't know about Spring MVC3, but you can add a custom serializer to the Jackson mapper object. See [this entry in Jackson](http://wiki.fasterxml.com/SimpleModule) You need to create a simple module for that SimpleModule isoDateTimeModule = new SimpleModule("ISODateTimeModule", new Version(1, 0, 0, null)); isoDateTimeModule.addSerializer(new JsonISODateTimeFormatSerializer()); mapper.registerModule(isoDateTimeModule); – Guillaume Belrose Feb 29 '12 at 17:02
  • 1
    @digz6666 see my answer here for Spring MVC 3: http://stackoverflow.com/questions/7854030/configurating-objectmapper-in-spring/12617890#12617890 – Aram Kocharyan Sep 27 '12 at 09:12
  • Note - make sure to use the above classes from `org.codehaus.jackson` rather than `com.fasterxml.jackson.core`. Using the second package didn't work for me. In fact, my Jersey app didn't even respect the `@JsonSerialized` annotation. – Kevin Meredith May 13 '14 at 17:08
  • The answer did work for me. But it would require this annotation on all the fields. Is there some global configuration for this which works with Jackson? – Taher Jun 12 '15 at 02:36
  • Is there a way to do this for ALL datetime objects as I do not want to alter all my mapped objects? – Lars Juel Jensen Jun 03 '16 at 18:06
27

As @Kimble has said, with Jackson 2, using the default formatting is very easy; simply register JodaModule on your ObjectMapper.

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

For custom serialization/de-serialization of DateTime, you need to implement your own StdScalarSerializer and StdScalarDeserializer; it's pretty convoluted, but anyway.

For example, here's a DateTime serializer that uses the ISODateFormat with the UTC time zone:

public class DateTimeSerializer extends StdScalarSerializer<DateTime> {

    public DateTimeSerializer() {
        super(DateTime.class);
    }

    @Override
    public void serialize(DateTime dateTime,
                          JsonGenerator jsonGenerator,
                          SerializerProvider provider) throws IOException, JsonGenerationException {
        String dateTimeAsString = ISODateTimeFormat.withZoneUTC().print(dateTime);
        jsonGenerator.writeString(dateTimeAsString);
    }
}

And the corresponding de-serializer:

public class DateTimeDesrializer extends StdScalarDeserializer<DateTime> {

    public DateTimeDesrializer() {
        super(DateTime.class);
    }

    @Override
    public DateTime deserialize(JsonParser jsonParser,
                                DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try {
            JsonToken currentToken = jsonParser.getCurrentToken();
            if (currentToken == JsonToken.VALUE_STRING) {
                String dateTimeAsString = jsonParser.getText().trim();
                return ISODateTimeFormat.withZoneUTC().parseDateTime(dateTimeAsString);
            }
        } finally {
            throw deserializationContext.mappingException(getValueClass());
        }
    }

Then tie these together with a module:

public class DateTimeModule extends SimpleModule {

    public DateTimeModule() {
        super();
        addSerializer(DateTime.class, new DateTimeSerializer());
        addDeserializer(DateTime.class, new DateTimeDeserializer());
    }
}

Then register the module on your ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new DateTimeModule());
oggmonster
  • 4,442
  • 8
  • 50
  • 73
19

The easy solution

I have encountered similar problem and my solution is much clear than above.

I simply used the pattern in @JsonFormat annotation

Basically my class has a DateTime field, so I put an annotation around the getter:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public DateTime getDate() {
    return date;
}

I serialize the class with ObjectMapper

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    ObjectWriter ow = mapper.writer();
    try {
        String logStr = ow.writeValueAsString(log);
        outLogger.info(logStr);
    } catch (IOException e) {
        logger.warn("JSON mapping exception", e);
    }

We use Jackson 2.5.4

Dan Torrey
  • 1,271
  • 15
  • 24
Atais
  • 10,067
  • 6
  • 65
  • 105
16

https://stackoverflow.com/a/10835114/1113510

Although you can put an annotation for each date field, is better to do a global configuration for your object mapper. If you use jackson you can configure your spring as follow:

<bean id="jacksonObjectMapper" class="com.company.CustomObjectMapper" />

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
    factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" >
</bean>

For CustomObjectMapper:

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        setDateFormat(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)"));
    }
}

Of course, SimpleDateFormat can use any format you need.

Community
  • 1
  • 1
Moesio
  • 2,970
  • 1
  • 26
  • 36
11

Meanwhile Jackson registers the Joda module automatically when the JodaModule is in classpath. I just added jackson-datatype-joda to Maven and it worked instantly.

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.8.7</version>
</dependency>

JSON output:

{"created" : "2017-03-28T05:59:27.258Z"}
Stephan
  • 4,296
  • 3
  • 25
  • 49
7

For those with Spring Boot you have to add the module to your context and it will be added to your configuration like this.

@Bean
public Module jodaTimeModule() {
    return new JodaModule();
}

And if you want to use the new java8 time module jsr-310.

@Bean
public Module jodaTimeModule() {
    return new JavaTimeModule();
}
jgeerts
  • 591
  • 7
  • 17
3

It seems that for Jackson 1.9.12 there is no such possibility by default, because of:

public final static class DateTimeSerializer
    extends JodaSerializer<DateTime>
{
    public DateTimeSerializer() { super(DateTime.class); }

    @Override
    public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException
    {
        if (provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)) {
            jgen.writeNumber(value.getMillis());
        } else {
            jgen.writeString(value.toString());
        }
    }

    @Override
    public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type typeHint)
    {
        return createSchemaNode(provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)
                ? "number" : "string", true);
    }
}

This class serializes data using toString() method of Joda DateTime.

Approach proposed by Rusty Kuntz works perfect for my case.

ydrozhdzhal
  • 154
  • 1
  • 4
  • You can still register custom serializer and deserializer, and those will have precedence over default implementation. – StaxMan Aug 06 '13 at 00:04
3

I'm using Java 8 and this worked for me.

Add the dependency on pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.4.0</version>
</dependency>

and add JodaModule on your ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
djm.im
  • 3,153
  • 4
  • 26
  • 42