/
TypeUtils.java
3458 lines (3120 loc) · 129 KB
/
TypeUtils.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 1999-2017 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.fastjson.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.JSONScanner;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.EnumDeserializer;
import com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.CalendarCodec;
import com.alibaba.fastjson.serializer.SerializeBeanInfo;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.AccessControlException;
import java.sql.Clob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author wenshao[szujobs@hotmail.com]
*/
public class TypeUtils {
private static final Pattern NUMBER_WITH_TRAILING_ZEROS_PATTERN = Pattern.compile("\\.0*$");
public static boolean compatibleWithJavaBean = false;
/**
* 根据field name的大小写输出输入数据
*/
public static boolean compatibleWithFieldName = false;
private static boolean setAccessibleEnable = true;
private static boolean oracleTimestampMethodInited = false;
private static Method oracleTimestampMethod;
private static boolean oracleDateMethodInited = false;
private static Method oracleDateMethod;
private static boolean optionalClassInited = false;
private static Class<?> optionalClass;
private static boolean transientClassInited = false;
private static Class<? extends Annotation> transientClass;
private static Class<? extends Annotation> class_OneToMany = null;
private static boolean class_OneToMany_error = false;
private static Class<? extends Annotation> class_ManyToMany = null;
private static boolean class_ManyToMany_error = false;
private static Method method_HibernateIsInitialized = null;
private static boolean method_HibernateIsInitialized_error = false;
private static volatile Class kotlin_metadata;
private static volatile boolean kotlin_metadata_error;
private static volatile boolean kotlin_class_klass_error;
private static volatile Constructor kotlin_kclass_constructor;
private static volatile Method kotlin_kclass_getConstructors;
private static volatile Method kotlin_kfunction_getParameters;
private static volatile Method kotlin_kparameter_getName;
private static volatile boolean kotlin_error;
private static volatile Map<Class, String[]> kotlinIgnores;
private static volatile boolean kotlinIgnores_error;
private static ConcurrentMap<String, Class<?>> mappings = new ConcurrentHashMap<String, Class<?>>(256, 0.75f, 1);
private static Class<?> pathClass;
private static boolean pathClass_error = false;
private static Class<? extends Annotation> class_JacksonCreator = null;
private static boolean class_JacksonCreator_error = false;
private static volatile Class class_XmlAccessType = null;
private static volatile Class class_XmlAccessorType = null;
private static volatile boolean classXmlAccessorType_error = false;
private static volatile Method method_XmlAccessorType_value = null;
private static volatile Field field_XmlAccessType_FIELD = null;
private static volatile Object field_XmlAccessType_FIELD_VALUE = null;
private static Class class_deque = null;
static {
try {
TypeUtils.compatibleWithJavaBean = "true".equals(IOUtils.getStringProperty(IOUtils.FASTJSON_COMPATIBLEWITHJAVABEAN));
TypeUtils.compatibleWithFieldName = "true".equals(IOUtils.getStringProperty(IOUtils.FASTJSON_COMPATIBLEWITHFIELDNAME));
} catch (Throwable e) {
// skip
}
try {
class_deque = Class.forName("java.util.Deque");
} catch (Throwable e) {
// skip
}
}
public static boolean isXmlField(Class clazz) {
if (class_XmlAccessorType == null && !classXmlAccessorType_error) {
try {
class_XmlAccessorType = Class.forName("javax.xml.bind.annotation.XmlAccessorType");
} catch (Throwable ex) {
classXmlAccessorType_error = true;
}
}
if (class_XmlAccessorType == null) {
return false;
}
Annotation annotation = TypeUtils.getAnnotation(clazz, class_XmlAccessorType);
if (annotation == null) {
return false;
}
if (method_XmlAccessorType_value == null && !classXmlAccessorType_error) {
try {
method_XmlAccessorType_value = class_XmlAccessorType.getMethod("value");
} catch (Throwable ex) {
classXmlAccessorType_error = true;
}
}
if (method_XmlAccessorType_value == null) {
return false;
}
Object value = null;
if (!classXmlAccessorType_error) {
try {
value = method_XmlAccessorType_value.invoke(annotation);
} catch (Throwable ex) {
classXmlAccessorType_error = true;
}
}
if (value == null) {
return false;
}
if (class_XmlAccessType == null && !classXmlAccessorType_error) {
try {
class_XmlAccessType = Class.forName("javax.xml.bind.annotation.XmlAccessType");
field_XmlAccessType_FIELD = class_XmlAccessType.getField("FIELD");
field_XmlAccessType_FIELD_VALUE = field_XmlAccessType_FIELD.get(null);
} catch (Throwable ex) {
classXmlAccessorType_error = true;
}
}
return value == field_XmlAccessType_FIELD_VALUE;
}
public static Annotation getXmlAccessorType(Class clazz) {
if (class_XmlAccessorType == null && !classXmlAccessorType_error) {
try {
class_XmlAccessorType = Class.forName("javax.xml.bind.annotation.XmlAccessorType");
} catch (Throwable ex) {
classXmlAccessorType_error = true;
}
}
if (class_XmlAccessorType == null) {
return null;
}
return TypeUtils.getAnnotation(clazz, class_XmlAccessorType);
}
//
// public static boolean isXmlAccessType(Class clazz) {
// if (class_XmlAccessType == null && !class_XmlAccessType_error) {
//
// try{
// class_XmlAccessType = Class.forName("javax.xml.bind.annotation.XmlAccessType");
// } catch(Throwable ex){
// class_XmlAccessType_error = true;
// }
// }
//
// if (class_XmlAccessType == null) {
// return false;
// }
//
// return class_XmlAccessType.isAssignableFrom(clazz);
// }
private static Function<Class, Boolean> isClobFunction = new Function<Class, Boolean>() {
public Boolean apply(Class clazz) {
return Clob.class.isAssignableFrom(clazz);
}
};
public static boolean isClob(final Class clazz) {
Boolean isClob = ModuleUtil.callWhenHasJavaSql(isClobFunction, clazz);
return isClob != null ? isClob : false;
}
public static String castToString(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
public static Byte castToByte(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return byteValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).byteValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
return Byte.parseByte(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? (byte) 1 : (byte) 0;
}
throw new JSONException("can not cast to byte, value : " + value);
}
public static Character castToChar(Object value) {
if (value == null) {
return null;
}
if (value instanceof Character) {
return (Character) value;
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0) {
return null;
}
if (strVal.length() != 1) {
throw new JSONException("can not cast to char, value : " + value);
}
return strVal.charAt(0);
}
throw new JSONException("can not cast to char, value : " + value);
}
public static Short castToShort(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return shortValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).shortValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
return Short.parseShort(strVal);
}
if (value instanceof Boolean) {
return ((Boolean) value).booleanValue() ? (short) 1 : (short) 0;
}
throw new JSONException("can not cast to short, value : " + value);
}
public static BigDecimal castToBigDecimal(Object value) {
if (value == null) {
return null;
}
if (value instanceof Float) {
if (Float.isNaN((Float) value) || Float.isInfinite((Float) value)) {
return null;
}
} else if (value instanceof Double) {
if (Double.isNaN((Double) value) || Double.isInfinite((Double) value)) {
return null;
}
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
} else if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
} else if (value instanceof Map && ((Map) value).size() == 0) {
return null;
}
String strVal = value.toString();
if (strVal.length() == 0
|| strVal.equalsIgnoreCase("null")) {
return null;
}
if (strVal.length() > 65535) {
throw new JSONException("decimal overflow");
}
return new BigDecimal(strVal);
}
public static BigInteger castToBigInteger(Object value) {
if (value == null) {
return null;
}
if (value instanceof Float) {
Float floatValue = (Float) value;
if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
return null;
}
return BigInteger.valueOf(floatValue.longValue());
} else if (value instanceof Double) {
Double doubleValue = (Double) value;
if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
return null;
}
return BigInteger.valueOf(doubleValue.longValue());
} else if (value instanceof BigInteger) {
return (BigInteger) value;
} else if (value instanceof BigDecimal) {
BigDecimal decimal = (BigDecimal) value;
int scale = decimal.scale();
if (scale > -1000 && scale < 1000) {
return ((BigDecimal) value).toBigInteger();
}
}
String strVal = value.toString();
if (strVal.length() == 0
|| strVal.equalsIgnoreCase("null")) {
return null;
}
if (strVal.length() > 65535) {
throw new JSONException("decimal overflow");
}
return new BigInteger(strVal);
}
public static Float castToFloat(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
String strVal = value.toString();
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
return Float.parseFloat(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? 1F : 0F;
}
throw new JSONException("can not cast to float, value : " + value);
}
public static Double castToDouble(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
String strVal = value.toString();
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
return Double.parseDouble(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? 1D : 0D;
}
throw new JSONException("can not cast to double, value : " + value);
}
public static Date castToDate(Object value) {
return castToDate(value, null);
}
public static Date castToDate(Object value, String format) {
if (value == null) {
return null;
}
if (value instanceof Date) { // 使用频率最高的,应优先处理
return (Date) value;
}
if (value instanceof Calendar) {
return ((Calendar) value).getTime();
}
long longValue = -1;
if (value instanceof BigDecimal) {
longValue = longValue((BigDecimal) value);
return new Date(longValue);
}
if (value instanceof Number) {
longValue = ((Number) value).longValue();
if ("unixtime".equals(format)) {
longValue *= 1000;
}
return new Date(longValue);
}
if (value instanceof String) {
String strVal = (String) value;
JSONScanner dateLexer = new JSONScanner(strVal);
try {
if (dateLexer.scanISO8601DateIfMatch(false)) {
Calendar calendar = dateLexer.getCalendar();
return calendar.getTime();
}
} finally {
dateLexer.close();
}
if (strVal.startsWith("/Date(") && strVal.endsWith(")/")) {
strVal = strVal.substring(6, strVal.length() - 2);
}
if (strVal.indexOf('-') > 0 || strVal.indexOf('+') > 0 || format != null) {
if (format == null) {
final int len = strVal.length();
if (len == JSON.DEFFAULT_DATE_FORMAT.length()
|| (len == 22 && JSON.DEFFAULT_DATE_FORMAT.equals("yyyyMMddHHmmssSSSZ"))) {
format = JSON.DEFFAULT_DATE_FORMAT;
} else if (len == 10) {
format = "yyyy-MM-dd";
} else if (len == "yyyy-MM-dd HH:mm:ss".length()) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (len == 29
&& strVal.charAt(26) == ':'
&& strVal.charAt(28) == '0') {
format = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
} else if (len == 23 && strVal.charAt(19) == ',') {
format = "yyyy-MM-dd HH:mm:ss,SSS";
} else {
format = "yyyy-MM-dd HH:mm:ss.SSS";
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat(format, JSON.defaultLocale);
dateFormat.setTimeZone(JSON.defaultTimeZone);
try {
return dateFormat.parse(strVal);
} catch (ParseException e) {
throw new JSONException("can not cast to Date, value : " + strVal);
}
}
if (strVal.length() == 0) {
return null;
}
longValue = Long.parseLong(strVal);
}
if (longValue == -1) {
Class<?> clazz = value.getClass();
if ("oracle.sql.TIMESTAMP".equals(clazz.getName())) {
if (oracleTimestampMethod == null && !oracleTimestampMethodInited) {
try {
oracleTimestampMethod = clazz.getMethod("toJdbc");
} catch (NoSuchMethodException e) {
// skip
} finally {
oracleTimestampMethodInited = true;
}
}
Object result;
try {
result = oracleTimestampMethod.invoke(value);
} catch (Exception e) {
throw new JSONException("can not cast oracle.sql.TIMESTAMP to Date", e);
}
return (Date) result;
}
if ("oracle.sql.DATE".equals(clazz.getName())) {
if (oracleDateMethod == null && !oracleDateMethodInited) {
try {
oracleDateMethod = clazz.getMethod("toJdbc");
} catch (NoSuchMethodException e) {
// skip
} finally {
oracleDateMethodInited = true;
}
}
Object result;
try {
result = oracleDateMethod.invoke(value);
} catch (Exception e) {
throw new JSONException("can not cast oracle.sql.DATE to Date", e);
}
return (Date) result;
}
throw new JSONException("can not cast to Date, value : " + value);
}
return new Date(longValue);
}
private static Function<Object, Object> castToSqlDateFunction = new Function<Object, Object>() {
public Object apply(Object value) {
if (value == null) {
return null;
}
if (value instanceof java.sql.Date) {
return (java.sql.Date) value;
}
if (value instanceof Date) {
return new java.sql.Date(((Date) value).getTime());
}
if (value instanceof Calendar) {
return new java.sql.Date(((Calendar) value).getTimeInMillis());
}
long longValue = 0;
if (value instanceof BigDecimal) {
longValue = longValue((BigDecimal) value);
} else if (value instanceof Number) {
longValue = ((Number) value).longValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (isNumber(strVal)) {
longValue = Long.parseLong(strVal);
} else {
JSONScanner scanner = new JSONScanner(strVal);
if (scanner.scanISO8601DateIfMatch(false)) {
longValue = scanner.getCalendar().getTime().getTime();
} else {
throw new JSONException("can not cast to Timestamp, value : " + strVal);
}
}
}
if (longValue <= 0) {
throw new JSONException("can not cast to Date, value : " + value); // TODO 忽略 1970-01-01 之前的时间处理?
}
return new java.sql.Date(longValue);
}
};
public static Object castToSqlDate(final Object value) {
return ModuleUtil.callWhenHasJavaSql(castToSqlDateFunction, value);
}
public static long longExtractValue(Number number) {
if (number instanceof BigDecimal) {
return ((BigDecimal) number).longValueExact();
}
return number.longValue();
}
private static Function<Object, Object> castToSqlTimeFunction = new Function<Object, Object>() {
public Object apply(Object value) {
if (value == null) {
return null;
}
if (value instanceof java.sql.Time) {
return (java.sql.Time) value;
}
if (value instanceof java.util.Date) {
return new java.sql.Time(((java.util.Date) value).getTime());
}
if (value instanceof Calendar) {
return new java.sql.Time(((Calendar) value).getTimeInMillis());
}
long longValue = 0;
if (value instanceof BigDecimal) {
longValue = longValue((BigDecimal) value);
} else if (value instanceof Number) {
longValue = ((Number) value).longValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equalsIgnoreCase(strVal)) {
return null;
}
if (isNumber(strVal)) {
longValue = Long.parseLong(strVal);
} else {
if (strVal.length() == 8 && strVal.charAt(2) == ':' && strVal.charAt(5) == ':') {
return java.sql.Time.valueOf(strVal);
}
JSONScanner scanner = new JSONScanner(strVal);
if (scanner.scanISO8601DateIfMatch(false)) {
longValue = scanner.getCalendar().getTime().getTime();
} else {
throw new JSONException("can not cast to Timestamp, value : " + strVal);
}
}
}
if (longValue <= 0) {
throw new JSONException("can not cast to Date, value : " + value); // TODO 忽略 1970-01-01 之前的时间处理?
}
return new java.sql.Time(longValue);
}
};
public static Object castToSqlTime(final Object value) {
return ModuleUtil.callWhenHasJavaSql(castToSqlTimeFunction, value);
}
public static Function<Object, Object> castToTimestampFunction = new Function<Object, Object>() {
public Object apply(Object value) {
if (value == null) {
return null;
}
if (value instanceof Calendar) {
return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
}
if (value instanceof java.sql.Timestamp) {
return (java.sql.Timestamp) value;
}
if (value instanceof java.util.Date) {
return new java.sql.Timestamp(((java.util.Date) value).getTime());
}
long longValue = 0;
if (value instanceof BigDecimal) {
longValue = longValue((BigDecimal) value);
} else if (value instanceof Number) {
longValue = ((Number) value).longValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.endsWith(".000000000")) {
strVal = strVal.substring(0, strVal.length() - 10);
} else if (strVal.endsWith(".000000")) {
strVal = strVal.substring(0, strVal.length() - 7);
}
if (strVal.length() == 29
&& strVal.charAt(4) == '-'
&& strVal.charAt(7) == '-'
&& strVal.charAt(10) == ' '
&& strVal.charAt(13) == ':'
&& strVal.charAt(16) == ':'
&& strVal.charAt(19) == '.') {
int year = num(
strVal.charAt(0),
strVal.charAt(1),
strVal.charAt(2),
strVal.charAt(3));
int month = num(
strVal.charAt(5),
strVal.charAt(6));
int day = num(
strVal.charAt(8),
strVal.charAt(9));
int hour = num(
strVal.charAt(11),
strVal.charAt(12));
int minute = num(
strVal.charAt(14),
strVal.charAt(15));
int second = num(
strVal.charAt(17),
strVal.charAt(18));
int nanos = num(
strVal.charAt(20),
strVal.charAt(21),
strVal.charAt(22),
strVal.charAt(23),
strVal.charAt(24),
strVal.charAt(25),
strVal.charAt(26),
strVal.charAt(27),
strVal.charAt(28));
return new java.sql.Timestamp(year - 1900, month - 1, day, hour, minute, second, nanos);
}
if (isNumber(strVal)) {
longValue = Long.parseLong(strVal);
} else {
JSONScanner scanner = new JSONScanner(strVal);
if (scanner.scanISO8601DateIfMatch(false)) {
longValue = scanner.getCalendar().getTime().getTime();
} else {
throw new JSONException("can not cast to Timestamp, value : " + strVal);
}
}
}
return new java.sql.Timestamp(longValue);
}
};
public static Object castToTimestamp(final Object value) {
return ModuleUtil.callWhenHasJavaSql(castToTimestampFunction, value);
}
static int num(char c0, char c1) {
if (c0 >= '0'
&& c0 <= '9'
&& c1 >= '0'
&& c1 <= '9'
) {
return (c0 - '0') * 10
+ (c1 - '0');
}
return -1;
}
static int num(char c0, char c1, char c2, char c3) {
if (c0 >= '0'
&& c0 <= '9'
&& c1 >= '0'
&& c1 <= '9'
&& c2 >= '0'
&& c2 <= '9'
&& c3 >= '0'
&& c3 <= '9'
) {
return (c0 - '0') * 1000
+ (c1 - '0') * 100
+ (c2 - '0') * 10
+ (c3 - '0');
}
return -1;
}
static int num(char c0, char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8) {
if (c0 >= '0'
&& c0 <= '9'
&& c1 >= '0'
&& c1 <= '9'
&& c2 >= '0'
&& c2 <= '9'
&& c3 >= '0'
&& c3 <= '9'
&& c4 >= '0'
&& c4 <= '9'
&& c5 >= '0'
&& c5 <= '9'
&& c6 >= '0'
&& c6 <= '9'
&& c7 >= '0'
&& c7 <= '9'
&& c8 >= '0'
&& c8 <= '9'
) {
return (c0 - '0') * 100000000
+ (c1 - '0') * 10000000
+ (c2 - '0') * 1000000
+ (c3 - '0') * 100000
+ (c4 - '0') * 10000
+ (c5 - '0') * 1000
+ (c6 - '0') * 100
+ (c7 - '0') * 10
+ (c8 - '0');
}
return -1;
}
public static boolean isNumber(String str) {
for (int i = 0; i < str.length(); ++i) {
char ch = str.charAt(i);
if (ch == '+' || ch == '-') {
if (i != 0) {
return false;
}
} else if (ch < '0' || ch > '9') {
return false;
}
}
return true;
}
public static Long castToLong(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return longValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).longValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
try {
return Long.parseLong(strVal);
} catch (NumberFormatException ex) {
//
}
JSONScanner dateParser = new JSONScanner(strVal);
Calendar calendar = null;
if (dateParser.scanISO8601DateIfMatch(false)) {
calendar = dateParser.getCalendar();
}
dateParser.close();
if (calendar != null) {
return calendar.getTimeInMillis();
}
}
if (value instanceof Map) {
Map map = (Map) value;
if (map.size() == 2
&& map.containsKey("andIncrement")
&& map.containsKey("andDecrement")) {
Iterator iter = map.values().iterator();
iter.next();
Object value2 = iter.next();
return castToLong(value2);
}
}
if (value instanceof Boolean) {
return (Boolean) value ? 1L : 0L;
}
throw new JSONException("can not cast to long, value : " + value);
}
public static byte byteValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.byteValue();
}
return decimal.byteValueExact();
}
public static short shortValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.shortValue();
}
return decimal.shortValueExact();
}
public static int intValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.intValue();
}
return decimal.intValueExact();
}
public static long longValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.longValue();
}
return decimal.longValueExact();
}
public static Integer castToInt(Object value) {
if (value == null) {
return null;
}
if (value instanceof Integer) {
return (Integer) value;
}
if (value instanceof BigDecimal) {
return intValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).intValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
Matcher matcher = NUMBER_WITH_TRAILING_ZEROS_PATTERN.matcher(strVal);
if (matcher.find()) {
strVal = matcher.replaceAll("");
}
return Integer.parseInt(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? 1 : 0;