Reputation: 25823
I have a Joda DateTime in an Order class:
public class Order {
private DateTime creationTime;
...
}
I have initialized my mapper as follows:
mapper.configure(
SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
When I serialize this class, I expect to see creationTime serialized in the ISO-8601 format as follows
{
"creationTime" : "2011-01-01T09:00:00.000-04:00"
}
This is working perfectly fine in my unit test. However, in my web application, the exact same code is serializing all the DateTime fields:
{
"creationTime" : {
"year" : 2011,
"dayOfMonth" : 17,
"dayOfWeek" : 7,
"era" : 1,
"dayOfYear" : 107,
"weekOfWeekyear" : 15,
"weekyear" : 2011,
"monthOfYear" : 4,
"yearOfEra" : 2011,
"yearOfCentury" : 11,
"centuryOfEra" : 20,
"millisOfSecond" : 590,
"millisOfDay" : 40311590,
"secondOfMinute" : 51,
"secondOfDay" : 40311,
"minuteOfHour" : 11,
"minuteOfDay" : 671,
"hourOfDay" : 11,
"millis" : 1303053111590,
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "America/New_York"
},
"id" : "America/New_York"
},
"chronology" : {
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "America/New_York"
},
"id" : "America/New_York"
}
}
}
What am I missing? I am including jackson-core-asl-1.7.6.jar and jackson-mapper-asl-1.7.6.jar in my classpath in both cases.
In some online examples I saw an annotation on DateTime. I don't know if this is needed, but I tried it nevertheness. See below:
public class Order {
@JsonSerialize(using=DateTimeSerializer.class)
private DateTime creationTime;
...
}
This seems to make no difference.
Thanks.
P.S. Does anyone know if the Jackson mailing list is working? I posted this question on the user mailing list, but it doesn't show in the archives. The last post in the archives is dated 24 June 2010.
Upvotes: 6
Views: 19485
Reputation: 31
This seemed to do the trick for me: How to serialize Joda DateTime with Jackson JSON processer?
Basically, the idea was to create a class that extends org.codehaus.jackson.map.JsonSerializer
with a overried serialize method:
public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.print(value));
}
Then just use that custom serializer in place of DateTimeSerializer
.
Upvotes: 3