|
3 | 3 | import java.time.Instant; |
4 | 4 | import java.time.OffsetDateTime; |
5 | 5 | import java.time.ZoneId; |
| 6 | +import java.time.ZoneOffset; |
6 | 7 | import java.time.ZonedDateTime; |
7 | 8 | import java.time.format.DateTimeFormatter; |
8 | 9 | import java.time.temporal.Temporal; |
|
13 | 14 | import com.fasterxml.jackson.annotation.JsonFormat; |
14 | 15 | import com.fasterxml.jackson.databind.ObjectMapper; |
15 | 16 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 17 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
16 | 18 | import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; |
17 | 19 | import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
18 | 20 | import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
@@ -284,4 +286,122 @@ public void testShapeInt() throws Exception { |
284 | 286 | String json1 = newMapper().writeValueAsString(new Pojo1()); |
285 | 287 | assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1); |
286 | 288 | } |
| 289 | + |
| 290 | + /* |
| 291 | + /********************************************************** |
| 292 | + /* Tests for custom formatter (#376) |
| 293 | + /********************************************************** |
| 294 | + */ |
| 295 | + |
| 296 | + @Test |
| 297 | + public void testSerializationWithCustomFormatter() throws Exception |
| 298 | + { |
| 299 | + // Create a custom formatter that displays only 3 digits of nano-seconds instead of 9 |
| 300 | + // Use ISO_LOCAL_DATE and ISO_LOCAL_TIME separately to control nanosecond precision |
| 301 | + DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder() |
| 302 | + .append(DateTimeFormatter.ISO_LOCAL_DATE) |
| 303 | + .appendLiteral('T') |
| 304 | + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) |
| 305 | + .appendLiteral(':') |
| 306 | + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) |
| 307 | + .optionalStart() |
| 308 | + .appendLiteral(':') |
| 309 | + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) |
| 310 | + .optionalStart() |
| 311 | + .appendFraction(java.time.temporal.ChronoField.NANO_OF_SECOND, 3, 3, true) |
| 312 | + .optionalEnd() |
| 313 | + .optionalEnd() |
| 314 | + .appendOffsetId() |
| 315 | + .toFormatter(); |
| 316 | + |
| 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 | + |
| 328 | + // Create a date with nanoseconds (123456789 nanos = .123456789 seconds) |
| 329 | + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC); |
| 330 | + String json = mapper.writeValueAsString(date); |
| 331 | + |
| 332 | + // Should output with only 3 digits of nano precision (.123 instead of .123456789) |
| 333 | + assertEquals(q("2025-01-01T22:01:05.123Z"), json); |
| 334 | + } |
| 335 | + |
| 336 | + @Test |
| 337 | + public void testSerializationWithCustomFormatterNoNanos() throws Exception |
| 338 | + { |
| 339 | + // Create a formatter without nanoseconds |
| 340 | + DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder() |
| 341 | + .append(DateTimeFormatter.ISO_LOCAL_DATE) |
| 342 | + .appendLiteral('T') |
| 343 | + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) |
| 344 | + .appendLiteral(':') |
| 345 | + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) |
| 346 | + .optionalStart() |
| 347 | + .appendLiteral(':') |
| 348 | + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) |
| 349 | + .optionalEnd() |
| 350 | + .appendOffsetId() |
| 351 | + .toFormatter(); |
| 352 | + |
| 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 | + |
| 363 | + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC); |
| 364 | + String json = mapper.writeValueAsString(date); |
| 365 | + |
| 366 | + // Should output without nanoseconds |
| 367 | + assertEquals(q("2025-01-01T22:01:05Z"), json); |
| 368 | + } |
| 369 | + |
| 370 | + @Test |
| 371 | + public void testSerializationWithCustomFormatterAndOffset() throws Exception |
| 372 | + { |
| 373 | + // Create a custom formatter that displays only 3 digits of nano-seconds |
| 374 | + DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder() |
| 375 | + .append(DateTimeFormatter.ISO_LOCAL_DATE) |
| 376 | + .appendLiteral('T') |
| 377 | + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) |
| 378 | + .appendLiteral(':') |
| 379 | + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) |
| 380 | + .optionalStart() |
| 381 | + .appendLiteral(':') |
| 382 | + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) |
| 383 | + .optionalStart() |
| 384 | + .appendFraction(java.time.temporal.ChronoField.NANO_OF_SECOND, 3, 3, true) |
| 385 | + .optionalEnd() |
| 386 | + .optionalEnd() |
| 387 | + .appendOffsetId() |
| 388 | + .toFormatter(); |
| 389 | + |
| 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(); |
| 399 | + |
| 400 | + // Create a date with a non-UTC offset |
| 401 | + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.ofHours(5)); |
| 402 | + String json = mapper.writeValueAsString(date); |
| 403 | + |
| 404 | + // Should output with offset +05:00 and 3 digits of nano precision |
| 405 | + assertEquals(q("2025-01-01T22:01:05.123+05:00"), json); |
| 406 | + } |
287 | 407 | } |
0 commit comments