Java – jax-rs Jackson Jason provider date format problem
Wrt found the following problems:
Jersey + Jackson JSON date format serialization – how to change the format or use custom JacksonJsonProvider.
I want to know
>Does Jackson specify that the JSON date format should be normalized to a UNIX time integer?
Follow up questions
>Has his position changed recently? > Shouldn't the date format be standardized to the same format provided for JAXB XML output? > Why / why not? > Any effort to solve this problem? > Resteasy provides a JSON provider mitigation. Will it output JSON dates in a generally recognized date format?
Solution
Sorry for the yeller - I found the answer 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
Use @ jsonserialize (using =...):
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); } }