Skip to content

Commit

Permalink
include the time zone name when serializing DateTimes, not just the o…
Browse files Browse the repository at this point in the history
…ffset from UTC
  • Loading branch information
David Byron committed Jul 8, 2014
1 parent 47460e1 commit 08c328c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.ReadableDateTime;
import org.joda.time.ReadableInstant;

Expand Down Expand Up @@ -53,10 +54,25 @@ public ReadableDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
if (str.length() == 0) { // [JACKSON-360]
return null;
}
if (ctxt.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE))
return new DateTime(str, dtz);
else
// Split the string at the first slash. If there's no first
// slash, assume we're dealing with an ISO8601 serialization.
int firstSlash = str.indexOf('/');
if (firstSlash == -1) {
if (ctxt.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) {
return new DateTime(str, dtz);
}

return DateTime.parse(str);
}

// Parse our custom serialized format with the explicit time zone identifier
String dateTimeStr = str.substring(0,firstSlash - 1);
String zoneStr = str.substring(firstSlash + 1);

LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr);
DateTimeZone zone = DateTimeZone.forID(zoneStr);

return localDateTime.toDateTime(zone);
}
// TODO: in 2.4, use 'handledType()'
throw ctxt.mappingException(getValueClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;

import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;

public final class DateTimeSerializer
extends JodaDateSerializerBase<DateTime>
{
protected final static JacksonJodaFormat DEFAULT_FORMAT
= new JacksonJodaFormat(ISODateTimeFormat.dateTime().withZoneUTC());
= new JacksonJodaFormat(new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateHourMinuteSecondMillis())
.appendLiteral('/')
.appendTimeZoneId()
.toFormatter());

public DateTimeSerializer() { this(DEFAULT_FORMAT); }
public DateTimeSerializer(JacksonJodaFormat format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public class JacksonJodaFormat

public JacksonJodaFormat(DateTimeFormatter defaultFormatter) {
_useTimestamp = null;
_jdkTimezone = defaultFormatter.getZone().toTimeZone();

DateTimeZone zone = defaultFormatter.getZone();
_jdkTimezone = (zone == null) ? null : zone.toTimeZone();

_locale = DEFAULT_LOCALE;
_formatter = defaultFormatter;
_explicitTimezone = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public void testSerializationFeatureNoTimestamp() throws IOException
{
ObjectMapper m = jodaMapper();
m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals(quote("1970-01-01T00:00:00.000Z"), m.writeValueAsString(DATE_JAN_1_1970_UTC));
assertEquals(quote("1970-01-01T00:00:00.000/UTC"), m.writeValueAsString(DATE_JAN_1_1970_UTC));
}

public void testAnnotationAsText() throws IOException
{
ObjectMapper m = jodaMapper();
m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// with annotations, doesn't matter if mapper configured to use timestamps
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000Z'}"),
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000/UTC'}"),
m.writeValueAsString(new DateAsText(DATE_JAN_1_1970_UTC)));
}

Expand All @@ -94,7 +94,7 @@ public void testSerializationWithTypeInfo() throws IOException
ObjectMapper m = jodaMapper();
m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
m.addMixInAnnotations(DateTime.class, ObjectConfiguration.class);
assertEquals("[\"org.joda.time.DateTime\",\"1970-01-01T00:00:00.000Z\"]",
assertEquals("[\"org.joda.time.DateTime\",\"1970-01-01T00:00:00.000/UTC\"]",
m.writeValueAsString(dt));
}
}

0 comments on commit 08c328c

Please sign in to comment.