注册反序列化解析(十一)

反序列化回调接口实现分析

内部注册的反序列化

fastjson针对常用的类型已经注册了反序列化实现方案,根据源代码注册com.alibaba.fastjson.parser.ParserConfig#initDeserializers可以得到列表:

注册的类型反序列化实例是否支持序列化是否支持反序列化
SimpleDateFormatMiscCodec
TimestampSqlDateDeserializer-
DateSqlDateDeserializer-
TimeTimeDeserializer-
DateDateCodec
CalendarCalendarCodec
XMLGregorianCalendarCalendarCodec
JSONObjectMapDeserializer-
JSONArrayCollectionCodec
MapMapDeserializer-
HashMapMapDeserializer-
LinkedHashMapMapDeserializer-
TreeMapMapDeserializer-
ConcurrentMapMapDeserializer-
ConcurrentHashMapMapDeserializer-
CollectionCollectionCodec
ListCollectionCodec
ArrayListCollectionCodec
ObjectJavaObjectDeserializer-
StringStringCodec
StringBufferStringCodec
StringBuilderStringCodec
charCharacterCodec
CharacterCharacterCodec
byteNumberDeserializer-
ByteNumberDeserializer-
shortNumberDeserializer-
ShortNumberDeserializer-
intIntegerCodec
IntegerIntegerCodec
longLongCodec
LongLongCodec
BigIntegerBigIntegerCodec
BigDecimalBigDecimalCodec
floatFloatCodec
FloatFloatCodec
doubleNumberDeserializer
DoubleNumberDeserializer
booleanBooleanCodec
BooleanBooleanCodec
ClassMiscCodec
char[]CharArrayCodec
AtomicBooleanBooleanCodec
AtomicBooleanIntegerCodec
AtomicLongLongCodec
AtomicReferenceReferenceCodec
WeakReferenceReferenceCodec
SoftReferenceReferenceCodec
UUIDMiscCodec
TimeZoneMiscCodec
LocaleMiscCodec
CurrencyMiscCodec
InetAddressMiscCodec
Inet4AddressMiscCodec
Inet6AddressMiscCodec
InetSocketAddressMiscCodec
FileMiscCodec
URIMiscCodec
URLMiscCodec
PatternMiscCodec
CharsetMiscCodec
JSONPathMiscCodec
NumberNumberDeserializer-
AtomicIntegerArrayAtomicCodec
AtomicLongArrayAtomicCodec
StackTraceElementStackTraceElementDeserializer-
SerializableJavaObjectDeserializer-
CloneableJavaObjectDeserializer-
ComparableJavaObjectDeserializer-
CloseableJavaObjectDeserializer-
JSONPObjectJSONPDeserializer-

通过上面表格发现几乎把所有JDK常用的类型都注册了一遍,目的是在运行时能够查找到特定的反序列化实例而不需要使用默认Java的反序列化实例。

我们先从常见的类型开始分析反序列化实现。

BooleanCodec反序列化

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
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;

Boolean boolObj;

try {
/** 遇到true类型的token,预读下一个token */
if (lexer.token() == JSONToken.TRUE) {
lexer.nextToken(JSONToken.COMMA);
boolObj = Boolean.TRUE;
/** 遇到false类型的token,预读下一个token */
} else if (lexer.token() == JSONToken.FALSE) {
lexer.nextToken(JSONToken.COMMA);
boolObj = Boolean.FALSE;
} else if (lexer.token() == JSONToken.LITERAL_INT) {
/** 遇到整数类型的token,预读下一个token */
int intValue = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);

/** 1代表true,其他情况false */
if (intValue == 1) {
boolObj = Boolean.TRUE;
} else {
boolObj = Boolean.FALSE;
}
} else {
Object value = parser.parse();

if (value == null) {
return null;
}

/** 处理其他情况,比如Y,T代表true */
boolObj = TypeUtils.castToBoolean(value);
}
} catch (Exception ex) {
throw new JSONException("parseBoolean error, field : " + fieldName, ex);
}

/** 如果是原子类型 */
if (clazz == AtomicBoolean.class) {
return (T) new AtomicBoolean(boolObj.booleanValue());
}

return (T) boolObj;
}

