Skip to content

Commit 41753dd

Browse files
committed
Rewrite serializer side
1 parent e010dfb commit 41753dd

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,11 @@ public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimest
3636
}
3737

3838
/**
39-
* Constructor for creating a new serializer instance with a custom {@link DateTimeFormatter}.
40-
* This allows customization of the serialization output format, such as controlling nano-second precision
41-
* (e.g., 3 digits instead of 9).
42-
*
43-
* @param formatter Custom {@link DateTimeFormatter} to use for formatting
44-
* @since 2.19
39+
* @since 2.21
4540
*/
46-
public OffsetDateTimeSerializer(DateTimeFormatter formatter) {
47-
// Call the protected constructor with useTimestamp=false to ensure string serialization
48-
this(INSTANCE, false, formatter, JsonFormat.Shape.STRING);
41+
public OffsetDateTimeSerializer withFormatter(DateTimeFormatter formatter)
42+
{
43+
return new OffsetDateTimeSerializer(this, _useTimestamp, formatter, _shape);
4944
}
5045

5146
@Override

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

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.time.ZoneOffset;
77
import java.time.ZonedDateTime;
88
import java.time.format.DateTimeFormatter;
9+
import java.time.format.DateTimeFormatterBuilder;
910
import java.time.temporal.Temporal;
1011
import java.util.TimeZone;
1112

@@ -14,7 +15,7 @@
1415
import com.fasterxml.jackson.annotation.JsonFormat;
1516
import com.fasterxml.jackson.databind.ObjectMapper;
1617
import com.fasterxml.jackson.databind.SerializationFeature;
17-
import com.fasterxml.jackson.databind.json.JsonMapper;
18+
import com.fasterxml.jackson.databind.module.SimpleModule;
1819
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
1920
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
2021
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
@@ -298,7 +299,7 @@ public void testSerializationWithCustomFormatter() throws Exception
298299
{
299300
// Create a custom formatter that displays only 3 digits of nano-seconds instead of 9
300301
// Use ISO_LOCAL_DATE and ISO_LOCAL_TIME separately to control nanosecond precision
301-
DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder()
302+
DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
302303
.append(DateTimeFormatter.ISO_LOCAL_DATE)
303304
.appendLiteral('T')
304305
.appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2)
@@ -314,20 +315,9 @@ public void testSerializationWithCustomFormatter() throws Exception
314315
.appendOffsetId()
315316
.toFormatter();
316317

317-
OffsetDateTimeSerializer customSerializer = new OffsetDateTimeSerializer(customFormatter);
318-
319-
com.fasterxml.jackson.databind.module.SimpleModule customModule =
320-
new com.fasterxml.jackson.databind.module.SimpleModule("CustomOffsetDateTimeModule");
321-
customModule.addSerializer(OffsetDateTime.class, customSerializer);
322-
323-
// Add both JavaTimeModule and our custom module
324-
ObjectMapper mapper = mapperBuilder()
325-
.addModule(customModule)
326-
.build();
327-
328318
// Create a date with nanoseconds (123456789 nanos = .123456789 seconds)
329319
OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC);
330-
String json = mapper.writeValueAsString(date);
320+
String json = _mapper(customFormatter).writeValueAsString(date);
331321

332322
// Should output with only 3 digits of nano precision (.123 instead of .123456789)
333323
assertEquals(q("2025-01-01T22:01:05.123Z"), json);
@@ -337,7 +327,7 @@ public void testSerializationWithCustomFormatter() throws Exception
337327
public void testSerializationWithCustomFormatterNoNanos() throws Exception
338328
{
339329
// Create a formatter without nanoseconds
340-
DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder()
330+
DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
341331
.append(DateTimeFormatter.ISO_LOCAL_DATE)
342332
.appendLiteral('T')
343333
.appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2)
@@ -350,18 +340,8 @@ public void testSerializationWithCustomFormatterNoNanos() throws Exception
350340
.appendOffsetId()
351341
.toFormatter();
352342

353-
OffsetDateTimeSerializer customSerializer = new OffsetDateTimeSerializer(customFormatter);
354-
355-
com.fasterxml.jackson.databind.module.SimpleModule customModule =
356-
new com.fasterxml.jackson.databind.module.SimpleModule("CustomOffsetDateTimeModule");
357-
customModule.addSerializer(OffsetDateTime.class, customSerializer);
358-
359-
ObjectMapper mapper = mapperBuilder()
360-
.addModule(customModule)
361-
.build();
362-
363343
OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC);
364-
String json = mapper.writeValueAsString(date);
344+
String json = _mapper(customFormatter).writeValueAsString(date);
365345

366346
// Should output without nanoseconds
367347
assertEquals(q("2025-01-01T22:01:05Z"), json);
@@ -371,7 +351,7 @@ public void testSerializationWithCustomFormatterNoNanos() throws Exception
371351
public void testSerializationWithCustomFormatterAndOffset() throws Exception
372352
{
373353
// Create a custom formatter that displays only 3 digits of nano-seconds
374-
DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder()
354+
DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
375355
.append(DateTimeFormatter.ISO_LOCAL_DATE)
376356
.appendLiteral('T')
377357
.appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2)
@@ -387,21 +367,22 @@ public void testSerializationWithCustomFormatterAndOffset() throws Exception
387367
.appendOffsetId()
388368
.toFormatter();
389369

390-
OffsetDateTimeSerializer customSerializer = new OffsetDateTimeSerializer(customFormatter);
391-
392-
com.fasterxml.jackson.databind.module.SimpleModule customModule =
393-
new com.fasterxml.jackson.databind.module.SimpleModule("CustomOffsetDateTimeModule");
394-
customModule.addSerializer(OffsetDateTime.class, customSerializer);
395-
396-
ObjectMapper mapper = mapperBuilder()
397-
.addModule(customModule)
398-
.build();
399370

400371
// Create a date with a non-UTC offset
401372
OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.ofHours(5));
402-
String json = mapper.writeValueAsString(date);
373+
String json = _mapper(customFormatter).writeValueAsString(date);
403374

404375
// Should output with offset +05:00 and 3 digits of nano precision
405376
assertEquals(q("2025-01-01T22:01:05.123+05:00"), json);
406377
}
378+
379+
private ObjectMapper _mapper(DateTimeFormatter dtf) {
380+
OffsetDateTimeSerializer customSerializer = OffsetDateTimeSerializer.INSTANCE
381+
.withFormatter(dtf);
382+
SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule")
383+
.addSerializer(OffsetDateTime.class, customSerializer);
384+
return mapperBuilder()
385+
.addModule(customModule)
386+
.build();
387+
}
407388
}

0 commit comments

Comments
 (0)