forked from FasterXML/jackson-datatype-joda
/
JacksonJodaDateFormat.java
328 lines (288 loc) · 10.6 KB
/
JacksonJodaDateFormat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.fasterxml.jackson.datatype.joda.cfg;
import java.util.Locale;
import java.util.TimeZone;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* Simple container used to encapsulate (some of) gory details of
* customizations related to date/time formatting.
*/
public class JacksonJodaDateFormat extends JacksonJodaFormatBase
{
private final static String JODA_STYLE_CHARS = "SMLF-";
protected final DateTimeFormatter _formatter;
protected final DateTimeFormatter _formatterWithOffsetParsed;
protected final TimeZone _jdkTimezone;
protected transient DateTimeZone _jodaTimezone;
protected final boolean _explicitTimezone;
/**
* Flag for <code>JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE</code>
*
* @since 2.8
*/
protected final Boolean _adjustToContextTZOverride;
/**
* Flag for <code>JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID</code>
*
* @since 2.8
*/
protected final Boolean _writeZoneId;
public JacksonJodaDateFormat(DateTimeFormatter defaultFormatter)
{
super();
_formatter = defaultFormatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
DateTimeZone tz = defaultFormatter.getZone();
_jdkTimezone = (tz == null) ? null : tz.toTimeZone();
_explicitTimezone = false;
_adjustToContextTZOverride = null;
_writeZoneId = null;
}
public JacksonJodaDateFormat(JacksonJodaDateFormat base,
Boolean useTimestamp)
{
super(base, useTimestamp);
_formatter = base._formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
_writeZoneId = base._writeZoneId;
}
public JacksonJodaDateFormat(JacksonJodaDateFormat base,
DateTimeFormatter formatter)
{
super(base);
_formatter = formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
_writeZoneId = base._writeZoneId;
}
public JacksonJodaDateFormat(JacksonJodaDateFormat base, TimeZone jdkTimezone)
{
super(base, jdkTimezone);
_formatter = base._formatter.withZone(DateTimeZone.forTimeZone(jdkTimezone));
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = jdkTimezone;
_explicitTimezone = true;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
_writeZoneId = base._writeZoneId;
}
public JacksonJodaDateFormat(JacksonJodaDateFormat base, Locale locale)
{
super(base, locale);
_formatter = base._formatter.withLocale(locale);
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
_writeZoneId = base._writeZoneId;
}
/**
* @since 2.8
*/
protected JacksonJodaDateFormat(JacksonJodaDateFormat base,
Boolean adjustToContextTZOverride, Boolean writeZoneId)
{
super(base);
_formatter = base._formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = adjustToContextTZOverride;
_writeZoneId = writeZoneId;
}
/*
/**********************************************************
/* Factory methods
/**********************************************************
*/
public JacksonJodaDateFormat with(JsonFormat.Value ann) {
JacksonJodaDateFormat format = this;
format = format.withLocale(ann.getLocale());
format = format.withTimeZone(ann.getTimeZone());
format = format.withFormat(ann.getPattern());
Boolean adjustTZ = ann.getFeature(JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
Boolean writeZoneId = ann.getFeature(JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID);
if ((adjustTZ != _adjustToContextTZOverride)
|| (writeZoneId != _writeZoneId)) {
format = new JacksonJodaDateFormat(format, adjustTZ, writeZoneId);
}
return format;
}
public JacksonJodaDateFormat withUseTimestamp(Boolean useTimestamp) {
if ((_useTimestamp != null) && _useTimestamp.equals(useTimestamp)) {
return this;
}
return new JacksonJodaDateFormat(this, useTimestamp);
}
public JacksonJodaDateFormat withFormat(String format) {
if (format == null || format.isEmpty()) {
return this;
}
DateTimeFormatter formatter;
if (_isStyle(format)) {
formatter = DateTimeFormat.forStyle(format);
} else {
formatter = DateTimeFormat.forPattern(format);
}
if (_locale != null) {
formatter = formatter.withLocale(_locale);
}
return new JacksonJodaDateFormat(this, formatter);
}
public JacksonJodaDateFormat withTimeZone(TimeZone tz) {
if ((tz == null) || (_jdkTimezone != null && _jdkTimezone.equals(tz))) {
return this;
}
return new JacksonJodaDateFormat(this, tz);
}
public JacksonJodaDateFormat withLocale(Locale locale) {
if ((locale == null) || (_locale != null && _locale.equals(locale))) {
return this;
}
return new JacksonJodaDateFormat(this, locale);
}
/**
* @since 2.8
*/
public JacksonJodaDateFormat withAdjustToContextTZOverride(Boolean adjustToContextTZOverride) {
// minor efficiency check to avoid recreation if no change:
if (adjustToContextTZOverride == _adjustToContextTZOverride) {
return this;
}
return new JacksonJodaDateFormat(this, adjustToContextTZOverride, _writeZoneId);
}
/**
* @since 2.8
*/
public JacksonJodaDateFormat withWriteZoneId(Boolean writeZoneId) {
// minor efficiency check to avoid recreation if no change:
if (writeZoneId == _writeZoneId) {
return this;
}
return new JacksonJodaDateFormat(this, _adjustToContextTZOverride, writeZoneId);
}
/*
/**********************************************************
/* Accessors
/**********************************************************
*/
/**
* @since 2.6
*/
public DateTimeZone getTimeZone() {
if (_jodaTimezone != null) {
return _jodaTimezone;
}
if (_jdkTimezone == null) {
return null;
}
DateTimeZone tz = DateTimeZone.forTimeZone(_jdkTimezone);
_jodaTimezone = tz;
return tz;
}
public Locale getLocale() {
return _locale;
}
/*
/**********************************************************
/* Factory methods for other types
/**********************************************************
*/
public DateTimeFormatter rawFormatter() {
return _formatter;
}
public DateTimeFormatter createFormatter(SerializerProvider ctxt)
{
DateTimeFormatter formatter = createFormatterWithLocale(ctxt);
if (!_explicitTimezone) {
TimeZone tz = ctxt.getTimeZone();
if ((tz != null) && !tz.equals(_jdkTimezone)) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
}
}
return formatter;
}
public DateTimeFormatter createFormatterWithLocale(SerializerProvider ctxt)
{
DateTimeFormatter formatter = _formatter;
if (!_explicitLocale) {
Locale loc = ctxt.getLocale();
if (loc != null && !loc.equals(_locale)) {
formatter = formatter.withLocale(loc);
}
}
return formatter;
}
/**
* Accessor used during deserialization.
*/
public DateTimeFormatter createParser(DeserializationContext ctxt)
{
DateTimeFormatter formatter = _formatter;
if (!_explicitTimezone) {
if (shouldAdjustToContextTimeZone(ctxt)) {
TimeZone tz = ctxt.getTimeZone();
if (tz != null && !tz.equals(_jdkTimezone)) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
}
} else {
formatter = _formatterWithOffsetParsed;
}
}
if (!_explicitLocale) {
Locale loc = ctxt.getLocale();
if (loc != null && !loc.equals(_locale)) {
formatter = formatter.withLocale(loc);
}
}
return formatter;
}
/**
* @since 2.8
*/
public boolean shouldAdjustToContextTimeZone(DeserializationContext ctxt) {
return (_adjustToContextTZOverride != null) ? _adjustToContextTZOverride :
ctxt.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}
/**
* @since 2.8
*/
public boolean shouldWriteWithZoneId(SerializerProvider ctxt) {
return (_writeZoneId != null) ? _writeZoneId :
ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
}
/**
* Differentiate if TimeZone is specified by caller
* @return true if TimeZone is specified by caller; false otherwise.
*/
public boolean isTimezoneExplicit() {
return _explicitTimezone;
}
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
protected static boolean _isStyle(String formatStr) {
if (formatStr.length() != 2) {
return false;
}
return (JODA_STYLE_CHARS.indexOf(formatStr.charAt(0)) >= 0)
&& (JODA_STYLE_CHARS.indexOf(formatStr.charAt(0)) >= 0);
}
@Override
public String toString() {
return String.format("[JacksonJodaFormat, explicitTZ? %s, JDK tz = %s, formatter = %s]",
_explicitTimezone, _jdkTimezone.getID(), _formatter);
}
}