66import java .time .ZoneOffset ;
77import java .time .ZonedDateTime ;
88import java .time .format .DateTimeFormatter ;
9+ import java .time .format .DateTimeFormatterBuilder ;
910import java .time .temporal .Temporal ;
1011import java .util .TimeZone ;
1112
1415import com .fasterxml .jackson .annotation .JsonFormat ;
1516import com .fasterxml .jackson .databind .ObjectMapper ;
1617import com .fasterxml .jackson .databind .SerializationFeature ;
17- import com .fasterxml .jackson .databind .json . JsonMapper ;
18+ import com .fasterxml .jackson .databind .module . SimpleModule ;
1819import com .fasterxml .jackson .datatype .jsr310 .DecimalUtils ;
1920import com .fasterxml .jackson .datatype .jsr310 .MockObjectConfiguration ;
2021import 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