Skip to content

Commit 3075bfe

Browse files
committed
Merge branch '2.13'
2 parents 19cea12 + 283536c commit 3075bfe

File tree

3 files changed

+111
-14
lines changed

3 files changed

+111
-14
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.fasterxml.jackson.datatype.jsr310.ser;
1818

19+
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE;
20+
1921
import java.time.format.DateTimeFormatter;
2022
import java.time.temporal.Temporal;
2123
import java.util.function.ToIntFunction;
@@ -130,10 +132,9 @@ protected String formatValue(T value, SerializerProvider provider)
130132
DateTimeFormatter formatter = (_formatter != null) ? _formatter : defaultFormat;
131133
if (formatter != null) {
132134
if (formatter.getZone() == null) { // timezone set if annotated on property
133-
// 19-Oct-2020, tatu: As per [modules-java#188], only override with explicitly
134-
// set timezone, to minimize change from pre-2.12. May need to further
135-
// improve in future to maybe introduce more configuration.
136-
if (provider.getConfig().hasExplicitTimeZone()) {
135+
// If the user specified to use the context TimeZone explicitly, and the formatter provided doesn't contain a TZ
136+
// Then we use the TZ specified in the objectMapper
137+
if (provider.getConfig().hasExplicitTimeZone() && provider.isEnabled(WRITE_DATES_WITH_CONTEXT_TIME_ZONE)) {
137138
formatter = formatter.withZone(provider.getTimeZone().toZoneId());
138139
}
139140
}

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package com.fasterxml.jackson.datatype.jsr310.ser;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import java.time.Instant;
46
import java.time.OffsetDateTime;
57
import java.time.ZoneId;
8+
import java.time.ZonedDateTime;
69
import java.time.format.DateTimeFormatter;
710
import java.time.temporal.Temporal;
811
import java.util.TimeZone;
912

13+
import org.junit.Test;
14+
1015
import com.fasterxml.jackson.annotation.JsonFormat;
1116
import com.fasterxml.jackson.databind.ObjectMapper;
1217
import com.fasterxml.jackson.databind.SerializationFeature;
1318
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
1419
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
1520
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
1621

17-
import org.junit.Test;
18-
19-
import static org.junit.Assert.assertEquals;
20-
2122
public class OffsetDateTimeSerTest
2223
extends ModuleTestBase
2324
{
@@ -233,4 +234,32 @@ public void testSerializationWithTypeInfoAndMapperTimeZone() throws Exception
233234
assertEquals("The value is not correct.",
234235
"[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value);
235236
}
237+
238+
@Test
239+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() throws Exception {
240+
OffsetDateTime date = OffsetDateTime.now(Z3);
241+
String value = MAPPER.writer()
242+
.with(TimeZone.getTimeZone(Z2))
243+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
244+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
245+
.with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
246+
.writeValueAsString(date);
247+
248+
// We expect to have the date written with the ZoneId Z2
249+
assertEquals("The value is incorrect", "\"" + FORMATTER.format(date.atZoneSameInstant(Z2)) + "\"", value);
250+
}
251+
252+
@Test
253+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() throws Exception {
254+
ZonedDateTime date = ZonedDateTime.now(Z3);
255+
String value = MAPPER.writer()
256+
.with(TimeZone.getTimeZone(Z2))
257+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
258+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
259+
.without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
260+
.writeValueAsString(date);
261+
262+
// We expect to have the date written with the ZoneId Z3
263+
assertEquals("The value is incorrect", "\"" + FORMATTER.format(date) + "\"", value);
264+
}
236265
}

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java

+73-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package com.fasterxml.jackson.datatype.jsr310.ser;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
1923
import java.time.Instant;
2024
import java.time.ZoneId;
2125
import java.time.ZonedDateTime;
@@ -26,24 +30,23 @@
2630
import java.util.Locale;
2731
import java.util.TimeZone;
2832

33+
import org.junit.Test;
34+
2935
import com.fasterxml.jackson.annotation.JsonFormat;
3036
import com.fasterxml.jackson.databind.DeserializationFeature;
3137
import com.fasterxml.jackson.databind.ObjectMapper;
3238
import com.fasterxml.jackson.databind.ObjectReader;
3339
import com.fasterxml.jackson.databind.SerializationFeature;
40+
import com.fasterxml.jackson.databind.module.SimpleModule;
3441
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
3542
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
3643
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
3744

38-
import org.junit.Test;
39-
40-
import static org.junit.Assert.assertEquals;
41-
import static org.junit.Assert.assertNotNull;
42-
import static org.junit.Assert.assertTrue;
43-
4445
public class ZonedDateTimeSerTest
4546
extends ModuleTestBase
4647
{
48+
private static final DateTimeFormatter FORMATTER_WITHOUT_ZONEID = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
49+
4750
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
4851

4952
private static final ZoneId Z1 = ZoneId.of("America/Chicago");
@@ -277,6 +280,70 @@ public void testSerializationAsStringWithZoneIdOn() throws Exception {
277280
assertEquals("The value is incorrect.", "\"" + DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date) + "\"", value);
278281
}
279282

283+
@Test
284+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOnAndACustomFormatter() throws Exception {
285+
ZonedDateTime date = ZonedDateTime.now(Z3);
286+
// With a custom DateTimeFormatter without a ZoneId.
287+
String value = newMapperBuilder().addModule(
288+
new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID)))
289+
.build()
290+
.writer()
291+
.with(TimeZone.getTimeZone(Z2))
292+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
293+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
294+
.with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
295+
.writeValueAsString(date);
296+
297+
// We expect to have the date written with the datetime of ZoneId Z2
298+
assertEquals("The value is incorrect", "\"" + date.withZoneSameInstant(Z2).format(FORMATTER_WITHOUT_ZONEID) + "\"", value);
299+
}
300+
301+
@Test
302+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOffAndACustomFormatter() throws Exception {
303+
ZonedDateTime date = ZonedDateTime.now(Z3);
304+
// With a custom DateTimeFormatter without a Zone.
305+
String value = newMapperBuilder().addModule(
306+
new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID)))
307+
.build()
308+
.writer()
309+
.with(TimeZone.getTimeZone(Z2))
310+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
311+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
312+
.without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
313+
.writeValueAsString(date);
314+
315+
// We expect to have the date written with the datetime of ZoneId Z3
316+
assertEquals("The value is incorrect", "\"" + date.format(FORMATTER_WITHOUT_ZONEID) + "\"", value);
317+
}
318+
319+
@Test
320+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() throws Exception {
321+
ZonedDateTime date = ZonedDateTime.now(Z3);
322+
String value = MAPPER.writer()
323+
.with(TimeZone.getTimeZone(Z2))
324+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
325+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
326+
.with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
327+
.writeValueAsString(date);
328+
329+
// We expect to have the date written with the ZoneId Z2
330+
assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.withZoneSameInstant(Z2)) + "\"", value);
331+
}
332+
333+
@Test
334+
public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() throws Exception {
335+
ZonedDateTime date = ZonedDateTime.now(Z3);
336+
String value = MAPPER.writer()
337+
.with(TimeZone.getTimeZone(Z2))
338+
.without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
339+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
340+
.without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
341+
.writeValueAsString(date);
342+
343+
// We expect to have the date written with the ZoneId Z3
344+
assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date) + "\"", value);
345+
}
346+
280347
@Test
281348
public void testSerializationWithTypeInfo01() throws Exception
282349
{

0 commit comments

Comments
 (0)