Skip to content

Commit 0a7631c

Browse files
committed
Fix InstantSerializer ignoring the JsonFormat shape
1 parent ed038ad commit 0a7631c

File tree

7 files changed

+76
-15
lines changed

7 files changed

+76
-15
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ protected InstantSerializer() {
4040
null);
4141
}
4242

43+
@Deprecated // For binary backward compat
4344
protected InstantSerializer(InstantSerializer base,
4445
Boolean useTimestamp, DateTimeFormatter formatter) {
45-
this(base, useTimestamp, null, formatter);
46+
this(base, useTimestamp, base._useNanoseconds, formatter);
47+
}
48+
49+
protected InstantSerializer(InstantSerializer base, Boolean useTimestamp,
50+
DateTimeFormatter formatter, JsonFormat.Shape shape) {
51+
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
4652
}
4753

4854
protected InstantSerializer(InstantSerializer base,
@@ -53,7 +59,7 @@ protected InstantSerializer(InstantSerializer base,
5359
@Override
5460
protected JSR310FormattedSerializerBase<Instant> withFormat(Boolean useTimestamp,
5561
DateTimeFormatter formatter, JsonFormat.Shape shape) {
56-
return new InstantSerializer(this, useTimestamp, formatter);
62+
return new InstantSerializer(this, useTimestamp, formatter, shape);
5763
}
5864

5965
@Override

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpo
7070
protected InstantSerializerBase(InstantSerializerBase<T> base,
7171
Boolean useTimestamp, DateTimeFormatter dtf)
7272
{
73-
this(base, useTimestamp, null, dtf);
73+
this(base, useTimestamp, base._useNanoseconds, dtf);
7474
}
7575

7676
protected InstantSerializerBase(InstantSerializerBase<T> base,
7777
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter dtf)
7878
{
79-
super(base, useTimestamp, useNanoseconds, dtf, null);
79+
this(base, useTimestamp, useNanoseconds, dtf, base._shape);
80+
}
81+
82+
protected InstantSerializerBase(InstantSerializerBase<T> base, Boolean useTimestamp,
83+
Boolean useNanoseconds, DateTimeFormatter dtf, JsonFormat.Shape shape) {
84+
super(base, useTimestamp, useNanoseconds, dtf, shape);
8085
defaultFormat = base.defaultFormat;
8186
getEpochMillis = base.getEpochMillis;
8287
getEpochSeconds = base.getEpochSeconds;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@ protected OffsetDateTimeSerializer() {
1616
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
1717
}
1818

19+
@Deprecated // for binary backward compat
1920
protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base,
2021
Boolean useTimestamp, DateTimeFormatter formatter) {
21-
this(base, useTimestamp, null, formatter);
22+
this(base, useTimestamp, base._useNanoseconds, formatter);
2223
}
2324

2425
protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base,
2526
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter) {
2627
super(base, useTimestamp, useNanoseconds, formatter);
2728
}
2829

30+
public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimestamp,
31+
DateTimeFormatter formatter, JsonFormat.Shape shape) {
32+
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
33+
}
34+
2935
@Override
3036
protected JSR310FormattedSerializerBase<?> withFormat(Boolean useTimestamp,
3137
DateTimeFormatter formatter, JsonFormat.Shape shape)
3238
{
33-
return new OffsetDateTimeSerializer(this, useTimestamp, formatter);
39+
return new OffsetDateTimeSerializer(this, useTimestamp, formatter, shape);
3440
}
3541

3642
@Override

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@ public ZonedDateTimeSerializer(DateTimeFormatter formatter) {
3636

3737
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
3838
Boolean useTimestamp, DateTimeFormatter formatter, Boolean writeZoneId) {
39-
this(base, useTimestamp, null, formatter, writeZoneId);
39+
this(base, useTimestamp, base._useNanoseconds, formatter, base._shape, writeZoneId);
4040
}
4141

