21
21
import static org .junit .Assert .assertTrue ;
22
22
23
23
import java .time .Instant ;
24
+ import java .time .ZoneId ;
25
+ import java .time .ZonedDateTime ;
24
26
import java .time .format .DateTimeFormatter ;
25
27
import java .time .temporal .ChronoUnit ;
26
28
import java .time .temporal .Temporal ;
33
35
public class TestInstantSerialization extends ModuleTestBase
34
36
{
35
37
private static final DateTimeFormatter FORMATTER = DateTimeFormatter .ISO_INSTANT ;
38
+ private static final String CUSTOM_PATTERN = "yyyy-MM-dd HH:mm:ss" ;
36
39
37
40
private final ObjectMapper mapper = newMapper ();
38
41
@@ -44,12 +47,29 @@ final static class Wrapper {
44
47
* representation here
45
48
*/
46
49
//pattern="YYYY-mm-dd",
47
- shape =JsonFormat .Shape .STRING )
50
+ shape =JsonFormat .Shape .STRING
51
+ )
48
52
public Instant value ;
49
53
50
54
public Wrapper () { }
51
55
public Wrapper (Instant v ) { value = v ; }
52
56
}
57
+
58
+ final static class WrapperWithCustomPattern {
59
+ @ JsonFormat (
60
+ pattern = CUSTOM_PATTERN ,
61
+ shape =JsonFormat .Shape .STRING ,
62
+ timezone = "UTC"
63
+ )
64
+ public Instant valueInUTC ;
65
+
66
+ public WrapperWithCustomPattern () { }
67
+ public WrapperWithCustomPattern (Instant v ) {
68
+ valueInUTC = v ;
69
+ }
70
+ }
71
+
72
+
53
73
54
74
@ Test
55
75
public void testSerializationAsTimestamp01Nanoseconds () throws Exception
@@ -357,7 +377,7 @@ public void testDeserializationWithTypeInfo04() throws Exception
357
377
}
358
378
359
379
@ Test
360
- public void testCustomPatternWithAnnotations () throws Exception
380
+ public void testCustomPatternWithAnnotations01 () throws Exception
361
381
{
362
382
final Wrapper input = new Wrapper (Instant .ofEpochMilli (0 ));
363
383
String json = mapper .writeValueAsString (input );
@@ -367,6 +387,25 @@ public void testCustomPatternWithAnnotations() throws Exception
367
387
assertEquals (input .value , result .value );
368
388
}
369
389
390
+ // [datatype-jsr310#69]
391
+ @ Test
392
+ public void testCustomPatternWithAnnotations02 () throws Exception
393
+ {
394
+ //Test date is pushed one year after start of the epoch just to avoid possible issues with UTC-X TZs which could
395
+ //push the instant before tha start of the epoch
396
+ final Instant instant = ZonedDateTime .ofInstant (Instant .ofEpochMilli (0 ), ZoneId .of ("UTC" )).plusYears (1 ).toInstant ();
397
+ final DateTimeFormatter formatter = DateTimeFormatter .ofPattern (CUSTOM_PATTERN );
398
+ final String valueInUTC = formatter .withZone (ZoneId .of ("UTC" )).format (instant );
399
+
400
+ final WrapperWithCustomPattern input = new WrapperWithCustomPattern (instant );
401
+ String json = mapper .writeValueAsString (input );
402
+
403
+ assertTrue ("Instant in UTC timezone was not serialized as expected." , json .contains (aposToQuotes ("'valueInUTC':'" + valueInUTC + "'" )));
404
+
405
+ WrapperWithCustomPattern result = mapper .readValue (json , WrapperWithCustomPattern .class );
406
+ assertEquals ("Instant in UTC timezone was not deserialized as expected." , input .valueInUTC , result .valueInUTC );
407
+ }
408
+
370
409
// [datatype-jsr310#16]
371
410
@ Test
372
411
public void testDeserializationFromStringAsNumber () throws Exception
0 commit comments