Skip to content

Package: ExpandedJsonLdDeserializer

ExpandedJsonLdDeserializer

nameinstructionbranchcomplexitylinemethod
ExpandedJsonLdDeserializer()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ExpandedJsonLdDeserializer(Configuration)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
deserialize(Object, Class)
M: 13 C: 45
78%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 1 C: 10
91%
M: 0 C: 1
100%
isNodeReference(Map)
M: 6 C: 6
50%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$verifyTargetType$0(String, Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
processObject(Map)
M: 0 C: 44
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
resolveCollectionValue(String, List)
M: 0 C: 52
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
resolvePropertyValue(String, Map)
M: 0 C: 44
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 8
100%
M: 0 C: 1
100%
resolveValue(Map)
M: 0 C: 46
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 9
100%
M: 0 C: 1
100%
shouldSkipProperty(String)
M: 0 C: 20
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
verifyTargetType(Map)
M: 5 C: 53
91%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 1 C: 11
92%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization;
16:
17: import cz.cvut.kbss.jsonld.ConfigParam;
18: import cz.cvut.kbss.jsonld.Configuration;
19: import cz.cvut.kbss.jsonld.JsonLd;
20: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
21: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
22: import cz.cvut.kbss.jsonld.exception.TargetTypeException;
23: import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
24:
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Optional;
28:
29: public class ExpandedJsonLdDeserializer extends JsonLdDeserializer {
30:
31: private InstanceBuilder instanceBuilder;
32:
33: ExpandedJsonLdDeserializer() {
34: }
35:
36: ExpandedJsonLdDeserializer(Configuration configuration) {
37: super(configuration);
38: }
39:
40: @Override
41: public <T> T deserialize(Object jsonLd, Class<T> resultClass) {
42:• if (!(jsonLd instanceof List)) {
43: throw new JsonLdDeserializationException(
44: "Expanded JSON-LD deserializer requires a JSON-LD array as input.");
45: }
46: final List<?> input = (List<?>) jsonLd;
47:• assert input.size() == 1;
48: final Map<?, ?> root = (Map<?, ?>) input.get(0);
49: this.instanceBuilder = new DefaultInstanceBuilder();
50: instanceBuilder.openObject(resultClass);
51: processObject(root);
52: instanceBuilder.closeObject();
53:• assert resultClass.isAssignableFrom(instanceBuilder.getCurrentRoot().getClass());
54: return resultClass.cast(instanceBuilder.getCurrentRoot());
55: }
56:
57: private void processObject(Map<?, ?> root) {
58: verifyTargetType(root);
59:• for (Map.Entry<?, ?> e : root.entrySet()) {
60: final String property = e.getKey().toString();
61: final boolean shouldSkip = shouldSkipProperty(property);
62:• if (shouldSkip) {
63: continue;
64: }
65:• if (e.getValue() instanceof List) {
66: resolveCollectionValue(property, (List<?>) e.getValue());
67: } else {
68: // Presumably @id
69: instanceBuilder.addValue(property, e.getValue());
70: }
71: }
72: }
73:
74: private void verifyTargetType(Map<?, ?> jsonLdObject) {
75:• if (isNodeReference(jsonLdObject)) {
76: return;
77: }
78: final Object types = jsonLdObject.get(JsonLd.TYPE);
79: final Class<?> targetType = instanceBuilder.getCurrentContextType();
80: final String targetTypeIri = BeanAnnotationProcessor.getOwlClass(targetType);
81:• if (types != null) {
82:• assert types instanceof List;
83: final List<?> typesList = (List<?>) types;
84: final Optional<?> type = typesList.stream().filter(t -> t.toString().equals(targetTypeIri)).findAny();
85:• if (type.isPresent()) {
86: return;
87: }
88: }
89: throw new TargetTypeException("Type <" + targetTypeIri + "> mapped by the target Java class " + targetType +
90: " not found in input JSON-LD object.");
91: }
92:
93: private boolean isNodeReference(Map<?, ?> jsonLdObject) {
94:• return jsonLdObject.size() == 1 && jsonLdObject.containsKey(JsonLd.ID);
95: }
96:
97: private boolean shouldSkipProperty(String property) {
98:• if (!instanceBuilder.isPropertyMapped(property)) {
99:• if (configure().is(ConfigParam.IGNORE_UNKNOWN_PROPERTIES)) {
100: return true;
101: }
102: throw UnknownPropertyException.create(property, instanceBuilder.getCurrentContextType());
103: }
104: return false;
105: }
106:
107: private void resolveCollectionValue(String property, List<?> value) {
108:• if (value.size() == 1 && value.get(0) instanceof Map && !instanceBuilder.isPlural(property)) {
109: resolvePropertyValue(property, (Map<?, ?>) value.get(0));
110: } else {
111: instanceBuilder.openCollection(property);
112:• for (Object item : value) {
113:• if (item instanceof Map) {
114: resolveValue((Map<?, ?>) item);
115: } else {
116: instanceBuilder.addValue(item);
117: }
118: }
119: instanceBuilder.closeCollection();
120: }
121: }
122:
123: private void resolveValue(Map<?, ?> value) {
124:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
125: instanceBuilder.addValue(value.get(JsonLd.VALUE));
126:• } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
127: instanceBuilder.addNodeReference(value.get(JsonLd.ID).toString());
128: } else {
129: final Class<?> elementType = instanceBuilder.getCurrentCollectionElementType();
130: instanceBuilder.openObject(elementType);
131: processObject(value);
132: instanceBuilder.closeObject();
133: }
134: }
135:
136: private void resolvePropertyValue(String property, Map<?, ?> value) {
137:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
138: instanceBuilder.addValue(property, value.get(JsonLd.VALUE));
139:• } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
140: instanceBuilder.addNodeReference(property, value.get(JsonLd.ID).toString());
141: } else {
142: instanceBuilder.openObject(property);
143: processObject(value);
144: instanceBuilder.closeObject();
145: }
146: }
147: }