-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObjectDeserializer.java
118 lines (106 loc) · 4.53 KB
/
ObjectDeserializer.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
package cz.cvut.kbss.jsonld.deserialization.expanded;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.IdentifierUtil;
import cz.cvut.kbss.jsonld.deserialization.InstanceBuilder;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
import java.lang.reflect.Field;
import java.util.*;
class ObjectDeserializer extends Deserializer<Map<?, ?>> {
private final String property;
private final Class<?> targetClass;
ObjectDeserializer(InstanceBuilder instanceBuilder, DeserializerConfig config, String property) {
super(instanceBuilder, config);
assert property != null;
this.property = property;
this.targetClass = null;
}
ObjectDeserializer(InstanceBuilder instanceBuilder, DeserializerConfig config, Class<?> targetClass) {
super(instanceBuilder, config);
assert targetClass != null;
this.targetClass = targetClass;
this.property = null;
}
@Override
void processValue(Map<?, ?> value) {
openObject(value);
for (Map.Entry<?, ?> e : orderAttributesForProcessing(value).entrySet()) {
final String property = e.getKey().toString();
final boolean shouldSkip = shouldSkipProperty(property);
if (shouldSkip) {
continue;
}
assert e.getValue() instanceof List;
new CollectionDeserializer(instanceBuilder, config, property).processValue((List<?>) e.getValue());
}
instanceBuilder.closeObject();
}
private void openObject(Map<?, ?> value) {
if (property != null) {
instanceBuilder.openObject(getId(value), property, getObjectTypes(value));
} else {
assert targetClass != null;
final Class<?> cls = resolveTargetClass(value, targetClass);
assert targetClass.isAssignableFrom(cls);
instanceBuilder.openObject(getId(value), cls);
}
}
private String getId(Map<?, ?> object) {
return object.containsKey(JsonLd.ID) ? object.get(JsonLd.ID).toString() : IdentifierUtil.generateBlankNodeId();
}
private Map<?, ?> orderAttributesForProcessing(Map<?, ?> value) {
final List<String> propertyOrder = getPropertyOrder();
if (propertyOrder.isEmpty()) {
return value;
}
final Map result = new LinkedHashMap<>(value.size());
for (String property : propertyOrder) {
final Iterator<? extends Map.Entry<?, ?>> it = value.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<?, ?> e = it.next();
if (property.equals(e.getKey())) {
result.put(e.getKey(), e.getValue());
it.remove();
break;
}
}
}
result.putAll(value);
return result;
}
private List<String> getPropertyOrder() {
final Class<?> cls = instanceBuilder.getCurrentContextType();
if (cls == null) {
return Collections.emptyList();
}
final String[] attributeOrder = BeanAnnotationProcessor.getAttributeOrder(cls);
if (attributeOrder.length == 0) {
return Collections.emptyList();
}
final List<Field> fields = BeanAnnotationProcessor.getSerializableFields(instanceBuilder.getCurrentRoot());
final List<String> propertyOrder = new ArrayList<>(attributeOrder.length);
for (String name : attributeOrder) {
final Optional<Field> field = fields.stream().filter(f -> f.getName().equals(name)).findFirst();
if (!field.isPresent()) {
throw new JsonLdDeserializationException(
"Field called " + name + " declared in JsonLdAttributeOrder annotation not found in class " + cls + " .");
}
propertyOrder.add(BeanAnnotationProcessor.getAttributeIdentifier(field.get()));
}
return propertyOrder;
}
private boolean shouldSkipProperty(String property) {
if (JsonLd.ID.equals(property)) {
return true;
}
if (!instanceBuilder.isPropertyMapped(property)) {
if (configuration().is(ConfigParam.IGNORE_UNKNOWN_PROPERTIES)) {
return true;
}
throw UnknownPropertyException.create(property, instanceBuilder.getCurrentContextType());
}
return false;
}
}