forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectionRecordDecoder.java
More file actions
113 lines (97 loc) · 3.7 KB
/
ReflectionRecordDecoder.java
File metadata and controls
113 lines (97 loc) · 3.7 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
package com.jsoniter;
import com.jsoniter.spi.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ReflectionRecordDecoder extends ReflectionObjectDecoder {
private boolean useOnlyFieldRecord = false;
public ReflectionRecordDecoder(ClassInfo classInfo) {
super(classInfo);
if (desc.clazz.isRecord() && !desc.fields.isEmpty() && tempCount == 0) {
tempCount = tempIdx;
tempCacheKey = "temp@" + desc.clazz.getName();
ctorArgsCacheKey = "ctor@" + desc.clazz.getName();
desc.ctor.parameters.addAll(desc.fields);
useOnlyFieldRecord = true;
}
}
@Override
public Decoder create() {
if (useOnlyFieldRecord)
return new OnlyFieldRecord();
if (desc.ctor.parameters.isEmpty()) {
if (desc.bindingTypeWrappers.isEmpty()) {
return new OnlyFieldRecord();
} else {
return new WithWrapper();
}
} else {
return new WithCtor();
}
}
public class OnlyFieldRecord implements Decoder {
@Override
public Object decode(JsonIterator iter) throws IOException {
try {
return decode_(iter);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new JsonException(e);
}
}
private Object decode_(JsonIterator iter) throws Exception {
if (iter.readNull()) {
CodegenAccess.resetExistingObject(iter);
return null;
}
if (iter.tempObjects == null) {
iter.tempObjects = new HashMap<String, Object>();
}
Object[] temp = (Object[]) iter.tempObjects.get(tempCacheKey);
if (temp == null) {
temp = new Object[tempCount];
iter.tempObjects.put(tempCacheKey, temp);
}
Arrays.fill(temp, NOT_SET);
if (!CodegenAccess.readObjectStart(iter)) {
if (requiredIdx > 0) {
throw new JsonException("missing required properties: " + collectMissingFields(0));
}
return createNewObject(iter, temp);
}
Map<String, Object> extra = null;
long tracker = 0L;
Slice fieldName = CodegenAccess.readObjectFieldAsSlice(iter);
Binding binding = allBindings.get(fieldName);
if (binding == null) {
extra = onUnknownProperty(iter, fieldName, extra);
} else {
if (binding.asMissingWhenNotPresent) {
tracker |= binding.mask;
}
temp[binding.idx] = decodeBinding(iter, binding);
}
while (CodegenAccess.nextToken(iter) == ',') {
fieldName = CodegenAccess.readObjectFieldAsSlice(iter);
binding = allBindings.get(fieldName);
if (binding == null) {
extra = onUnknownProperty(iter, fieldName, extra);
} else {
if (binding.asMissingWhenNotPresent) {
tracker |= binding.mask;
}
temp[binding.idx] = decodeBinding(iter, binding);
}
}
if (tracker != expectedTracker) {
throw new JsonException("missing required properties: " + collectMissingFields(tracker));
}
Object obj = createNewObject(iter, temp.clone());
setExtra(obj, extra);
applyWrappers(temp, obj);
return obj;
}
}
}