diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java index d7593a34cd..682da99624 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java @@ -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 @@ -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 @JsonFormat 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); + } }