1
+ package com .fasterxml .jackson .datatype .joda .ser ;
2
+
3
+ import java .util .TimeZone ;
4
+
5
+ import org .joda .time .DateTime ;
6
+ import org .joda .time .DateTimeZone ;
7
+ import org .joda .time .LocalDate ;
8
+ import org .joda .time .LocalDateTime ;
9
+ import org .joda .time .LocalTime ;
10
+ import org .junit .jupiter .api .Test ;
11
+
12
+ import com .fasterxml .jackson .annotation .JsonFormat ;
13
+ import com .fasterxml .jackson .databind .ObjectMapper ;
14
+ import com .fasterxml .jackson .databind .SerializationFeature ;
15
+ import com .fasterxml .jackson .datatype .joda .JodaTestBase ;
16
+
17
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
18
+
19
+ public class JsonFormatTimeZoneWithPattern98Test extends JodaTestBase {
20
+ static class Wrapper3Z <T > {
21
+ @ JsonFormat (
22
+ shape = JsonFormat .Shape .STRING ,
23
+ pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZZ" ,
24
+ timezone = "Europe/Budapest" // +01:00 in winter
25
+ )
26
+ public T value ;
27
+ Wrapper3Z (T v ) { value = v ; }
28
+ }
29
+
30
+ static class Wrapper2Z <T > {
31
+ @ JsonFormat (
32
+ shape = JsonFormat .Shape .STRING ,
33
+ pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZ" ,
34
+ timezone = "Europe/Budapest"
35
+ )
36
+ public T value ;
37
+ Wrapper2Z (T v ) { value = v ; }
38
+ }
39
+
40
+ static class Wrapper1Z <T > {
41
+ @ JsonFormat (
42
+ shape = JsonFormat .Shape .STRING ,
43
+ pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS Z" ,
44
+ timezone = "Europe/Budapest" // +01:00 in winter
45
+ )
46
+ public T value ;
47
+ Wrapper1Z (T v ) { value = v ; }
48
+ }
49
+
50
+ private final ObjectMapper MAPPER = mapperWithModuleBuilder ()
51
+ .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS )
52
+ .build ();
53
+
54
+ @ Test
55
+ public void patternShouldNotEraseTimeZone ()
56
+ throws Exception
57
+ {
58
+ // DateTime already in Europe/Budapest zone (no shift)
59
+ _testSerialization (
60
+ "{\" value\" :\" 2018-01-01T12:01:02.003 Europe/Budapest\" }" ,
61
+ new Wrapper3Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
62
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )))));
63
+ // DateTime in UTC, should shift +1h
64
+ _testSerialization (
65
+ "{\" value\" :\" 2018-01-01T13:01:02.003 Europe/Budapest\" }" ,
66
+ new Wrapper3Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
67
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("UTC" )))));
68
+ // LocalDate
69
+ _testSerialization (
70
+ "{\" value\" :\" 2018-01-01T��:��:��.000 \" }" ,
71
+ new Wrapper3Z <>(new LocalDate (2018 ,1 ,1 )));
72
+ // LocalTime
73
+ _testSerialization (
74
+ "{\" value\" :\" ����-��-��T12:01:02.003 \" }" ,
75
+ new Wrapper3Z <>(new LocalTime (12 ,1 ,2 ,3 )));
76
+ // LocalDateTime
77
+ _testSerialization (
78
+ "{\" value\" :\" 2018-01-01T12:01:02.003 \" }" ,
79
+ new Wrapper3Z <>(new LocalDateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 )));
80
+ }
81
+
82
+ @ Test
83
+ public void patternShouldNotEraseTimeZoneWithZZ ()
84
+ throws Exception
85
+ {
86
+ // DateTime already in Europe/Budapest zone (no shift)
87
+ _testSerialization (
88
+ "{\" value\" :\" 2018-01-01T12:01:02.003 +01:00\" }" ,
89
+ new Wrapper2Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
90
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )))));
91
+ // DateTime in UTC, should shift +1h
92
+ _testSerialization (
93
+ "{\" value\" :\" 2018-01-01T13:01:02.003 +01:00\" }" ,
94
+ new Wrapper2Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
95
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("UTC" )))));
96
+ }
97
+
98
+ @ Test
99
+ public void patternShouldNotEraseTimeZoneWithZ ()
100
+ throws Exception
101
+ {
102
+ // DateTime already in Europe/Budapest zone (no shift)
103
+ _testSerialization (
104
+ "{\" value\" :\" 2018-01-01T12:01:02.003 +0100\" }" ,
105
+ new Wrapper1Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
106
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )))));
107
+ // DateTime in UTC, should shift +1h
108
+ _testSerialization (
109
+ "{\" value\" :\" 2018-01-01T13:01:02.003 +0100\" }" ,
110
+ new Wrapper1Z <>(new DateTime (2018 ,1 ,1 ,12 ,1 ,2 ,3 ,
111
+ DateTimeZone .forTimeZone (TimeZone .getTimeZone ("UTC" )))));
112
+ }
113
+
114
+ private void _testSerialization (String expectedJson , Object wrapper ) throws Exception {
115
+ String actual = MAPPER .writeValueAsString (wrapper );
116
+ assertEquals (expectedJson , actual );
117
+ }
118
+
119
+ }
0 commit comments