每次反序列化拿到token是,当前记录的字符ch变量实际是token结尾的下一个字符,boolean类型字段会触发该接口。

CharacterCodec反序列化

1
2
3
4
5
6
7
8
9
10
11
12
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
/** 根据token解析类型 */
Object value = parser.parse();
return value == null
? null
/** 转换成char类型,如果是string取字符串第一个char */
: (T) TypeUtils.castToChar(value);
}

public Object parse() {
return parse(null);
}

看着反序列化应该挺简单,但是内部解析值委托给了DefaultJSONParser#parse(java.lang.Object), 会把字符串解析取第一个字符处理:

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
public Object parse(Object fieldName) {
final JSONLexer lexer = this.lexer;
switch (lexer.token()) {
/**
* ...忽略其他类型token,后面遇到会讲解
* /
case LITERAL_STRING:
/** 探测到是字符串类型,解析值 */
String stringLiteral = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);

if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
JSONScanner iso8601Lexer = new JSONScanner(stringLiteral);
try {
if (iso8601Lexer.scanISO8601DateIfMatch()) {
return iso8601Lexer.getCalendar().getTime();
}
} finally {
iso8601Lexer.close();
}
}

return stringLiteral;
/**
* ...忽略其他类型token,后面遇到会讲解
* /
}
}

IntegerCodec反序列化

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
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;

final int token = lexer.token();

/** 如果解析到null值,返回null */
if (token == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}


Integer intObj;
try {
if (token == JSONToken.LITERAL_INT) {
/** 整型字面量,预读下一个token */
int val = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(val);
} else if (token == JSONToken.LITERAL_FLOAT) {
/** 浮点数字面量,预读下一个token */
BigDecimal decimalValue = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(decimalValue.intValue());
} else {
if (token == JSONToken.LBRACE) {

/** 处理历史原因反序列化AtomicInteger成map */
JSONObject jsonObject = new JSONObject(true);
parser.parseObject(jsonObject);
intObj = TypeUtils.castToInt(jsonObject);
} else {
/** 处理其他情况 */
Object value = parser.parse();
intObj = TypeUtils.castToInt(value);
}
}
} catch (Exception ex) {
throw new JSONException("parseInt error, field : " + fieldName, ex);
}


if (clazz == AtomicInteger.class) {
return (T) new AtomicInteger(intObj.intValue());
}

return (T) intObj;
}

针对特殊场景AutomicInteger类型,可以通过单元测试com.alibaba.json.bvt.parser.AtomicIntegerComptableAndroidTest#test_for_compatible_zero进行动手实践调试:

1
2
3
4
5
public void test_for_compatible_zero() throws Exception {
String text = "{\"andIncrement\":-1,\"andDecrement\":0}";

assertEquals(0, JSON.parseObject(text, AtomicInteger.class).intValue());
}

继续对parseObject(jsonObject)进行分析:

1
2
3
public Object parseObject(final Map object) {
return parseObject(object, null);
}

LongCodec反序列化

因为和整数反序列化极其类似,请参考IntegerCodec不进行冗余分析。

FloatCodec反序列化

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
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.lexer;

if (lexer.token() == JSONToken.LITERAL_INT) {
/** 整型字面量,预读下一个token */
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) Float.valueOf(Float.parseFloat(val));
}

if (lexer.token() == JSONToken.LITERAL_FLOAT) {
/** 浮点数字面量,预读下一个token */
float val = lexer.floatValue();
lexer.nextToken(JSONToken.COMMA);
return (T) Float.valueOf(val);
}

/** 处理其他情况 */
Object value = parser.parse();

if (value == null) {
return null;
}

return (T) TypeUtils.castToFloat(value);
}

BigDecimalCodec反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_INT) {
/** 整型字面量,预读下一个token */
BigDecimal decimalValue = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
return (T) decimalValue;
}

if (lexer.token() == JSONToken.LITERAL_FLOAT) {
/** 浮点数字面量,预读下一个token */
BigDecimal val = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
return (T) val;
}

Object value = parser.parse();
return value == null //
? null //
: (T) TypeUtils.castToBigDecimal(value);
}

