Repository navigation
Expand file tree
/
Copy pathTypeUtils.java
More file actions
209 lines (177 loc) · 5.31 KB
/
Copy pathTypeUtils.java
File metadata and controls
209 lines (177 loc) · 5.31 KB
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
package org.tron.json;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.regex.Pattern;
/**
* Type coercion utilities ported from {@code com.alibaba.fastjson.util.TypeUtils}
* to maintain exact behavioral parity with Fastjson 1.x.
*
* <p>Key Fastjson behaviors preserved:
* <ul>
* <li>Comma stripping in numeric strings ({@code "1,000"} → {@code 1000})</li>
* <li>Trailing-zero removal ({@code "1.0"} → {@code 1} for int/long)</li>
* <li>Boolean coercion from {@code "Y"/"T"/"F"/"N"} strings</li>
* <li>Boolean from Number: {@code intValue() == 1} (only 1 is true)</li>
* <li>{@code NaN}/{@code Infinity} → {@code null} for BigDecimal</li>
* <li>{@code null}/{@code "null"}/{@code "NULL"}/empty → {@code null}</li>
* </ul>
*/
final class TypeUtils {
private static final Pattern NUMBER_WITH_TRAILING_ZEROS_PATTERN =
Pattern.compile("\\.0*$");
private TypeUtils() {
}
static Boolean castToBoolean(Object value) {
if (value == null) {
return null;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
if (value instanceof BigDecimal) {
return intValue((BigDecimal) value) == 1;
}
if (value instanceof Number) {
return ((Number) value).intValue() == 1;
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.isEmpty()
|| "null".equals(strVal)
|| "NULL".equals(strVal)) {
return null;
}
if ("true".equalsIgnoreCase(strVal)
|| "1".equals(strVal)) {
return Boolean.TRUE;
}
if ("false".equalsIgnoreCase(strVal)
|| "0".equals(strVal)) {
return Boolean.FALSE;
}
if ("Y".equalsIgnoreCase(strVal)
|| "T".equals(strVal)) {
return Boolean.TRUE;
}
if ("F".equalsIgnoreCase(strVal)
|| "N".equals(strVal)) {
return Boolean.FALSE;
}
}
throw new JSONException("can not cast to boolean, value : " + value);
}
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.isEmpty()
|| "null".equals(strVal)
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
strVal = NUMBER_WITH_TRAILING_ZEROS_PATTERN.matcher(strVal).replaceAll("");
return Integer.parseInt(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? 1 : 0;
}
throw new JSONException("can not cast to int, value : " + value);
}
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.isEmpty()
|| "null".equals(strVal)
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
try {
return Long.parseLong(strVal);
} catch (NumberFormatException ex) {
// Fastjson falls through to BigDecimal attempt
}
strVal = NUMBER_WITH_TRAILING_ZEROS_PATTERN.matcher(strVal).replaceAll("");
return Long.parseLong(strVal);
}
if (value instanceof Boolean) {
return (Boolean) value ? 1L : 0L;
}
throw new JSONException("can not cast to long, value : " + value);
}
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);
}
String strVal = value.toString();
if (strVal.isEmpty() || "null".equalsIgnoreCase(strVal)) {
return null;
}
if (strVal.length() > 65535) {
throw new JSONException("decimal overflow");
}
if (strVal.indexOf(',') != -1) {
strVal = strVal.replaceAll(",", "");
}
return new BigDecimal(strVal);
}
// -- BigDecimal helper methods (ported from Fastjson) --
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();
}
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();
}
}