-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultJsonLdDeserializer.java
76 lines (58 loc) · 2.04 KB
/
DefaultJsonLdDeserializer.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
package cz.cvut.kbss.jsonld.deserialization;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
import cz.cvut.kbss.jsonld.common.CollectionType;
import java.util.Collection;
import java.util.Stack;
/**
* Default implementation of the JSON-LD deserializer, which takes values parsed from a JSON-LD document and builds
* Java instances from them.
*/
public class DefaultJsonLdDeserializer implements JsonLdDeserializer {
private final Stack<InstanceContext> openInstances = new Stack<>();
private InstanceContext currentInstance;
// TODO Add support for polymorphism
@Override
public void openObject(String property) {
}
@Override
public <T> void openObject(Class<T> cls) {
final T instance = BeanClassProcessor.createInstance(cls);
final InstanceContext ctx = new InstanceContext(instance, BeanAnnotationProcessor.mapSerializableFields(cls));
if (currentInstance != null) {
currentInstance.addItem(instance);
openInstances.push(currentInstance);
}
this.currentInstance = ctx;
}
@Override
public void closeObject() {
if (!openInstances.isEmpty()) {
this.currentInstance = openInstances.pop();
}
}
@Override
public void openCollection(String property) {
}
@Override
public void openCollection(CollectionType collectionType) {
final Collection<?> collection = BeanClassProcessor.createCollection(collectionType);
final InstanceContext context = new InstanceContext(collection);
this.currentInstance = context;
}
@Override
public void closeCollection() {
}
@Override
public void addValue(String property, Object value) {
}
@Override
public void addValue(Object value) {
assert currentInstance != null;
currentInstance.addItem(value);
}
@Override
public Object getCurrentRoot() {
return currentInstance != null ? currentInstance.getInstance() : null;
}
}