StringCodec反序列化

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
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
if (clazz == StringBuffer.class) {
/** 将解析的字符序列转换成StringBuffer */
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_STRING) {
/** 字符串字面量,预读下一个token */
String val = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);

return (T) new StringBuffer(val);
}

Object value = parser.parse();

if (value == null) {
return null;
}

return (T) new StringBuffer(value.toString());
}

if (clazz == StringBuilder.class) {
/** 将解析的字符序列转换成StringBuilder */
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_STRING) {
String val = lexer.stringVal();
/** 字符串字面量,预读下一个token */
lexer.nextToken(JSONToken.COMMA);

return (T) new StringBuilder(val);
}

Object value = parser.parse();

if (value == null) {
return null;
}

return (T) new StringBuilder(value.toString());
}

return (T) deserialze(parser);
}

@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.LITERAL_STRING) {
/** 字符串字面量,预读下一个token */
String val = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) val;
}

if (lexer.token() == JSONToken.LITERAL_INT) {
/** 整型字面量,预读下一个token */
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) val;
}

Object value = parser.parse();

if (value == null) {
return null;
}

return (T) value.toString();
}

ObjectArrayCodec反序列化

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
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
final JSONLexer lexer = parser.lexer;
int token = lexer.token();
if (token == JSONToken.NULL) {
/** 解析到Null,预读下一个token */
lexer.nextToken(JSONToken.COMMA);
return null;
}

if (token == JSONToken.LITERAL_STRING || token == JSONToken.HEX) {
byte[] bytes = lexer.bytesValue();
lexer.nextToken(JSONToken.COMMA);

if (bytes.length == 0 && type != byte[].class) {
return null;
}

return (T) bytes;
}

Class componentClass;
Type componentType;
if (type instanceof GenericArrayType) {
GenericArrayType clazz = (GenericArrayType) type;
/** 获取泛型数组真实参数类型 */
componentType = clazz.getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable typeVar = (TypeVariable) componentType;
Type objType = parser.getContext().type;
if (objType instanceof ParameterizedType) {
/** 获取泛型参数化类型,eg: Collection<String> */
ParameterizedType objParamType = (ParameterizedType) objType;
Type objRawType = objParamType.getRawType();
Type actualType = null;
if (objRawType instanceof Class) {
/** 遍历Class包含的参数化类型,查找与泛型数组类型名字一致的作为真实类型 */
TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
for (int i = 0; i < objTypeParams.length; ++i) {
if (objTypeParams[i].getName().equals(typeVar.getName())) {
actualType = objParamType.getActualTypeArguments()[i];
}
}
}
if (actualType instanceof Class) {
componentClass = (Class) actualType;
} else {
componentClass = Object.class;
}
} else {
// 获取数组类型上界
componentClass = TypeUtils.getClass(typeVar.getBounds()[0]);
}
} else {
componentClass = TypeUtils.getClass(componentType);
}
} else {
/** 非泛型数组,普通对象数组 */
Class clazz = (Class) type;
componentType = componentClass = clazz.getComponentType();
}
JSONArray array = new JSONArray();
/** 根据token解析数组元素放到array中 */
parser.parseArray(componentType, array, fieldName);

return (T) toObjectArray(parser, componentClass, array);
}

JavaBeanDeserializer反序列化

为了节省冗余的分析,我们主要分析最复杂的默认JavaBeanDeserializer反序列化实现。

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
public JavaBeanDeserializer(ParserConfig config, JavaBeanInfo beanInfo){
/** java对象类名称 */
this.clazz = beanInfo.clazz;
this.beanInfo = beanInfo;

Map<String, FieldDeserializer> alterNameFieldDeserializers = null;
sortedFieldDeserializers = new FieldDeserializer[beanInfo.sortedFields.length];
/**
* 给已排序的字段创建反序列化实例,如果字段有别名,
* 关联别名到反序列化的映射
*/
for (int i = 0, size = beanInfo.sortedFields.length; i < size; ++i) {
FieldInfo fieldInfo = beanInfo.sortedFields[i];
FieldDeserializer fieldDeserializer = config.createFieldDeserializer(config, beanInfo, fieldInfo);

sortedFieldDeserializers[i] = fieldDeserializer;

for (String name : fieldInfo.alternateNames) {
if (alterNameFieldDeserializers == null) {
alterNameFieldDeserializers = new HashMap<String, FieldDeserializer>();
}
alterNameFieldDeserializers.put(name, fieldDeserializer);
}
}
this.alterNameFieldDeserializers = alterNameFieldDeserializers;

fieldDeserializers = new FieldDeserializer[beanInfo.fields.length];
for (int i = 0, size = beanInfo.fields.length; i < size; ++i) {
FieldInfo fieldInfo = beanInfo.fields[i];
/** 采用二分法在sortedFieldDeserializers中查找已创建的反序列化类型 */
FieldDeserializer fieldDeserializer = getFieldDeserializer(fieldInfo.name);
fieldDeserializers[i] = fieldDeserializer;
}
}

