Skip to main content

How to control Date formatting when Jackson JSON Processor is used with Spring 3.0

If you have not already checked out Spring 3.0, I highly recommend that you do. They have introduced lots of new features in this release. Rest support in Spring MVC and being able to output XML and JSON formatted strings out of the box are few new features of this release.

I recently set up a simple web application to try out few of these features. This web application had a simple controller that was being exposed as a REST service. I configured Spring 3.0 to use ContentNegotiatingViewResolver (more to come on this on later posts). With this and the built in support for JSON, this simple controller was able output JSON string in no time.

Spring 3 by default uses Jackson JSON from http://jackson.codehaus.org/ with a MappingJacksonJsonView. One of the data fields that was getting outputted was java.util.Date. Jackson uses default strategy to determine date formatting in serialization. If you go to Jackson's website, you will see that there are few different ways to change the formatting and the strategy. You can for example use SerilizationConfig object to configure the date formatting. However, this was going to take a lot to integrate into Spring and Spring MVC Views.

One of the great things about Jackson is its annotations. One of the annotations they have is @JsonSerialize. Here is detailed information about this annotation http://jackson.codehaus.org/1.2.1/javadoc/org/codehaus/jackson/map/annotate/JsonSerialize.html. You basically use this annotation for configuring serialization aspects. In my example, I decorated by model objects date getter method with this annotation.

@JsonSerialize(using = CustomDateSerializer.class)
public Date getEnd() {
return end;
}

CustomDateSerializer is an object I wrote that extends JsonSerializer that will handle the serialization for this field. Here is how this class looks.

public class CustomDateSerializer extends JsonSerializer {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {

SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
String formattedDate = formatter.format(value);

gen.writeString(formattedDate);

}
}

With serialize method as you can see you get access to the field value and the generator that is responsible for handling the write operation. In the method, I basically format my date string and have it written.

Comments

Unknown said…
Thank you! That's exactly what I'm looking for.
anjan said…
cool... exactly what I was looking for... cheers.
Nick said…
Using jackson 1.4 I found I had to change the class declaration from:

public class CustomDateSerializer extends JsonSerializer {

to

public class CustomDateSerializer extends JsonSerializer<Object> {

for it to compile.
KZM said…
Thank you so much
Unknown said…
I there a way to do this globally? Not having to repeat annotations on every Date property.... Thanks!
Riccardo Cossu said…
If you want to do it globally you have to properly configure Jackson's ObjectMapper.
You can see an example in this stackoverflow thread:
http://stackoverflow.com/questions/4428109/jersey-jackson-json-date-serialization-format-problem-how-to-change-the-forma

The solution applies to JAX-RS, but it can be easily extrapolated to work with plain Jackson
villuj said…
This comment has been removed by the author.
eodgooch said…
Thanks for the info. One thing to note, http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/JsonSerializer.html states "NOTE: it is recommended that custom serializers extend SerializerBase instead of this class, since it will implement many of optional methods of this class."
vignesh said…
Hi,
Thank you, can you please update how to set date.