42+
@Deprecated // for binary backward compat
4243
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
4344
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter,
4445
Boolean writeZoneId) {
45-
super(base, useTimestamp, useNanoseconds, formatter);
46+
this(base, useTimestamp, useNanoseconds, formatter, base._shape, writeZoneId);
47+
}
48+
49+
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
50+
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter,
51+
JsonFormat.Shape shape, Boolean writeZoneId) {
52+
super(base, useTimestamp, useNanoseconds, formatter, shape);
4653
_writeZoneId = writeZoneId;
4754
}
4855

@@ -51,7 +58,8 @@ protected JSR310FormattedSerializerBase<?> withFormat(
5158
Boolean useTimestamp,
5259
DateTimeFormatter formatter,
5360
JsonFormat.Shape shape) {
54-
return new ZonedDateTimeSerializer(this, useTimestamp, formatter, _writeZoneId);
61+
return new ZonedDateTimeSerializer(this, useTimestamp, _useNanoseconds, formatter,
62+
shape, _writeZoneId);
5563
}
5664

5765
@Override

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
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+
22+
import com.fasterxml.jackson.annotation.JsonFormat;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
1924
import com.fasterxml.jackson.databind.ObjectMapper;
2025
import com.fasterxml.jackson.databind.SerializationFeature;
2126
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
2227
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
2328
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
24-
25-
import org.junit.Test;
26-
2729
import java.time.Instant;
2830
import java.time.format.DateTimeFormatter;
2931
import java.time.temporal.Temporal;
30-
31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertNotNull;
32+
import org.junit.Test;
3333

3434
public class InstantSerTest extends ModuleTestBase
3535
{
@@ -171,5 +171,15 @@ public void testSerializationWithTypeInfo03() throws Exception
171171
"[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value);
172172
}
173173

174+
private static class Pojo1 {
175+
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
176+
public Instant t1 = Instant.parse("2022-04-27T12:00:00Z");
177+
public Instant t2 = t1;
178+
}
174179

180+
@Test
181+
public void testShapeInt() throws JsonProcessingException {
182+
String json1 = newMapper().writeValueAsString(new Pojo1());
183+
assertEquals("{\"t1\":1651060800000,\"t2\":1651060800.000000000}", json1);
184+
}
175185
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import com.fasterxml.jackson.core.JsonProcessingException;
56
import java.time.Instant;
67
import java.time.OffsetDateTime;
78
import java.time.ZoneId;
@@ -259,4 +260,16 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff()
259260
// We expect to have the date written with the ZoneId Z3
260261
assertEquals("The value is incorrect", "\"" + FORMATTER.format(date) + "\"", value);
261262
}
263+
264+
private static class Pojo1 {
265+
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
266+
public OffsetDateTime t1 = OffsetDateTime.parse("2022-04-27T12:00:00+02:00");
267+
public OffsetDateTime t2 = t1;
268+
}
269+
270+
@Test
271+
public void testShapeInt() throws JsonProcessingException {
272+
String json1 = newMapper().writeValueAsString(new Pojo1());
273+
assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1);
274+
}
262275
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertNotNull;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import com.fasterxml.jackson.core.JsonProcessingException;
2324
import java.time.Instant;
2425
import java.time.ZoneId;
2526
import java.time.ZonedDateTime;
@@ -947,6 +948,18 @@ public void testInstantPriorToEpochIsEqual() throws Exception
947948
assertEquals(original, deserialized);
948949
}
949950

951+
private static class Pojo1 {
952+
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
953+
public ZonedDateTime t1 = ZonedDateTime.parse("2022-04-27T12:00:00+02:00[Europe/Paris]");
954+
public ZonedDateTime t2 = t1;
955+
}
956+
957+
@Test
958+
public void testShapeInt() throws JsonProcessingException {
959+
String json1 = newMapper().writeValueAsString(new Pojo1());
960+
assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1);
961+
}
962+
950963
private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual)
951964
{
952965
assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.",

0 commit comments

Comments
 (0)