Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS Jackson Json Provider Date Format Issue

WRT to the following question:

Jersey + Jackson JSON date format serialization - how to change the format or use custom JacksonJsonProvider.

I wish to know

  • Is Jackson specifying that the json date format should be normalised to a unix time integer?

Follow-up questions ...

  • Has there been a change in its stance anytime recently?
  • Shouldn't a date format be normalised to the same format provided by jaxb xml output?
  • why/why not?
  • any effort put into resolving this issue?
  • has RestEasy provided a json provider mitigation that would output json date in a generally recognisable date format?
like image 210
Blessed Geek Avatar asked Jun 27 '12 16:06

Blessed Geek


1 Answers

Sorry people for yelling out loud - I found the answers here

http://wiki.fasterxml.com/JacksonFAQDateHandling,

here

http://wiki.fasterxml.com/JacksonFAQ#Serializing_Dates,

here

http://wiki.fasterxml.com/JacksonHowToCustomSerializers

here

http://jackson.codehaus.org/1.1.2/javadoc/org/codehaus/jackson/map/util/StdDateFormat.html

Using the @JsonSerialize(using= ... ) way:

public class JsonStdDateSerializer
extends JsonSerializer<Date> {
  private static final DateFormat iso8601Format =
    StdDateFormat.getBlueprintISO8601Format();

  @Override
  public void serialize(
    Date date, JsonGenerator jgen, SerializerProvider provider)
  throws IOException, JsonProcessingException {

    // clone because DateFormat is not thread-safe
    DateFormat myformat = (DateFormat) iso8601Format.clone();
    String formattedDate = myformat.format(date);
    jgen.writeString(formattedDate);
  }
}
like image 161
Blessed Geek Avatar answered Sep 21 '22 01:09

Blessed Geek