Skip to content

Commit bacb37f

Browse files
committed
Fix #1155 fix in 2.7 branch
1 parent 700617a commit bacb37f

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

release-notes/CREDITS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,8 @@ Jiri Mikulasek (pirkogdc@github)
430430
Xavi Torrens (xavitorrens@github)
431431
* Reported #1150: Problem with Object id handling, explicit `null` token
432432
(2.7.3)
433+
434+
Yoann Rodière (fenrhil@github)
435+
* Reported #1154: @JsonFormat.pattern on dates is now ignored if shape is not
436+
explicitely provided
437+
(2.7.3)

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project: jackson-databind
99
#1125: Problem with polymorphic types, losing properties from base type(s)
1010
#1150: Problem with Object id handling, explicit `null` token
1111
(reported by Xavi T)
12+
#1154: @JsonFormat.pattern on dates is now ignored if shape is not explicitely provided
13+
(reported by Yoann R)
1214

1315
2.7.2 (26-Feb-2016)
1416

src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
5555
if (format != null) {
5656

5757
// Simple case first: serialize as numeric timestamp?
58-
if (format.getShape().isNumeric()) {
58+
JsonFormat.Shape shape = format.getShape();
59+
if (shape.isNumeric()) {
5960
return withFormat(Boolean.TRUE, null);
6061
}
6162

62-
if (format.getShape() == JsonFormat.Shape.STRING) {
63+
if ((shape == JsonFormat.Shape.STRING) || format.hasPattern()
64+
|| format.hasLocale() || format.hasTimeZone()) {
6365
TimeZone tz = format.getTimeZone();
6466
final String pattern = format.hasPattern()
65-
? format.getPattern()
66-
: StdDateFormat.DATE_FORMAT_STR_ISO8601;
67+
? format.getPattern()
68+
: StdDateFormat.DATE_FORMAT_STR_ISO8601;
6769
final Locale loc = format.hasLocale()
68-
? format.getLocale()
69-
: serializers.getLocale();
70+
? format.getLocale()
71+
: serializers.getLocale();
7072
SimpleDateFormat df = new SimpleDateFormat(pattern, loc);
7173
if (tz == null) {
7274
tz = serializers.getTimeZone();

src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ public CalendarAsStringBean(long l) {
6464
}
6565
}
6666

67+
static class DateAsDefaultBean {
68+
public Date date;
69+
public DateAsDefaultBean(long l) { date = new java.util.Date(l); }
70+
}
71+
72+
static class DateAsDefaultBeanWithEmptyJsonFormat {
73+
@JsonFormat
74+
public Date date;
75+
public DateAsDefaultBeanWithEmptyJsonFormat(long l) { date = new java.util.Date(l); }
76+
}
77+
78+
static class DateAsDefaultBeanWithPattern {
79+
@JsonFormat(pattern="yyyy-MM-dd")
80+
public Date date;
81+
public DateAsDefaultBeanWithPattern(long l) { date = new java.util.Date(l); }
82+
}
83+
84+
static class DateAsDefaultBeanWithLocale {
85+
@JsonFormat(locale = "fr")
86+
public Date date;
87+
public DateAsDefaultBeanWithLocale(long l) { date = new java.util.Date(l); }
88+
}
89+
90+
static class DateAsDefaultBeanWithTimezone {
91+
@JsonFormat(timezone="CET")
92+
public Date date;
93+
public DateAsDefaultBeanWithTimezone(long l) { date = new java.util.Date(l); }
94+
}
95+
6796
/*
6897
/**********************************************************
6998
/* Test methods
@@ -229,5 +258,53 @@ public void testWithTimeZoneOverride() throws Exception
229258
json = w.writeValueAsString(new Date(0));
230259
assertEquals(quote("1969-12-31/19:00 EST"), json);
231260
}
261+
262+
/**
263+
* Test to ensure that the default shape is correctly inferred as string or numeric,
264+
* when this shape is not explicitly set with a <code>@JsonFormat</code> annotation
265+
*/
266+
public void testDateDefaultShape() throws Exception
267+
{
268+
ObjectMapper mapper = new ObjectMapper();
269+
// No @JsonFormat => default to user config
270+
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
271+
String json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
272+
assertEquals(aposToQuotes("{'date':0}"), json);
273+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
274+
json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
275+
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
276+
277+
// Empty @JsonFormat => default to user config
278+
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
279+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
280+
assertEquals(aposToQuotes("{'date':0}"), json);
281+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
282+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
283+
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
284+
285+
// @JsonFormat with Shape.ANY and pattern => STRING shape, regardless of user config
286+
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
287+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
288+
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);
289+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
290+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
291+
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);
292+
293+
// @JsonFormat with Shape.ANY and locale => STRING shape, regardless of user config
294+
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
295+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
296+
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
297+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
298+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
299+
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
300+
301+
// @JsonFormat with Shape.ANY and timezone => STRING shape, regardless of user config
302+
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
303+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
304+
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
305+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
306+
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
307+
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
308+
}
232309
}
233310

0 commit comments

Comments
 (0)