Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,20 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
return withFormat(Boolean.TRUE, null);
}

Boolean asNumber = (format.getShape() == JsonFormat.Shape.STRING) ? Boolean.FALSE : null;
// If not, do we have a pattern?
TimeZone tz = format.getTimeZone();
if (format.hasPattern()) {
String pattern = format.getPattern();
final Locale loc = format.hasLocale() ? format.getLocale() : serializers.getLocale();
if (format.getShape() == JsonFormat.Shape.STRING) {
TimeZone tz = format.getTimeZone();
final String pattern = format.hasPattern()
? format.getPattern()
: StdDateFormat.DATE_FORMAT_STR_ISO8601;
final Locale loc = format.hasLocale()
? format.getLocale()
: serializers.getLocale();
SimpleDateFormat df = new SimpleDateFormat(pattern, loc);
if (tz == null) {
tz = serializers.getTimeZone();
}
df.setTimeZone(tz);
return withFormat(asNumber, df);
}
// If not, do we at least have a custom timezone?
if (tz != null) {
DateFormat df = serializers.getConfig().getDateFormat();
// one shortcut: with our custom format, can simplify handling a bit
if (df.getClass() == StdDateFormat.class) {
final Locale loc = format.hasLocale() ? format.getLocale() : serializers.getLocale();
df = StdDateFormat.getISO8601Format(tz, loc);
} else {
// otherwise need to clone, re-set timezone:
df = (DateFormat) df.clone();
df.setTimeZone(tz);
}
return withFormat(asNumber, df);
return withFormat(Boolean.FALSE, df);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StdDateFormat
* to ISO-8601 date formatting standard, when it includes basic undecorated
* timezone definition
*/
protected final static String DATE_FORMAT_STR_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
public final static String DATE_FORMAT_STR_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

/**
* Same as 'regular' 8601, but handles 'Z' as an alias for "+0000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ static class DateAsStringBean {
public DateAsStringBean(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultStringBean {
@JsonFormat(shape=JsonFormat.Shape.STRING)
public Date date;
public DateAsDefaultStringBean(long l) { date = new java.util.Date(l); }
}

static class DateInCETBean {
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
public Date date;
Expand Down Expand Up @@ -192,6 +198,10 @@ public void testDateWithJsonFormat() throws Exception
// and for [Issue#423] as well:
json = mapper.writer().with(getUTCTimeZone()).writeValueAsString(new CalendarAsStringBean(0L));
assertEquals("{\"value\":\"1970-01-01\"}", json);

// and with default (ISO8601) format (databind#1109)
json = mapper.writeValueAsString(new DateAsDefaultStringBean(0L));
assertEquals("{\"date\":\"1970-01-01T00:00:00.000+0000\"}", json);
}

/**
Expand Down