构造函数就是简单构造类字段对应的反序列化实例而已,接下来看下关键实现:

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
protected <T> T deserialze(DefaultJSONParser parser,
Type type,
Object fieldName,
Object object,
int features,
int[] setFlags) {
if (type == JSON.class || type == JSONObject.class) {
/** 根据当前token类型判断解析对象 */
return (T) parser.parse();
}

final JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
final ParserConfig config = parser.getConfig();

int token = lexer.token();
if (token == JSONToken.NULL) {
/** 解析null,预读下一个token并返回 */
lexer.nextToken(JSONToken.COMMA);
return null;
}

ParseContext context = parser.getContext();
if (object != null && context != null) {
context = context.parent;
}
ParseContext childContext = null;

try {
Map<String, Object> fieldValues = null;

if (token == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
/** 遇到}认为遇到对象结束,尝试创建实例对象 */
if (object == null) {
object = createInstance(parser, type);
}
return (T) object;
}

if (token == JSONToken.LBRACKET) {
final int mask = Feature.SupportArrayToBean.mask;
boolean isSupportArrayToBean = (beanInfo.parserFeatures & mask) != 0
|| lexer.isEnabled(Feature.SupportArrayToBean)
|| (features & mask) != 0
;
if (isSupportArrayToBean) {
/** 将数组值反序列化为对象,根据sortedFieldDeserializers依次写字段值 */
return deserialzeArrayMapping(parser, type, fieldName, object);
}
}

if (token != JSONToken.LBRACE && token != JSONToken.COMMA) {
if (lexer.isBlankInput()) {
return null;
}

if (token == JSONToken.LITERAL_STRING) {
String strVal = lexer.stringVal();
/** 读到空值字符串,返回null */
if (strVal.length() == 0) {
lexer.nextToken();
return null;
}

if (beanInfo.jsonType != null) {
/** 探测是否是枚举类型 */
for (Class<?> seeAlsoClass : beanInfo.jsonType.seeAlso()) {
if (Enum.class.isAssignableFrom(seeAlsoClass)) {
try {
Enum<?> e = Enum.valueOf((Class<Enum>) seeAlsoClass, strVal);
return (T) e;
} catch (IllegalArgumentException e) {
// skip
}
}
}
}
} else if (token == JSONToken.LITERAL_ISO8601_DATE) {
Calendar calendar = lexer.getCalendar();
}

if (token == JSONToken.LBRACKET && lexer.getCurrent() == ']') {
/** 包含零元素的数组 */
lexer.next();
lexer.nextToken();
return null;
}

StringBuffer buf = (new StringBuffer()) //
.append("syntax error, expect {, actual ") //
.append(lexer.tokenName()) //
.append(", pos ") //
.append(lexer.pos());

if (fieldName instanceof String) {
buf //
.append(", fieldName ") //
.append(fieldName);
}

buf.append(", fastjson-version ").append(JSON.VERSION);

throw new JSONException(buf.toString());
}

if (parser.resolveStatus == DefaultJSONParser.TypeNameRedirect) {
parser.resolveStatus = DefaultJSONParser.NONE;
}

String typeKey = beanInfo.typeKey;
for (int fieldIndex = 0;; fieldIndex++) {
String key = null;
FieldDeserializer fieldDeser = null;
FieldInfo fieldInfo = null;
Class<?> fieldClass = null;
JSONField feildAnnotation = null;
/** 检查是否所有字段都已经处理 */
if (fieldIndex < sortedFieldDeserializers.length) {
fieldDeser = sortedFieldDeserializers[fieldIndex];
fieldInfo = fieldDeser.fieldInfo;
fieldClass = fieldInfo.fieldClass;
feildAnnotation = fieldInfo.getAnnotation();
}

boolean matchField = false;
boolean valueParsed = false;

Object fieldValue = null;
if (fieldDeser != null) {
char[] name_chars = fieldInfo.name_chars;
if (fieldClass == int.class || fieldClass == Integer.class) {
/** 扫描整数值 */
fieldValue = lexer.scanFieldInt(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == long.class || fieldClass == Long.class) {
/** 扫描长整型值 */
fieldValue = lexer.scanFieldLong(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == String.class) {
/** 扫描字符串值 */
fieldValue = lexer.scanFieldString(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == java.util.Date.class && fieldInfo.format == null) {
/** 扫描日期值 */
fieldValue = lexer.scanFieldDate(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == BigDecimal.class) {
/** 扫描高精度值 */
fieldValue = lexer.scanFieldDecimal(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == BigInteger.class) {
fieldValue = lexer.scanFieldBigInteger(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == boolean.class || fieldClass == Boolean.class) {
/** 扫描boolean值 */
fieldValue = lexer.scanFieldBoolean(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == float.class || fieldClass == Float.class) {
/** 扫描浮点值 */
fieldValue = lexer.scanFieldFloat(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == double.class || fieldClass == Double.class) {
/** 扫描double值 */
fieldValue = lexer.scanFieldDouble(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}

} else if (fieldClass.isEnum()
&& parser.getConfig().getDeserializer(fieldClass) instanceof EnumDeserializer
&& (feildAnnotation == null || feildAnnotation.deserializeUsing() == Void.class)
) {
if (fieldDeser instanceof DefaultFieldDeserializer) {
ObjectDeserializer fieldValueDeserilizer = ((DefaultFieldDeserializer) fieldDeser).fieldValueDeserilizer;
fieldValue = this.scanEnum(lexer, name_chars, fieldValueDeserilizer);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
}
} else if (fieldClass == int[].class) {
/** 扫描整型数组值 */
fieldValue = lexer.scanFieldIntArray(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == float[].class) {
fieldValue = lexer.scanFieldFloatArray(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (fieldClass == float[][].class) {
/** 扫描浮点数组值 */
fieldValue = lexer.scanFieldFloatArray2(name_chars);

if (lexer.matchStat > 0) {
matchField = true;
valueParsed = true;
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
continue;
}
} else if (lexer.matchField(name_chars)) {
matchField = true;
} else {
continue;
}
}

/** 如果当前字符串的json不匹配当前字段名称 */
if (!matchField) {
/** 将当前的字段名称加入符号表 */
key = lexer.scanSymbol(parser.symbolTable);

/** 当前是无效的字段标识符,比如是,等符号 */
if (key == null) {
token = lexer.token();
if (token == JSONToken.RBRACE) {
/** 结束花括号, 预读下一个token */
lexer.nextToken(JSONToken.COMMA);
break;
}
if (token == JSONToken.COMMA) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}

if ("$ref" == key && context != null) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
token = lexer.token();
if (token == JSONToken.LITERAL_STRING) {
String ref = lexer.stringVal();
if ("@".equals(ref)) {
object = context.object;
} else if ("..".equals(ref)) {
ParseContext parentContext = context.parent;
if (parentContext.object != null) {
object = parentContext.object;
} else {
parser.addResolveTask(new ResolveTask(parentContext, ref));
parser.resolveStatus = DefaultJSONParser.NeedToResolve;
}
} else if ("$".equals(ref)) {
ParseContext rootContext = context;
while (rootContext.parent != null) {
rootContext = rootContext.parent;
}

if (rootContext.object != null) {
object = rootContext.object;
} else {
parser.addResolveTask(new ResolveTask(rootContext, ref));
parser.resolveStatus = DefaultJSONParser.NeedToResolve;
}
} else {
Object refObj = parser.resolveReference(ref);
if (refObj != null) {
object = refObj;
} else {
parser.addResolveTask(new ResolveTask(context, ref));
parser.resolveStatus = DefaultJSONParser.NeedToResolve;
}
}
} else {
throw new JSONException("illegal ref, " + JSONToken.name(token));
}

lexer.nextToken(JSONToken.RBRACE);
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("illegal ref");
}
lexer.nextToken(JSONToken.COMMA);

parser.setContext(context, object, fieldName);

return (T) object;
}

if ((typeKey != null && typeKey.equals(key))
|| JSON.DEFAULT_TYPE_KEY == key) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String typeName = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);

/** 忽略字符串中包含@type解析 */
if (typeName.equals(beanInfo.typeName)|| parser.isEnabled(Feature.IgnoreAutoType)) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
continue;
}


/** 根据枚举seeAlso查找反序列化实例 */
ObjectDeserializer deserializer = getSeeAlso(config, this.beanInfo, typeName);
Class<?> userType = null;

if (deserializer == null) {
/** 无法匹配,查找类对应的泛型或者参数化类型关联的反序列化实例 */
Class<?> expectClass = TypeUtils.getClass(type);
userType = config.checkAutoType(typeName, expectClass, lexer.getFeatures());
deserializer = parser.getConfig().getDeserializer(userType);
}

Object typedObject = deserializer.deserialze(parser, userType, fieldName);
if (deserializer instanceof JavaBeanDeserializer) {
JavaBeanDeserializer javaBeanDeserializer = (JavaBeanDeserializer) deserializer;
if (typeKey != null) {
FieldDeserializer typeKeyFieldDeser = javaBeanDeserializer.getFieldDeserializer(typeKey);
typeKeyFieldDeser.setValue(typedObject, typeName);
}
}
return (T) typedObject;
} else {
throw new JSONException("syntax error");
}
}
}

/** 第一次创建并初始化对象实例 */
if (object == null && fieldValues == null) {
object = createInstance(parser, type);
if (object == null) {
fieldValues = new HashMap<String, Object>(this.fieldDeserializers.length);
}
childContext = parser.setContext(context, object, fieldName);
if (setFlags == null) {
setFlags = new int[(this.fieldDeserializers.length / 32) + 1];
}
}

if (matchField) {
if (!valueParsed) {
/** json串当前满足字段名称,并且没有解析过值 ,
* 直接使用当前字段关联的反序列化实例解析
*/
fieldDeser.parseField(parser, object, type, fieldValues);
} else {
if (object == null) {
/** 值已经解析过了,存储到map中 */
fieldValues.put(fieldInfo.name, fieldValue);
} else if (fieldValue == null) {
/** 字段值是null,排除int,long,float,double,boolean */
if (fieldClass != int.class
&& fieldClass != long.class
&& fieldClass != float.class
&& fieldClass != double.class
&& fieldClass != boolean.class
) {
fieldDeser.setValue(object, fieldValue);
}
} else {
fieldDeser.setValue(object, fieldValue);
}

if (setFlags != null) {
int flagIndex = fieldIndex / 32;
int bitIndex = fieldIndex % 32;
setFlags[flagIndex] |= (1 >> bitIndex);
}

if (lexer.matchStat == JSONLexer.END) {
break;
}
}
} else {
/** 字段名称当前和json串不匹配,通常顺序或者字段增加或者缺少,
* 根据key查找反序列化实例解析
*/
boolean match = parseField(parser, key, object, type, fieldValues, setFlags);
if (!match) {
/** 遇到封闭花括号,与读下一个token,跳出循环 */
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}

continue;
} else if (lexer.token() == JSONToken.COLON) {
throw new JSONException("syntax error, unexpect token ':'");
}
}

if (lexer.token() == JSONToken.COMMA) {
continue;
}

if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}

if (lexer.token() == JSONToken.IDENTIFIER || lexer.token() == JSONToken.ERROR) {
throw new JSONException("syntax error, unexpect token " + JSONToken.name(lexer.token()));
}
}

if (object == null) {
if (fieldValues == null) {
/** 第一次创建并初始化对象实例 */
object = createInstance(parser, type);
if (childContext == null) {
childContext = parser.setContext(context, object, fieldName);
}
return (T) object;
}

/** 提取构造函数参数名称 */
String[] paramNames = beanInfo.creatorConstructorParameters;
final Object[] params;
if (paramNames != null) {
params = new Object[paramNames.length];
for (int i = 0; i < paramNames.length; i++) {
String paramName = paramNames[i];

Object param = fieldValues.remove(paramName);
/** 解析过的字段不包含当前参数名字 */
if (param == null) {
Type fieldType = beanInfo.creatorConstructorParameterTypes[i];
FieldInfo fieldInfo = beanInfo.fields[i];
/** 探测并设置类型默认值 */
if (fieldType == byte.class) {
param = (byte) 0;
} else if (fieldType == short.class) {
param = (short) 0;
} else if (fieldType == int.class) {
param = 0;
} else if (fieldType == long.class) {
param = 0L;
} else if (fieldType == float.class) {
param = 0F;
} else if (fieldType == double.class) {
param = 0D;
} else if (fieldType == boolean.class) {
param = Boolean.FALSE;
} else if (fieldType == String.class
&& (fieldInfo.parserFeatures & Feature.InitStringFieldAsEmpty.mask) != 0) {
param = "";
}
}
params[i] = param;
}
} else {
/** 根据字段探测并初始化构造函数参数默认值 */
FieldInfo[] fieldInfoList = beanInfo.fields;
int size = fieldInfoList.length;
params = new Object[size];
for (int i = 0; i < size; ++i) {
FieldInfo fieldInfo = fieldInfoList[i];
Object param = fieldValues.get(fieldInfo.name);
if (param == null) {
Type fieldType = fieldInfo.fieldType;
if (fieldType == byte.class) {
param = (byte) 0;
} else if (fieldType == short.class) {
param = (short) 0;
} else if (fieldType == int.class) {
param = 0;
} else if (fieldType == long.class) {
param = 0L;
} else if (fieldType == float.class) {
param = 0F;
} else if (fieldType == double.class) {
param = 0D;
} else if (fieldType == boolean.class) {
param = Boolean.FALSE;
} else if (fieldType == String.class
&& (fieldInfo.parserFeatures & Feature.InitStringFieldAsEmpty.mask) != 0) {
param = "";
}
}
params[i] = param;
}
}

if (beanInfo.creatorConstructor != null) {
try {
object = beanInfo.creatorConstructor.newInstance(params);
} catch (Exception e) {
throw new JSONException("create instance error, " + paramNames + ", "
+ beanInfo.creatorConstructor.toGenericString(), e);
}

if (paramNames != null) {
/** 剩余字段查找反序列化器set值 */
for (Map.Entry<String, Object> entry : fieldValues.entrySet()) {
FieldDeserializer fieldDeserializer = getFieldDeserializer(entry.getKey());
if (fieldDeserializer != null) {
fieldDeserializer.setValue(object, entry.getValue());
}
}
}
} else if (beanInfo.factoryMethod != null) {
try {
object = beanInfo.factoryMethod.invoke(null, params);
} catch (Exception e) {
throw new JSONException("create factory method error, " + beanInfo.factoryMethod.toString(), e);
}
}

childContext.object = object;
}

/** 检查是否扩展后置方法buildMethod,如果有进行调用 */
Method buildMethod = beanInfo.buildMethod;
if (buildMethod == null) {
return (T) object;
}


Object builtObj;
try {
builtObj = buildMethod.invoke(object);
} catch (Exception e) {
throw new JSONException("build object error", e);
}

return (T) builtObj;
} finally {
if (childContext != null) {
childContext.object = object;
}
parser.setContext(context);
}
}

这段代码实在又臭又长,实际做的事情如下:

  1. 根据类所有的字段,字段类型进行json串进行匹配,首先检查json串的值是否和当前字段名称相等,如果相等认为匹配成功,会创建实例对象并且把解析字段值set进去。
  2. 如果当前json串顺序和java对象字段不一致怎么办,这个时候我字段又全部遍历完了,fastjson会自动把当前解析的字段名称加入符号表中,然后查找字段名称对应的反序列化实例进行set值操作
  3. 当前实现提供了解析对象后buildMethod扩展点,如果提供了会进行回调然后返回

值得一提的是构造函数处理:

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
public Object createInstance(DefaultJSONParser parser, Type type) {
if (type instanceof Class) {
if (clazz.isInterface()) {
/** 针对反序列化时接口类型的,通过jdk冬天代理拦截put和get等操作,
* 进行set或者put值的操作值会存储在jsonobject内部的map结构
*/
Class<?> clazz = (Class<?>) type;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
final JSONObject obj = new JSONObject();
Object proxy = Proxy.newProxyInstance(loader, new Class<?>[] { clazz }, obj);
return proxy;
}
}

/** 忽略没有默认构造函数和没有创建对象的工厂方法 */
if (beanInfo.defaultConstructor == null && beanInfo.factoryMethod == null) {
return null;
}

/** 忽略同时存在显示构造函数和创建对象的工厂方法的场景 */
if (beanInfo.factoryMethod != null && beanInfo.defaultConstructorParameterSize > 0) {
return null;
}

Object object;
try {
Constructor<?> constructor = beanInfo.defaultConstructor;
/** 存在默认无参构造函数 */
if (beanInfo.defaultConstructorParameterSize == 0) {
if (constructor != null) {
object = constructor.newInstance();
} else {
/** 否则使用工厂方法生成对象 */
object = beanInfo.factoryMethod.invoke(null);
}
} else {
ParseContext context = parser.getContext();
if (context == null || context.object == null) {
throw new JSONException("can't create non-static inner class instance.");
}

final String typeName;
if (type instanceof Class) {
typeName = ((Class<?>) type).getName();
} else {
throw new JSONException("can't create non-static inner class instance.");
}

final int lastIndex = typeName.lastIndexOf('$');
String parentClassName = typeName.substring(0, lastIndex);

Object ctxObj = context.object;
String parentName = ctxObj.getClass().getName();

Object param = null;
if (!parentName.equals(parentClassName)) {
/** 处理继承过来的类 */
ParseContext parentContext = context.parent;
if (parentContext != null
&& parentContext.object != null
&& ("java.util.ArrayList".equals(parentName)
|| "java.util.List".equals(parentName)
|| "java.util.Collection".equals(parentName)
|| "java.util.Map".equals(parentName)
|| "java.util.HashMap".equals(parentName))) {
parentName = parentContext.object.getClass().getName();
if (parentName.equals(parentClassName)) {
param = parentContext.object;
}
}
} else {
/** 处理非静态内部类场景,
* 编译器会自动修改内部类构造函数,添加外层类实例对象作为参数,
* ctxObj就是外层实例对象
*/
param = ctxObj;
}

if (param == null) {
throw new JSONException("can't create non-static inner class instance.");
}

object = constructor.newInstance(param);
}
} catch (JSONException e) {
throw e;
} catch (Exception e) {
throw new JSONException("create instance error, class " + clazz.getName(), e);
}

/** 开启InitStringFieldAsEmpty特性,会把字符串字段初始化为空串 */
if (parser != null
&& parser.lexer.isEnabled(Feature.InitStringFieldAsEmpty)) {
for (FieldInfo fieldInfo : beanInfo.fields) {
if (fieldInfo.fieldClass == String.class) {
try {
fieldInfo.set(object, "");
} catch (Exception e) {
throw new JSONException("create instance error, class " + clazz.getName(), e);
}
}
}
}

return object;
}

编译器会为非静态内部类构造函数添加外层的实例对象作为第一个参数,所以在生成实例化对象的时候会从上下文中获取外层对象进行反射创建对象constructor.newInstance(param)

为了更容易理解这段逻辑,提供一下单元测试可以调试:

1
2
3
4
5
com.alibaba.json.bvt.parser.deser.InnerClassDeser2#test_for_inner_class

com.alibaba.json.bvt.parser.deser.InnerClassDeser3#test_for_inner_class

com.alibaba.json.bvt.parser.deser.InnerClassDeser4#test_for_inner_class
感谢您的阅读,本文由 诣极的博客 版权所有。如若转载,请注明出处:诣极的博客(https://zonghaishang.github.io/2018/09/30/Fastjson源码解析-反序列化(二)-内部注册反序列化解析/
反序列化解析介绍(十)
工作经历