Skip to content

Commit

Permalink
Fix #1155 fix in 2.7 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 12, 2016
1 parent 700617a commit bacb37f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,8 @@ Jiri Mikulasek (pirkogdc@github)
Xavi Torrens (xavitorrens@github)
* Reported #1150: Problem with Object id handling, explicit `null` token
(2.7.3)

Yoann Rodière (fenrhil@github)
* Reported #1154: @JsonFormat.pattern on dates is now ignored if shape is not
explicitely provided
(2.7.3)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project: jackson-databind
#1125: Problem with polymorphic types, losing properties from base type(s)
#1150: Problem with Object id handling, explicit `null` token
(reported by Xavi T)
#1154: @JsonFormat.pattern on dates is now ignored if shape is not explicitely provided
(reported by Yoann R)

2.7.2 (26-Feb-2016)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
if (format != null) {

// Simple case first: serialize as numeric timestamp?
if (format.getShape().isNumeric()) {
JsonFormat.Shape shape = format.getShape();
if (shape.isNumeric()) {
return withFormat(Boolean.TRUE, null);
}

if (format.getShape() == JsonFormat.Shape.STRING) {
if ((shape == JsonFormat.Shape.STRING) || format.hasPattern()
|| format.hasLocale() || format.hasTimeZone()) {
TimeZone tz = format.getTimeZone();
final String pattern = format.hasPattern()
? format.getPattern()
: StdDateFormat.DATE_FORMAT_STR_ISO8601;
? format.getPattern()
: StdDateFormat.DATE_FORMAT_STR_ISO8601;
final Locale loc = format.hasLocale()
? format.getLocale()
: serializers.getLocale();
? format.getLocale()
: serializers.getLocale();
SimpleDateFormat df = new SimpleDateFormat(pattern, loc);
if (tz == null) {
tz = serializers.getTimeZone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ public CalendarAsStringBean(long l) {
}
}

static class DateAsDefaultBean {
public Date date;
public DateAsDefaultBean(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithEmptyJsonFormat {
@JsonFormat
public Date date;
public DateAsDefaultBeanWithEmptyJsonFormat(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithPattern {
@JsonFormat(pattern="yyyy-MM-dd")
public Date date;
public DateAsDefaultBeanWithPattern(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithLocale {
@JsonFormat(locale = "fr")
public Date date;
public DateAsDefaultBeanWithLocale(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithTimezone {
@JsonFormat(timezone="CET")
public Date date;
public DateAsDefaultBeanWithTimezone(long l) { date = new java.util.Date(l); }
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -229,5 +258,53 @@ public void testWithTimeZoneOverride() throws Exception
json = w.writeValueAsString(new Date(0));
assertEquals(quote("1969-12-31/19:00 EST"), json);
}

/**
* Test to ensure that the default shape is correctly inferred as string or numeric,
* when this shape is not explicitly set with a <code>@JsonFormat</code> annotation
*/
public void testDateDefaultShape() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// No @JsonFormat => default to user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
assertEquals(aposToQuotes("{'date':0}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// Empty @JsonFormat => default to user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
assertEquals(aposToQuotes("{'date':0}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// @JsonFormat with Shape.ANY and pattern => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);

// @JsonFormat with Shape.ANY and locale => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// @JsonFormat with Shape.ANY and timezone => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
}
}

0 comments on commit bacb37f

Please sign in to comment.