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%
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: 27
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 6
100%
M: 0 C: 1
100%
resolveValue(Map)
M: 0 C: 30
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
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: 4 C: 49
92%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 10
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: *
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: *
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: final Object types = jsonLdObject.get(JsonLd.TYPE);
76: final Class<?> targetType = instanceBuilder.getCurrentContextType();
77: final String targetTypeIri = BeanAnnotationProcessor.getOwlClass(targetType);
78:• if (types != null) {
79:• assert types instanceof List;
80: final List<?> typesList = (List<?>) types;
81: final Optional<?> type = typesList.stream().filter(t -> t.toString().equals(targetTypeIri)).findAny();
82:• if (type.isPresent()) {
83: return;
84: }
85: }
86: throw new TargetTypeException("Type <" + targetTypeIri + "> mapped by the target Java class " + targetType +
87: " not found in input JSON-LD object.");
88: }
89:
90: private boolean shouldSkipProperty(String property) {
91:• if (!instanceBuilder.isPropertyMapped(property)) {
92:• if (configure().is(ConfigParam.IGNORE_UNKNOWN_PROPERTIES)) {
93: return true;
94: }
95: throw UnknownPropertyException.create(property, instanceBuilder.getCurrentContextType());
96: }
97: return false;
98: }
99:
100: private void resolveCollectionValue(String property, List<?> value) {
101:• if (value.size() == 1 && value.get(0) instanceof Map && !instanceBuilder.isPlural(property)) {
102: resolvePropertyValue(property, (Map<?, ?>) value.get(0));
103: } else {
104: instanceBuilder.openCollection(property);
105:• for (Object item : value) {
106:• if (item instanceof Map) {
107: resolveValue((Map<?, ?>) item);
108: } else {
109: instanceBuilder.addValue(item);
110: }
111: }
112: instanceBuilder.closeCollection();
113: }
114: }
115:
116: private void resolveValue(Map<?, ?> value) {
117:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
118: instanceBuilder.addValue(value.get(JsonLd.VALUE));
119: } else {
120: final Class<?> elementType = instanceBuilder.getCurrentCollectionElementType();
121: instanceBuilder.openObject(elementType);
122: processObject(value);
123: instanceBuilder.closeObject();
124: }
125: }
126:
127: private void resolvePropertyValue(String property, Map<?, ?> value) {
128:• if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
129: instanceBuilder.addValue(property, value.get(JsonLd.VALUE));
130: } else {
131: instanceBuilder.openObject(property);
132: processObject(value);
133: instanceBuilder.closeObject();
134: }
135: }
136: }