Skip to content

Commit

Permalink
Further default change for 3.0 wrt #2643 (+00:00 -> Z)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 6, 2020
1 parent b81afe0 commit 07422d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,9 @@ protected void _format(TimeZone tz, Locale loc, Date date,
}
pad2(buffer, minutes);
} else {
// 24-Jun-2017, tatu: While `Z` would be conveniently short, older specs
// mandate use of full `+0000`
// 06-Mar-2020, tatu: Actually statement should read "for compatibility reasons"
// and not standards (unless it is wrt RFC-1123). This will change in 3.0 at latest
// formatted.append('Z');
if (_tzSerializedWithColon) {
buffer.append("+00:00");
} else {
buffer.append("+0000");
}
// 06-Mar-2020, tatu: Jackson versions 2.x forced use of numeric offset even
// for Zulu; for 3.0 `Z` is used.
buffer.append('Z');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,25 @@ public void testDateISO8601() throws IOException
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.build();

serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+00:00");
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+00:00");
serialize(mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+00:00");
serialize(mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+00:00");
serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000Z");
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000Z");
serialize(mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000Z");
serialize(mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000Z");

// 22-Nov-2018, tatu: Also ensure we use padding...
serialize(mapper, judate(911, 1, 1, 00, 00, 00, 0, "UTC"), "0911-01-01T00:00:00.000+00:00");
serialize(mapper, judate(87, 1, 1, 00, 00, 00, 0, "UTC"), "0087-01-01T00:00:00.000+00:00");
serialize(mapper, judate(1, 1, 1, 00, 00, 00, 0, "UTC"), "0001-01-01T00:00:00.000+00:00");
serialize(mapper, judate(911, 1, 1, 00, 00, 00, 0, "UTC"), "0911-01-01T00:00:00.000Z");
serialize(mapper, judate(87, 1, 1, 00, 00, 00, 0, "UTC"), "0087-01-01T00:00:00.000Z");
serialize(mapper, judate(1, 1, 1, 00, 00, 00, 0, "UTC"), "0001-01-01T00:00:00.000Z");
}

// [databind#2167]: beyond year 9999 needs special handling
public void testDateISO8601_10k() throws IOException
{
ObjectWriter w = MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
serialize(w, judate(10204, 1, 1, 00, 00, 00, 0, "UTC"), "+10204-01-01T00:00:00.000+00:00");
serialize(w, judate(10204, 1, 1, 00, 00, 00, 0, "UTC"), "+10204-01-01T00:00:00.000Z");
// and although specification lacks for beyond 5 digits (well, actually even 5...), let's do our best:
serialize(w, judate(123456, 1, 1, 00, 00, 00, 0, "UTC"), "+123456-01-01T00:00:00.000+00:00");
serialize(w, judate(123456, 1, 1, 00, 00, 00, 0, "UTC"), "+123456-01-01T00:00:00.000Z");
}

// [databind#2167]: dates before Common Era (CE), that is, BCE, need special care:
Expand All @@ -140,10 +140,10 @@ public void testDateISO8601_BCE() throws IOException
// First: I _think_ BCE-1 is what you get with year 0, and should become "+0000"
// and from further back in time, it'll be "-0001" (BCE-2) etc)

serialize(w, judate(0, 1, 1, 00, 00, 00, 0, "UTC"), "+0000-01-01T00:00:00.000+00:00");
serialize(w, judate(-1, 1, 1, 00, 00, 00, 0, "UTC"), "-0001-01-01T00:00:00.000+00:00");
serialize(w, judate(-49, 1, 1, 00, 00, 00, 0, "UTC"), "-0049-01-01T00:00:00.000+00:00"); // All hail Caesar
serialize(w, judate(-264, 1, 1, 00, 00, 00, 0, "UTC"), "-0264-01-01T00:00:00.000+00:00"); // Carthage FTW?
serialize(w, judate(0, 1, 1, 00, 00, 00, 0, "UTC"), "+0000-01-01T00:00:00.000Z");
serialize(w, judate(-1, 1, 1, 00, 00, 00, 0, "UTC"), "-0001-01-01T00:00:00.000Z");
serialize(w, judate(-49, 1, 1, 00, 00, 00, 0, "UTC"), "-0049-01-01T00:00:00.000Z"); // All hail Caesar
serialize(w, judate(-264, 1, 1, 00, 00, 00, 0, "UTC"), "-0264-01-01T00:00:00.000Z"); // Carthage FTW?
}

/**
Expand Down Expand Up @@ -177,8 +177,8 @@ public void testDateISO8601_colonInTZ() throws IOException
.defaultDateFormat(dateFormat)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.build();
serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+0000");
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+0000");
serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000Z");
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000Z");
}

public void testDateOther() throws IOException
Expand Down Expand Up @@ -230,7 +230,7 @@ public void testDatesAsMapKeys() throws IOException
assertFalse(mapper.isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS));
map.put(new Date(0L), Integer.valueOf(1));
// by default will serialize as ISO-8601 values...
assertEquals("{\"1970-01-01T00:00:00.000+00:00\":1}", mapper.writeValueAsString(map));
assertEquals("{\"1970-01-01T00:00:00.000Z\":1}", mapper.writeValueAsString(map));

// but can change to use timestamps too
mapper = jsonMapperBuilder()
Expand Down Expand Up @@ -264,7 +264,7 @@ public void testDateWithJsonFormat() throws Exception

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

/**
Expand Down Expand Up @@ -313,7 +313,7 @@ public void testDateDefaultShape() throws Exception
json = MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(new DateAsDefaultBean(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+00:00'}"), json);
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000Z'}"), json);

// Empty @JsonFormat => default to user config
json = MAPPER.writer()
Expand All @@ -323,7 +323,7 @@ public void testDateDefaultShape() throws Exception
json = MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+00:00'}"), json);
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000Z'}"), json);

// @JsonFormat with Shape.ANY and pattern => STRING shape, regardless of user config
json = MAPPER.writer()
Expand All @@ -339,11 +339,11 @@ public void testDateDefaultShape() throws Exception
json = MAPPER.writer()
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+00:00'}"), json);
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000Z'}"), json);
json = MAPPER.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+00:00'}"), json);
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000Z'}"), json);

// @JsonFormat with Shape.ANY and timezone => STRING shape, regardless of user config
json = MAPPER.writer()
Expand Down

3 comments on commit 07422d3

@arungitan
Copy link

@arungitan arungitan commented on 07422d3 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the default is changing from "+00:00" to "Z", would it be possible to have some options in 3.x to produce the old backward compat formats "+0000" ?
We have some existing APIs which produce "+0000" and where (some) users are very sensitive to any change in the format. we were able to address this in 2.11 setting the provided colon flag to false. What would be the options to produce the same output in 3.x?

@cowtowncoder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably make sense to file a separate request (issue) against jackson-databind, mentioning desire for allowing this.
We will hopefully add a new way to more conveniently configure some date/time aspects and this could fall under that.

@arungitan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowtowncoder thx for reply - filed #3284

Please sign in to comment.