Skip to content

Commit 9f0d023

Browse files
authored
Fix InstantSerializer ignoring the JsonFormat shape (#242)
1 parent 29fa5d5 commit 9f0d023

File tree

7 files changed

+88
-15
lines changed

7 files changed

+88
-15
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,18 @@ protected InstantSerializer() {
4040
null);
4141
}
4242

43+
@Deprecated // since 2.14
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+
/**
50+
* @since 2.14
51+
*/
52+
protected InstantSerializer(InstantSerializer base, Boolean useTimestamp,
53+
DateTimeFormatter formatter, JsonFormat.Shape shape) {
54+
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
4655
}
4756

4857
protected InstantSerializer(InstantSerializer base,
@@ -53,7 +62,7 @@ protected InstantSerializer(InstantSerializer base,
5362
@Override
5463
protected JSR310FormattedSerializerBase<Instant> withFormat(Boolean useTimestamp,
5564
DateTimeFormatter formatter, JsonFormat.Shape shape) {
56-
return new InstantSerializer(this, useTimestamp, formatter);
65+
return new InstantSerializer(this, useTimestamp, formatter, shape);
5766
}
5867

5968
@Override

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ 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+
/**
83+
* @since 2.14
84+
*/
85+
protected InstantSerializerBase(InstantSerializerBase<T> base, Boolean useTimestamp,
86+
Boolean useNanoseconds, DateTimeFormatter dtf, JsonFormat.Shape shape) {
87+
super(base, useTimestamp, useNanoseconds, dtf, shape);
8088
defaultFormat = base.defaultFormat;
8189
getEpochMillis = base.getEpochMillis;
8290
getEpochSeconds = base.getEpochSeconds;

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,30 @@ protected OffsetDateTimeSerializer() {
1616
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
1717
}
1818

19+
@Deprecated // since 2.14
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+
/**
31+
* @since 2.14
32+
*/
33+
public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimestamp,
34+
DateTimeFormatter formatter, JsonFormat.Shape shape) {
35+
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
36+
}
37+
2938
@Override
3039
protected JSR310FormattedSerializerBase<?> withFormat(Boolean useTimestamp,
3140
DateTimeFormatter formatter, JsonFormat.Shape shape)
3241
{
33-
return new OffsetDateTimeSerializer(this, useTimestamp, formatter);
42+
return new OffsetDateTimeSerializer(this, useTimestamp, formatter, shape);
3443
}
3544

3645
@Override

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,23 @@ 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 // since 2.14
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+
/**
50+
* @since 2.14
51+
*/
52+
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
53+
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter,
54+
JsonFormat.Shape shape, Boolean writeZoneId) {
55+
super(base, useTimestamp, useNanoseconds, formatter, shape);
4656
_writeZoneId = writeZoneId;
4757
}
4858

@@ -51,7 +61,8 @@ protected JSR310FormattedSerializerBase<?> withFormat(
5161
Boolean useTimestamp,
5262
DateTimeFormatter formatter,
5363
JsonFormat.Shape shape) {
54-
return new ZonedDateTimeSerializer(this, useTimestamp, formatter, _writeZoneId);
64+
return new ZonedDateTimeSerializer(this, useTimestamp, _useNanoseconds, formatter,
65+
shape, _writeZoneId);
5566
}
5667

5768
@Override

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

+16-6
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

+13
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

+13
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)