forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionArrayDecoder.java
More file actions
73 lines (67 loc) · 2.44 KB
/
ReflectionArrayDecoder.java
File metadata and controls
73 lines (67 loc) · 2.44 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
package com.jsoniter;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.TypeLiteral;
import java.io.IOException;
import java.lang.reflect.Array;
class ReflectionArrayDecoder implements Decoder {
private final Class componentType;
private final TypeLiteral compTypeLiteral;
public ReflectionArrayDecoder(Class clazz) {
componentType = clazz.getComponentType();
compTypeLiteral = TypeLiteral.create(componentType);
}
@Override
public Object decode(JsonIterator iter) throws IOException {
CodegenAccess.resetExistingObject(iter);
if (iter.readNull()) {
return null;
}
if (!CodegenAccess.readArrayStart(iter)) {
return Array.newInstance(componentType, 0);
}
Object a1 = CodegenAccess.read(iter, compTypeLiteral);
if (CodegenAccess.nextToken(iter) != ',') {
Object arr = Array.newInstance(componentType, 1);
Array.set(arr, 0, a1);
return arr;
}
Object a2 = CodegenAccess.read(iter, compTypeLiteral);
if (CodegenAccess.nextToken(iter) != ',') {
Object arr = Array.newInstance(componentType, 2);
Array.set(arr, 0, a1);
Array.set(arr, 1, a2);
return arr;
}
Object a3 = CodegenAccess.read(iter, compTypeLiteral);
if (CodegenAccess.nextToken(iter) != ',') {
Object arr = Array.newInstance(componentType, 3);
Array.set(arr, 0, a1);
Array.set(arr, 1, a2);
Array.set(arr, 2, a3);
return arr;
}
Object a4 = CodegenAccess.read(iter, compTypeLiteral);
Object arr = Array.newInstance(componentType, 8);
Array.set(arr, 0, a1);
Array.set(arr, 1, a2);
Array.set(arr, 2, a3);
Array.set(arr, 3, a4);
int i = 4;
int arrLen = 8;
while (CodegenAccess.nextToken(iter) == ',') {
if (i == arrLen) {
Object newArr = Array.newInstance(componentType, 2 * arrLen);
System.arraycopy(arr, 0, newArr, 0, arrLen);
arr = newArr;
arrLen = 2 * arrLen;
}
Array.set(arr, i++, CodegenAccess.read(iter, compTypeLiteral));
}
if (i == arrLen) {
return arr;
}
Object newArr = Array.newInstance(componentType, i);
System.arraycopy(arr, 0, newArr, 0, i);
return newArr;
}
}