/
JodaCodec.java
503 lines (444 loc) · 21 KB
/
JodaCodec.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package com.alibaba.fastjson.serializer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Locale;
import java.util.TimeZone;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.util.TypeUtils;
import org.joda.time.*;
import org.joda.time.format.*;
public class JodaCodec implements ObjectSerializer, ContextObjectSerializer, ObjectDeserializer {
public final static JodaCodec instance = new JodaCodec();
private final static String defaultPatttern = "yyyy-MM-dd HH:mm:ss";
private final static DateTimeFormatter defaultFormatter = DateTimeFormat.forPattern(defaultPatttern);
private final static DateTimeFormatter defaultFormatter_23 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
private final static DateTimeFormatter formatter_dt19_tw = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_cn = DateTimeFormat.forPattern("yyyy年M月d日 HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_cn_1 = DateTimeFormat.forPattern("yyyy年M月d日 H时m分s秒");
private final static DateTimeFormatter formatter_dt19_kr = DateTimeFormat.forPattern("yyyy년M월d일 HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_us = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_eur = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_de = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
private final static DateTimeFormatter formatter_dt19_in = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss");
private final static DateTimeFormatter formatter_d8 = DateTimeFormat.forPattern("yyyyMMdd");
private final static DateTimeFormatter formatter_d10_tw = DateTimeFormat.forPattern("yyyy/MM/dd");
private final static DateTimeFormatter formatter_d10_cn = DateTimeFormat.forPattern("yyyy年M月d日");
private final static DateTimeFormatter formatter_d10_kr = DateTimeFormat.forPattern("yyyy년M월d일");
private final static DateTimeFormatter formatter_d10_us = DateTimeFormat.forPattern("MM/dd/yyyy");
private final static DateTimeFormatter formatter_d10_eur = DateTimeFormat.forPattern("dd/MM/yyyy");
private final static DateTimeFormatter formatter_d10_de = DateTimeFormat.forPattern("dd.MM.yyyy");
private final static DateTimeFormatter formatter_d10_in = DateTimeFormat.forPattern("dd-MM-yyyy");
private final static DateTimeFormatter ISO_FIXED_FORMAT =
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(DateTimeZone.getDefault());
private final static String formatter_iso8601_pattern = "yyyy-MM-dd'T'HH:mm:ss";
private final static String formatter_iso8601_pattern_23 = "yyyy-MM-dd'T'HH:mm:ss.SSS";
private final static String formatter_iso8601_pattern_29 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS";
private final static DateTimeFormatter formatter_iso8601 = DateTimeFormat.forPattern(formatter_iso8601_pattern);
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
return deserialze(parser, type, fieldName, null, 0);
}
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int feature) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.NULL){
lexer.nextToken();
return null;
}
if (lexer.token() == JSONToken.LITERAL_STRING) {
String text = lexer.stringVal();
lexer.nextToken();
DateTimeFormatter formatter = null;
if (format != null) {
if (defaultPatttern.equals(format)) {
formatter = defaultFormatter;
} else {
formatter = DateTimeFormat.forPattern(format);
}
}
if ("".equals(text)) {
return null;
}
if (type == LocalDateTime.class) {
LocalDateTime localDateTime;
if (text.length() == 10 || text.length() == 8) {
LocalDate localDate = parseLocalDate(text, format, formatter);
localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);
} else {
localDateTime = parseDateTime(text, formatter);
}
return (T) localDateTime;
} else if (type == LocalDate.class) {
LocalDate localDate;
if (text.length() == 23) {
LocalDateTime localDateTime = LocalDateTime.parse(text);
localDate = localDateTime.toLocalDate();
} else {
localDate = parseLocalDate(text, format, formatter);
}
return (T) localDate;
} else if (type == LocalTime.class) {
LocalTime localDate;
if (text.length() == 23) {
LocalDateTime localDateTime = LocalDateTime.parse(text);
localDate = localDateTime.toLocalTime();
} else {
localDate = LocalTime.parse(text);
}
return (T) localDate;
} else if (type == DateTime.class) {
if (formatter == defaultFormatter) {
formatter = ISO_FIXED_FORMAT;
}
DateTime zonedDateTime = parseZonedDateTime(text, formatter);
return (T) zonedDateTime;
} else if (type == DateTimeZone.class) {
DateTimeZone offsetTime = DateTimeZone.forID(text);
return (T) offsetTime;
} else if (type == Period.class) {
Period period = Period.parse(text);
return (T) period;
} else if (type == Duration.class) {
Duration duration = Duration.parse(text);
return (T) duration;
} else if (type == Instant.class) {
boolean digit = true;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit && text.length() > 8 && text.length() < 19) {
long epochMillis = Long.parseLong(text);
return (T) new Instant(epochMillis);
}
Instant instant = Instant.parse(text);
return (T) instant;
} else if (type == DateTimeFormatter.class) {
return (T) DateTimeFormat.forPattern(text);
}
} else if (lexer.token() == JSONToken.LITERAL_INT) {
long millis = lexer.longValue();
lexer.nextToken();
TimeZone timeZone = JSON.defaultTimeZone;
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
if (type == DateTime.class) {
return (T) new DateTime(millis, DateTimeZone.forTimeZone(timeZone));
}
LocalDateTime localDateTime = new LocalDateTime(millis, DateTimeZone.forTimeZone(timeZone));
if (type == LocalDateTime.class) {
return (T) localDateTime;
}
if (type == LocalDate.class) {
return (T) localDateTime.toLocalDate();
}
if (type == LocalTime.class) {
return (T) localDateTime.toLocalTime();
}
if (type == Instant.class) {
Instant instant = new Instant(millis);
return (T) instant;
}
throw new UnsupportedOperationException();
} else if (lexer.token() == JSONToken.LBRACE) {
JSONObject object = parser.parseObject();
if (type == Instant.class) {
Object epochSecond = object.get("epochSecond");
if (epochSecond instanceof Number) {
return (T) Instant.ofEpochSecond(
TypeUtils.longExtractValue((Number) epochSecond));
}
Object millis = object.get("millis");
if (millis instanceof Number) {
return (T) Instant.ofEpochMilli(
TypeUtils.longExtractValue((Number) millis));
}
}
} else {
throw new UnsupportedOperationException();
}
return null;
}
protected LocalDateTime parseDateTime(String text, DateTimeFormatter formatter) {
if (formatter == null) {
if (text.length() == 19) {
char c4 = text.charAt(4);
char c7 = text.charAt(7);
char c10 = text.charAt(10);
char c13 = text.charAt(13);
char c16 = text.charAt(16);
if (c13 == ':' && c16 == ':') {
if (c4 == '-' && c7 == '-') { // yyyy-MM-dd or yyyy-MM-dd'T'
if (c10 == 'T') {
formatter = formatter_iso8601;
} else if (c10 == ' ') {
formatter = defaultFormatter;
}
} else if (c4 == '/' && c7 == '/') { // tw yyyy/mm/dd
formatter = formatter_dt19_tw;
} else {
char c0 = text.charAt(0);
char c1 = text.charAt(1);
char c2 = text.charAt(2);
char c3 = text.charAt(3);
char c5 = text.charAt(5);
if (c2 == '/' && c5 == '/') { // mm/dd/yyyy or mm/dd/yyyy
int v0 = (c0 - '0') * 10 + (c1 - '0');
int v1 = (c3 - '0') * 10 + (c4 - '0');
if (v0 > 12) {
formatter = formatter_dt19_eur;
} else if (v1 > 12) {
formatter = formatter_dt19_us;
} else {
String country = Locale.getDefault().getCountry();
if (country.equals("US")) {
formatter = formatter_dt19_us;
} else if (country.equals("BR") //
|| country.equals("AU")) {
formatter = formatter_dt19_eur;
}
}
} else if (c2 == '.' && c5 == '.') { // dd.mm.yyyy
formatter = formatter_dt19_de;
} else if (c2 == '-' && c5 == '-') { // dd-mm-yyyy
formatter = formatter_dt19_in;
}
}
}
} else if (text.length() == 23) {
char c4 = text.charAt(4);
char c7 = text.charAt(7);
char c10 = text.charAt(10);
char c13 = text.charAt(13);
char c16 = text.charAt(16);
char c19 = text.charAt(19);
if (c13 == ':'
&& c16 == ':'
&& c4 == '-'
&& c7 == '-'
&& c10 == ' '
&& c19 == '.'
) {
formatter = defaultFormatter_23;
}
}
if (text.length() >= 17) {
char c4 = text.charAt(4);
if (c4 == '年') {
if (text.charAt(text.length() - 1) == '秒') {
formatter = formatter_dt19_cn_1;
} else {
formatter = formatter_dt19_cn;
}
} else if (c4 == '년') {
formatter = formatter_dt19_kr;
}
}
boolean digit = true;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit && text.length() > 8 && text.length() < 19) {
long epochMillis = Long.parseLong(text);
return new LocalDateTime(epochMillis, DateTimeZone.forTimeZone(JSON.defaultTimeZone));
}
}
return formatter == null ? //
LocalDateTime.parse(text) //
: LocalDateTime.parse(text, formatter);
}
protected LocalDate parseLocalDate(String text, String format, DateTimeFormatter formatter) {
if (formatter == null) {
if (text.length() == 8) {
formatter = formatter_d8;
}
if (text.length() == 10) {
char c4 = text.charAt(4);
char c7 = text.charAt(7);
if (c4 == '/' && c7 == '/') { // tw yyyy/mm/dd
formatter = formatter_d10_tw;
}
char c0 = text.charAt(0);
char c1 = text.charAt(1);
char c2 = text.charAt(2);
char c3 = text.charAt(3);
char c5 = text.charAt(5);
if (c2 == '/' && c5 == '/') { // mm/dd/yyyy or mm/dd/yyyy
int v0 = (c0 - '0') * 10 + (c1 - '0');
int v1 = (c3 - '0') * 10 + (c4 - '0');
if (v0 > 12) {
formatter = formatter_d10_eur;
} else if (v1 > 12) {
formatter = formatter_d10_us;
} else {
String country = Locale.getDefault().getCountry();
if (country.equals("US")) {
formatter = formatter_d10_us;
} else if (country.equals("BR") //
|| country.equals("AU")) {
formatter = formatter_d10_eur;
}
}
} else if (c2 == '.' && c5 == '.') { // dd.mm.yyyy
formatter = formatter_d10_de;
} else if (c2 == '-' && c5 == '-') { // dd-mm-yyyy
formatter = formatter_d10_in;
}
}
if (text.length() >= 9) {
char c4 = text.charAt(4);
if (c4 == '年') {
formatter = formatter_d10_cn;
} else if (c4 == '년') {
formatter = formatter_d10_kr;
}
}
boolean digit = true;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit && text.length() > 8 && text.length() < 19) {
long epochMillis = Long.parseLong(text);
return new LocalDateTime(epochMillis, DateTimeZone.forTimeZone(JSON.defaultTimeZone))
.toLocalDate();
}
}
return formatter == null ? //
LocalDate.parse(text) //
: LocalDate.parse(text, formatter);
}
protected DateTime parseZonedDateTime(String text, DateTimeFormatter formatter) {
if (formatter == null) {
if (text.length() == 19) {
char c4 = text.charAt(4);
char c7 = text.charAt(7);
char c10 = text.charAt(10);
char c13 = text.charAt(13);
char c16 = text.charAt(16);
if (c13 == ':' && c16 == ':') {
if (c4 == '-' && c7 == '-') { // yyyy-MM-dd or yyyy-MM-dd'T'
if (c10 == 'T') {
formatter = formatter_iso8601;
} else if (c10 == ' ') {
formatter = defaultFormatter;
}
} else if (c4 == '/' && c7 == '/') { // tw yyyy/mm/dd
formatter = formatter_dt19_tw;
} else {
char c0 = text.charAt(0);
char c1 = text.charAt(1);
char c2 = text.charAt(2);
char c3 = text.charAt(3);
char c5 = text.charAt(5);
if (c2 == '/' && c5 == '/') { // mm/dd/yyyy or mm/dd/yyyy
int v0 = (c0 - '0') * 10 + (c1 - '0');
int v1 = (c3 - '0') * 10 + (c4 - '0');
if (v0 > 12) {
formatter = formatter_dt19_eur;
} else if (v1 > 12) {
formatter = formatter_dt19_us;
} else {
String country = Locale.getDefault().getCountry();
if (country.equals("US")) {
formatter = formatter_dt19_us;
} else if (country.equals("BR") //
|| country.equals("AU")) {
formatter = formatter_dt19_eur;
}
}
} else if (c2 == '.' && c5 == '.') { // dd.mm.yyyy
formatter = formatter_dt19_de;
} else if (c2 == '-' && c5 == '-') { // dd-mm-yyyy
formatter = formatter_dt19_in;
}
}
}
}
if (text.length() >= 17) {
char c4 = text.charAt(4);
if (c4 == '年') {
if (text.charAt(text.length() - 1) == '秒') {
formatter = formatter_dt19_cn_1;
} else {
formatter = formatter_dt19_cn;
}
} else if (c4 == '년') {
formatter = formatter_dt19_kr;
}
}
}
return formatter == null ? //
DateTime.parse(text) //
: DateTime.parse(text, formatter);
}
public int getFastMatchToken() {
return JSONToken.LITERAL_STRING;
}
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
} else {
if (fieldType == null) {
fieldType = object.getClass();
}
if (fieldType == LocalDateTime.class) {
final int mask = SerializerFeature.UseISO8601DateFormat.getMask();
LocalDateTime dateTime = (LocalDateTime) object;
String format = serializer.getDateFormatPattern();
if (format == null) {
if ((features & mask) != 0 || serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)) {
format = formatter_iso8601_pattern;
} else if (serializer.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
format = JSON.DEFFAULT_DATE_FORMAT;
} else {
int millis = dateTime.getMillisOfSecond();
if (millis == 0) {
format = formatter_iso8601_pattern_23;
} else {
format = formatter_iso8601_pattern_29;
}
}
}
if (format != null) {
write(out, dateTime, format);
} else {
out.writeLong(dateTime.toDateTime(DateTimeZone.forTimeZone(JSON.defaultTimeZone)).toInstant().getMillis());
}
} else {
out.writeString(object.toString());
}
}
}
public void write(JSONSerializer serializer, Object object, BeanContext context) throws IOException {
SerializeWriter out = serializer.out;
String format = context.getFormat();
write(out, (ReadablePartial) object, format);
}
private void write(SerializeWriter out, ReadablePartial object, String format) {
DateTimeFormatter formatter;
if (format.equals(formatter_iso8601_pattern)) {
formatter = formatter_iso8601;
} else {
formatter = DateTimeFormat.forPattern(format);
}
String text = formatter.print(object);
out.writeString(text);
}
}